forked from ASCIIwwdc/asciiwwdc.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
69 lines (55 loc) · 1.5 KB
/
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
require './lib/models/session'
class Web < Sinatra::Base
register Sinatra::Contrib
helpers Sinatra::Param
helpers do
def title(*args)
[*args].compact.join(" - ")
end
def url(session)
"/#{session.year}/sessions/#{session.number}"
end
def video_url(session)
case Integer(session.year)
when 2013
"https://developer.apple.com/wwdc/videos/?id=#{session.number}"
when 2010..2012
"https://developer.apple.com/videos/wwdc/2012/?id=#{session.number}"
end
end
end
before do
@query = Rack::Utils.escape_html(params[:q]) if params[:q]
end
get '/' do
@sessions = Session.order(:year, :number).all
haml :index
end
get '/2013' do
redirect '/'
end
get '/:year/sessions/:number', provides: [:html, :json, :vtt, :txt] do
param :year, Integer, required: true
param :number, Integer, required: true
halt 404 unless @session = Session.first(year: params[:year], number: params[:number])
respond_to do |f|
f.html {haml :session}
f.json {@session.to_json}
f.vtt {send_file "data/#{params[:year]}/#{params[:number]}.srt", type: :vtt}
f.txt {@session.transcript}
end
end
get '/search', provides: [:html, :json] do
param :q, String, blank: false
@sessions = Session.search(@query)
respond_to do |f|
f.html {haml :search}
f.json do
{
query: @query,
results: @sessions.collect(&:to_h)
}.to_json
end
end
end
end