This repository has been archived by the owner on Oct 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
web.rb
170 lines (137 loc) · 4.31 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
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
# frozen_string_literal: true
require './lib/models/session'
class Web < Sinatra::Base
register Sinatra::Contrib
helpers Sinatra::Param
set :raise_sinatra_param_exceptions, true
set :show_exceptions, !settings.production?
set :raise_errors, true
set :supported_mime_types, lambda {
[
'text/html',
'application/json',
'text/vtt',
'text/plain',
'application/rss+xml'
].flat_map { |content_type| MIME::Types[content_type] }
}.call
helpers do
def title(*args)
[*args].compact.join(' - ')
end
def url(session)
request.base_url + "/#{session.year}/sessions/#{session.number}"
end
def image_url(session)
request.base_url + case Integer(session.year)
when 2010, 2011
"/images/wwdc-#{session.year}.jpg"
else
"/images/wwdc-#{session.year}.png"
end
end
def video_url(session)
"https://developer.apple.com/videos/wwdc/#{session.year}/?id=#{session.number}"
end
end
before do
@query = params[:q]
cache_control :public, max_age: 86_400
settings.supported_mime_types.each do |mimetype|
extension = '.' + mimetype.preferred_extension
next unless request.path.end_with?(extension)
request.accept.unshift(mimetype)
request.path_info = request.path_info.chomp(extension)
break
end
end
after do
if settings.production? && response.content_type.nil?
headers 'Content-Security-Policy' => %(
default-src 'self' *.asciiwwdc.com;
form-action 'self';
frame-ancestors 'none';
object-src 'none';
base-uri 'none';
).gsub("\n", ' ').squeeze(' ').strip,
'Link' => %(
</css/screen.css>; rel=preload; as=style
).gsub("\n", ' ').squeeze(' ').strip,
'Referrer-Policy' => 'same-origin',
'Server' => '',
'Strict-Transport-Security' => 'max-age=63072000; includeSubDomains; preload',
'X-Content-Type-Options' => 'nosniff',
'X-Frame-Options' => 'DENY',
'X-XSS-Protection' => '1; mode=block'
end
end
error Sinatra::Param::InvalidParameterError do
haml :error, locals: { message: env['sinatra.error'], code: 400 }
end
error 404 do
haml :error, locals: { message: 'Not found', code: 404 }
end
get '/' do
@sessions = Session.select(:title, :year, :number, :track)
.order(:year, :number)
.all
.group_by(&:year)
haml :index
end
get '/contribute' do
haml :contribute
end
get '/:year/sessions/:number.?:format?', provides: %i[html json vtt txt] do
param :year, Integer, required: true
param :number, Integer, required: true
unless @session = Session.first(year: params[:year], number: params[:number])
halt 404
end
link video_url(@session), rel: :alternate
respond_to do |f|
f.html { haml :session }
f.json { @session.to_json }
f.vtt { send_file "data/#{params[:year]}/#{params[:number]}.vtt", type: :vtt }
f.txt { @session.transcript }
end
end
get '/search.?:format?', provides: %i[html json rss] do
param :q, String, blank: false
param :year, Integer, in: 2010..2019
@sessions = Session.search(@query, params[:year])
respond_to do |f|
f.html { haml :search }
f.json do
headers['Content-Type'] = 'application/json'
{
query: @query,
results: @sessions.collect(&:to_h)
}.to_json
end
f.rss {
headers['Content-Type'] = 'application/rss+xml'
builder :'search.rss'
}
end
end
get '/sitemap.xml' do
headers['Content-Type'] = 'application/xml'
@sessions ||= Session.select(:title, :year, :number, :track)
.order(:year, :number)
.all
builder :'sitemap.xml'
end
get '/opensearch.xml' do
headers['Content-Type'] = 'text/xml'
builder :'opensearch.xml'
end
get %r{/(?<year>\d{4})/} do
param :year, Integer
case params[:year]
when 2010..2019
redirect "/#wwdc-#{params[:year]}"
else
pass
end
end
end