-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathseinfeld_calendar.rb
170 lines (141 loc) · 3.94 KB
/
seinfeld_calendar.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
require 'rubygems'
require 'sinatra'
require 'json'
$: << File.join(File.dirname(__FILE__), 'lib')
require 'seinfeld/models'
require 'seinfeld/calendar_helper'
$0 = __FILE__
error do
e = request.env['sinatra.error']
puts "#{e.class}: #{e.message}\n#{e.backtrace.join("\n ")}"
end
configure do
require File.dirname(__FILE__) + '/config/seinfeld.rb'
end
before do
Time.zone = "UTC"
end
get '/' do
cache_for 5.minutes
@recent_users = Seinfeld::User.best_current_streak
@alltime_users = Seinfeld::User.best_alltime_streak
haml :index
end
get '/~:name.json' do
show_user_json
end
get '/~:name/widget' do
cache_for 1.hour
@user = Seinfeld::User.first(:login => params[:name])
haml :widget
end
get '/~:name' do
show_user_calendar
end
get '/~:name/:year.json' do
show_user_json
end
get '/~:name/:year' do
show_user_calendar
end
get '/~:name/:year/:month.json' do
show_user_json
end
get '/~:name/:year/:month' do
show_user_calendar
end
get '/group/:names' do
show_group_calendar
end
get '/group/:names/:year/:month' do
show_group_calendar
end
post '/github' do
if params[:token] == Seinfeld::User.creation_token
Seinfeld::User.process_new_github_user(params[:subject])
else
redirect "/"
end
end
get '/*' do
redirect "~#{params[:splat].join("/")}", 301
end
helpers do
include Seinfeld::CalendarHelper
def page_title
"%s's Calendar" % @user.login
end
def get_user_and_progressions(extra = 0, name = params[:name])
[:year, :month].each do |key|
value = params[key].to_i
params[key] = value.zero? ? Date.today.send(key) : value
end
if @user = Seinfeld::User.first(:login => name)
Time.zone = @user.time_zone || "UTC"
progressions = @user.progress_for(params[:year], params[:month], extra)
end
Set.new(progressions || [])
end
def show_user_calendar
cache_for 5.minutes
@progressions = get_user_and_progressions(6)
if @user
haml :show
else
redirect "/"
end
end
def show_group_calendar
cache_for 5.minutes
@progressions = Set.new
@users = params[:names].split(',')
@users.each do |name|
@progressions.merge get_user_and_progressions(6, name)
end
haml :group
end
def show_user_json
cache_for 5.minutes
@progressions = get_user_and_progressions
json = {:days => @progressions.map { |p| p.to_s }.sort!, :longest_streak => @user.longest_streak, :current_streak => @user.current_streak}.to_json
if params[:callback]
"#{params[:callback]}(#{json})"
else
json
end
end
def link_to_user(user, streak_count = :current_streak)
%(<a href="/~#{user.login}">#{user.login} (#{user.send(streak_count)})</a>)
end
def seinfeld
now = Date.new(params[:year], params[:month])
prev_month = now << 1
next_month = now >> 1
calendar :year => now.year, :month => now.month,
:previous_month_text => %(<a href="/~#{@user.login}/#{prev_month.year}/#{prev_month.month}">Previous Month</a>),
:next_month_text => %(<a href="/~#{@user.login}/#{next_month.year}/#{next_month.month}" class="next">Next Month</a>) do |d|
if @progressions.include? d
[d.mday, {:class => "progressed"}]
else
[d.mday, {:class => "slacked"}]
end
end
end
def group_seinfeld
now = Date.new(params[:year], params[:month])
prev_month = now << 1
next_month = now >> 1
calendar :year => now.year, :month => now.month,
:previous_month_text => %(<a href="/group/#{params[:names]}/#{prev_month.year}/#{prev_month.month}">Previous Month</a>),
:next_month_text => %(<a href="/group/#{params[:names]}/#{next_month.year}/#{next_month.month}" class="next">Next Month</a>) do |d|
if @progressions.include? d
[d.mday, {:class => "progressed"}]
else
[d.mday, {:class => "slacked"}]
end
end
end
def cache_for(time)
response['Cache-Control'] = "public, max-age=#{time.to_i}"
end
end