This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
showbot_web.rb
291 lines (251 loc) · 7.88 KB
/
showbot_web.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# showbot_web.rb
# The web front-end for showbot
# Gems
require 'bundler/setup'
require 'coffee_script'
require 'sinatra' unless defined?(Sinatra)
require "sinatra/reloader" if development?
require 'json'
require File.join(File.dirname(__FILE__), 'environment')
SHOWS_JSON = File.expand_path(File.join(File.dirname(__FILE__), "public", "shows.json")) unless defined? SHOWS_JSON
SAMPLE_TITLES_JSON = File.expand_path("hypercritical.json")
class ShowbotWeb < Sinatra::Base
configure do
set :public_folder, "#{File.dirname(__FILE__)}/public"
set :views, "#{File.dirname(__FILE__)}/views"
set :shows, Shows.new(SHOWS_JSON)
end
configure(:production, :development) do
enable :logging
end
configure :development do
register Sinatra::Reloader
end
# ------------------
# Pages
# ------------------
# CoffeeScript
get '/js/showbot.js' do
coffee :'coffeescripts/showbot'
end
get '/' do
@title = "Home"
suggestion_sets = Suggestion.recent.group_by_show
view_mode = params[:view_mode] || 'tables'
haml :index, :locals => {suggestion_sets: suggestion_sets, :view_mode => view_mode}
end
get '/titles' do
@title = "Title Suggestions in the last 24 hours"
view_mode = params[:view_mode] || 'tables'
suggestion_sets = Suggestion.recent.group_by_show
if view_mode == 'hacker'
content_type 'text/plain'
haml :'suggestion/hacker_mode', :locals => {suggestion_sets: suggestion_sets, :view_mode => view_mode}, :layout => false
else
haml :'suggestion/index', :locals => {suggestion_sets: suggestion_sets, :view_mode => view_mode}
end
end
get '/links' do
@title = "Suggested Links in the last 24 hours"
@links = Link.recent.all(:order => [:created_at.desc])
haml :links
end
get '/all' do
suggestion_sets = Suggestion.all(:order => [:created_at.desc]).group_by_show
content_type 'text/plain'
haml :'suggestion/hacker_mode', :locals => {suggestion_sets: suggestion_sets}, :layout => false
end
get '/titles/:id/vote_up' do
content_type :json
# Only allow XHR requests for voting
if request.xhr?
suggestion = Suggestion.get(params[:id])
cluster_top = suggestion.top_of_cluster? # figure out if top before adding new vote
suggestion.vote_up(request.ip)
{votes: suggestion.votes.count.to_s,
cluster_top: cluster_top,
cluster_id: suggestion.cluster_id,
cluster_votes: suggestion.total_for_cluster}.to_json
else
redirect '/'
end
end
# Word cloud generation
get '/clouds_between/:days_a/:days_b' do
days_ago = [params[:days_a].to_i, params[:days_b].to_i].sort
suggestion_sets = Suggestion.all(:created_at => ( (DateTime.now - days_ago[1])..(DateTime.now - days_ago[0]) ), :order => [:created_at.desc]).group_by_show
haml :'clouds', :locals => { cloud_data: WordCount.generate_clouds(suggestion_sets) }
end
get '/cloud_svg/:year/:month/:day/:index' do
the_date = DateTime.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
bracketed_suggestion_sets = Suggestion.all(:created_at => ( (the_date - 1)..(the_date + 2) ), :order => [:created_at.desc]).group_by_show
suggestion_sets = bracketed_suggestion_sets.select { |set| set.suggestions[0].created_at.to_date == the_date.to_date }
haml :'clouds_svg', :locals => { cloud_data: WordCount.generate_clouds(suggestion_sets), cloud_index: params[:index].to_i }
end
get '/num_clouds_on_date/:year/:month/:day' do
content_type :json
the_date = DateTime.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
bracketed_suggestion_sets = Suggestion.all(:created_at => ( (the_date - 1)..(the_date + 2) ), :order => [:created_at.desc]).group_by_show
{ num_clouds: bracketed_suggestion_sets.select { |set| set.suggestions[0].created_at.to_date == the_date.to_date }.count }.to_json
end
# ------------------
# API
# ------------------
# Creates a title suggestion based on a POST request with valid
# title and user parameters.
#
# title - String less than 40 characters.
# user - String username to use.
# api_key - Your API key.
#
# Examples
#
# Context: Posting a valid show title with a valid user.
# POST /suggestions/new
# params: {
# title: 'Omg Title',
# user: 'mrman',
# api_key: 'keyhere'
# }
#
# Response:
# {
# suggestion: {
# user: 'mrman',
# title: 'Omg Title'
# }
# }
#
# Context: Posting a show title that's too long.
# POST /suggestions/new
# params: {
# title: 'Super freaking long title that will make showbot cry.',
# user: 'badman',
# api_key: 'keyhere'
# }
#
# Response:
# {
# error: 'That suggestion was too long. Showbot is sorry. Think title,
# not transcript.'
# }
#
# Context: The same title suggested seconds later.
# POST /suggestions/new
# params: {
# title: 'Same Title',
# user: 'slowpoke',
# api_key: 'keyhere'
# }
#
# Response:
# {
# error: 'Darn, fastman beat you to "Same Title".'
# }
#
# Returns a JSON response with the original suggestion and an error
# message if one was generated.
post '/suggestions/new' do
content_type :json
api_key = params[:api_key]
response = nil
if api_key and ApiKey.first(value: api_key)
title = params[:title]
user = params[:user]
if title && user
suggestion = Suggestion.create(
title: title,
user: user
)
if suggestion.saved?
response = {
suggestion: {
title: title,
user: user
}
}
else
response = {
error: suggestion.errors.first.first
}
end
else
if !title
response = {
error: 'Missing / Invalid Title'
}
else
response = {
error: 'Missing / Invalid User'
}
end
end
end
if response
response.to_json
else
halt 404, {
error: "Invalid Api Key #{api_key}"
}.to_json
end
end
# ------------------
# Helpers
# ------------------
helpers do
include Rack::Utils
alias_method :h, :escape_html
def external_link(link)
/^http/.match(link) ? link : "http://#{link}"
end
# Returns a string truncated in the middle
# Note: Rounds max_length down to nearest even number
def truncate_string(string, max_length)
if string.length > max_length
# +/- 2 is to account for the elipse in the middle
"#{string[0..(max_length/2)-2]}...#{string[-(max_length/2)+2..-1]}"
else
string
end
end
def show_title_for_slug(slug)
text = "Show Not Listed"
if slug
text = Shows.find_show_title(slug)
end
text
end
def suggestion_set_hr(suggestion_set)
"<h2 class='show_break'>#{show_title_for_slug(suggestion_set.slug)}</h2>"
end
def link_to_vote_up(suggestion)
html = ''
# onclick returns false to keep from allowing
html << "<a href='#' class='vote_up' onclick='return false;' data-id='#{suggestion.id}'>"
html << "<span class='vote_arrow'/>"
html << "</a>"
end
def link_and_vote_count(suggestion, user_ip)
html = ''
extra_classes = []
if suggestion.user_already_voted?(user_ip)
extra_classes << 'voted'
else
html << link_to_vote_up(suggestion)
end
html << "<span class='vote_count #{extra_classes.join(',')}'>#{suggestion.votes_counter}</span>"
end
def development?
settings.development?
end
def cloud_json(cloud_data)
return if cloud_data.nil?
cloud_data.map do |d|
{
title: "#{show_title_for_slug(d[:show])} #{d[:time].to_date.to_s}",
data: d[:data]
}
end.to_json
end
end # helpers
end