1
+ # frozen_string_literal: true
2
+
1
3
require './lib/models/session'
2
4
3
5
class Web < Sinatra ::Base
@@ -10,7 +12,7 @@ class Web < Sinatra::Base
10
12
11
13
helpers do
12
14
def title ( *args )
13
- [ *args ] . compact . join ( " - " )
15
+ [ *args ] . compact . join ( ' - ' )
14
16
end
15
17
16
18
def url ( session )
@@ -21,10 +23,8 @@ def image_url(session)
21
23
case Integer ( session . year )
22
24
when 2010 , 2011
23
25
"/images/wwdc-#{ session . year } .jpg"
24
- when 2012 ..2018
26
+ when 2012 ..2019
25
27
"/images/wwdc-#{ session . year } .png"
26
- else
27
- nil
28
28
end
29
29
end
30
30
@@ -36,74 +36,76 @@ def video_url(session)
36
36
before do
37
37
@query = params [ :q ]
38
38
39
- cache_control :public , max_age : 3600
40
-
41
- headers "Content-Security-Policy" => %(
42
- default-src 'self' *.asciiwwdc.com;
43
- form-action 'self';
44
- frame-ancestors 'none';
45
- object-src 'none';
46
- base-uri 'none';
47
- ) . gsub ( "\n " , ' ' ) . squeeze ( ' ' ) . strip ,
48
- "Link" => %(
49
- </css/screen.css>; rel=preload; as=style
50
- ) . gsub ( "\n " , ' ' ) . squeeze ( ' ' ) . strip ,
51
- "Referrer-Policy" => "same-origin" ,
52
- "Server" => '' ,
53
- "Strict-Transport-Security" => "max-age=63072000; includeSubDomains; preload" ,
54
- "X-Content-Type-Options" => "nosniff" ,
55
- "X-Frame-Options" => "DENY" ,
56
- "X-XSS-Protection" => "1; mode=block" unless settings . development?
39
+ cache_control :public , max_age : 86_400
40
+
41
+ unless settings . development?
42
+ headers 'Content-Security-Policy' => %(
43
+ default-src 'self' *.asciiwwdc.com;
44
+ form-action 'self';
45
+ frame-ancestors 'none';
46
+ object-src 'none';
47
+ base-uri 'none';
48
+ ) . gsub ( "\n " , ' ' ) . squeeze ( ' ' ) . strip ,
49
+ 'Link' => %(
50
+ </css/screen.css>; rel=preload; as=style
51
+ ) . gsub ( "\n " , ' ' ) . squeeze ( ' ' ) . strip ,
52
+ 'Referrer-Policy' => 'same-origin' ,
53
+ 'Server' => '' ,
54
+ 'Strict-Transport-Security' => 'max-age=63072000; includeSubDomains; preload' ,
55
+ 'X-Content-Type-Options' => 'nosniff' ,
56
+ 'X-Frame-Options' => 'DENY' ,
57
+ 'X-XSS-Protection' => '1; mode=block'
58
+ end
57
59
end
58
60
59
61
error Sinatra ::Param ::InvalidParameterError do
60
- haml :error , : locals => { : msg => env [ 'sinatra.error' ] }
62
+ haml :error , locals : { msg : env [ 'sinatra.error' ] }
61
63
end
62
64
63
65
error 404 do
64
- haml :error , : locals => { : msg => " 404 Not found" }
66
+ haml :error , locals : { msg : ' 404 Not found' }
65
67
end
66
68
67
69
not_found do
68
- haml :error , : locals => { : msg => " 404 Not found" }
70
+ haml :error , locals : { msg : ' 404 Not found' }
69
71
end
70
72
71
73
get '/' do
72
74
@sessions = Session . select ( :title , :year , :number , :track )
73
- . order ( :year , :number )
74
- . all
75
- . group_by ( &:year )
75
+ . order ( :year , :number )
76
+ . all
77
+ . group_by ( &:year )
76
78
haml :index
77
79
end
78
80
79
81
get '/contribute' do
80
82
haml :contribute
81
83
end
82
84
83
- get '/:year/sessions/:number' , provides : [ : html, : json, : vtt, : txt] do
85
+ get '/:year/sessions/:number' , provides : %i[ html json vtt txt ] do
84
86
param :year , Integer , required : true
85
87
param :number , Integer , required : true
86
88
87
89
halt 404 unless @session = Session . first ( year : params [ :year ] , number : params [ :number ] )
88
90
89
- link video_url ( @session ) , : rel => :alternate
91
+ link video_url ( @session ) , rel : :alternate
90
92
91
93
respond_to do |f |
92
- f . html { haml :session }
93
- f . json { @session . to_json }
94
- f . vtt { send_file "data/#{ params [ :year ] } /#{ params [ :number ] } .vtt" , type : :vtt }
95
- f . txt { @session . transcript }
94
+ f . html { haml :session }
95
+ f . json { @session . to_json }
96
+ f . vtt { send_file "data/#{ params [ :year ] } /#{ params [ :number ] } .vtt" , type : :vtt }
97
+ f . txt { @session . transcript }
96
98
end
97
99
end
98
100
99
- get '/search' , provides : [ : html, : json] do
101
+ get '/search' , provides : %i[ html json ] do
100
102
param :q , String , blank : false
101
103
param :year , Integer , in : 2010 ..2018
102
104
103
105
@sessions = Session . search ( @query , params [ :year ] )
104
106
105
107
respond_to do |f |
106
- f . html { haml :search }
108
+ f . html { haml :search }
107
109
f . json do
108
110
{
109
111
query : @query ,
@@ -131,5 +133,4 @@ def video_url(session)
131
133
pass
132
134
end
133
135
end
134
-
135
136
end
0 commit comments