-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptn.rb
253 lines (211 loc) · 5.56 KB
/
captn.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
require "bundler/setup"
require "sinatra"
require 'sinatra/browserid'
require "sinatra/flash"
require "sinatra/reloader" if development?
require "data_mapper"
require "digest/md5"
require "httparty"
require "json"
Sinatra.register Sinatra::BrowserID
Sinatra.register Sinatra::Flash
set :browserid_login_button, :blue
set :sessions, true
DBURL = ENV["DATABASE_URL"] || File.join("sqlite3://#{Dir.pwd}", "captn.db")
DataMapper::Logger.new($stdout, :debug) if development?
DataMapper.setup(:default, DBURL)
class Captainship
include DataMapper::Resource
property :id, Serial
property :url, String
property :name, String
property :avatar, String
property :email, String, :required => true
property :started_at, DateTime, :required => true
property :created_at, DateTime
def to_hash
Digest::MD5.hexdigest(@email)
end
end
DataMapper.finalize
helpers do
def get_timeline_string(date)
today = Date.today
return "current" if date.cweek == today.cweek
date > today ? "future" : "past"
end
def pretty_date(date)
date.strftime "%A %e %B %G"
end
def md5_email(email)
Digest::MD5.hexdigest(email)
end
def lookup_user(email)
@cache = {} unless @cache
hash = md5_email(email)
user = @cache[hash]
unless user
url = "http://who.theskiff.org/profiles/#{hash}.json"
begin
request = HTTParty.get(url, :timeout => 5)
user = JSON.parse(request.body)
@cache[hash] = user
rescue
user = {}
end
end
user
end
def lookup_captain(captain)
user = lookup_user(captain.email)
captain.name = user["real_name"]
captain.avatar = user["profile_image"]
captain.url = user["html"]
captain
end
def valid_cweek?(cweek)
cweek.to_s =~ /^\d{1,2}$/ and (1..52) === cweek.to_i
end
def date_for_cweek(cweek, year=nil)
year = Date.today.year if year.nil?
Date.commercial(year, cweek, 1, Date::ENGLAND)
end
def render_weeks_for_range(start_cweek, end_cweek, year=nil)
weeks = []
first = date_for_cweek(start_cweek, year)
last = date_for_cweek(end_cweek, year)
captains = Captainship.all(
:started_at.lt => last,
:started_at.gte => first,
:order => :started_at
)
captains_hash = {}
captains.each { |c| captains_hash[c.started_at.to_date.iso8601] = c }
first.step(last, 7) do |date|
captain = captains_hash[date.iso8601]
week = {:date => date}
week[:captain] = lookup_captain(captain) if captain
weeks << week
end
if accept_json?
render_weeks_as_json(weeks)
else
render_weeks_as_html(weeks)
end
end
def render_weeks_as_html(weeks)
user = lookup_user(authorized_email) if authorized?
erb :index, :locals => {
:user => user,
:weeks => weeks,
:render_login_button => render_login_button
}
end
def render_weeks_as_json(weeks)
list = []
weeks.each do |week|
list << {
:hash => week[:captain] ? md5_email(week[:captain].email) : nil,
:week => week[:date].iso8601
}
end
render_json_list list
end
def render_json(response)
content_type :json
body = response.to_json
if params[:callback]
content_type :js
body = "#{params[:callback]}(#{body})"
end
body
end
def render_json_list(items=[])
render_json({
:meta => {
:total => items.length
},
:items => items
})
end
def accept_json?
# This seems incredibly hacky but #accept? returns a string?
is_jsonp = !!(params[:callback] and request.accept?("text/javascript"))
is_json = request.preferred_type(%w[text/html application/json]) == "application/json"
is_json or is_jsonp
end
end
get "/" do
today = Date.today
render_weeks_for_range(today.cweek - 1, today.cweek + 11)
end
get "/logout/" do
logout!
redirect back
end
get "/captain.json" do
start = date_for_cweek(Date.today.cweek)
captain = Captainship.first(:started_at => start)
response = {:captain => nil}
if captain
response[:captain] = {
:week => start.iso8601,
:hash => md5_email(captain.email)
}
end
render_json response
end
post "/captainships/" do
authorize!
week = params[:week].to_i
year = params[:year]
year = year.to_i unless year.nil?
notice = {:type => :error}
if valid_cweek? week
started_at = date_for_cweek(week, year)
exists = Captainship.first(:started_at => started_at)
if exists
notice[:msg] = "There is already a Captain for this week"
else
Captainship.create(
:email => authorized_email,
:started_at => date_for_cweek(week, year)
)
notice = {
:type => :success,
:msg => "Thanks for volunteering!"
}
end
end
unless notice[:msg]
notice[:msg] = "There was an error when trying to save this date"
end
flash[:notice] = notice
redirect back
end
delete "/captainships/" do
authorize!
week = params[:week].to_i
year = params[:year]
year = year.to_i unless year.nil?
if valid_cweek? week
started_at = date_for_cweek(week, year)
captn = Captainship.all(:started_at => started_at, :email => authorized_email)
if captn and captn.destroy()
flash[:notice] = {
:type => :success,
:msg => "Cancelled your captainship!"
}
end
end
redirect back
end
get "/:year/from/:start/to/:end/" do
start = params[:start]
last = params[:end]
if valid_cweek? start and valid_cweek? last
render_weeks_for_range(start.to_i, last.to_i, params[:year].to_i)
else
pass
end
end