kastner / seinfeld forked from entp/seinfeld

This URL has Read+Write access

seinfeld / seinfeld_calendar.rb
100644 101 lines (83 sloc) 2.407 kb
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
unless Object.const_defined?(:Seinfeld)
  # setup a config.ru for rack, or some other ruby config file
  $: << File.join(File.dirname(__FILE__), 'lib')
  require 'seinfeld/models'
  DataMapper.setup :default, 'mysql://localhost/seinfeld'
end
 
require 'seinfeld/calendar_helper'
require 'sinatra'
require 'json'
 
get '/' do
  @recent_users = Seinfeld::User.best_current_streak
  @alltime_users = Seinfeld::User.best_alltime_streak
  haml :index
end
 
get '/~:name' do
  show_user_calendar
end
 
get '/~:name.json' do
  show_user_json
end
 
get '/~:name/:year' do
  show_user_calendar
end
 
get '/~:name/:year.json' do
  show_user_json
end
 
get '/~:name/:year/:month' do
  show_user_calendar
end
 
get '/~:name/:year/:month.json' do
  show_user_json
end
 
post '/github' do
  if params[:token] == Seinfeld::User.creation_token
    Seinfeld::User.process_new_github_user(params[:subject])
  else
    redirect "/"
  end
end
 
helpers do
  include Seinfeld::CalendarHelper
 
  def page_title
    "%s's Calendar" % @user.login
  end
 
  def get_user_and_progressions
    [: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 => params[:name])
      @progressions = Set.new @user.progress_for(params[:year], params[:month])
    end
  end
 
  def show_user_calendar
    get_user_and_progressions
    if @user
      haml :show
    else
      redirect "/"
    end
  end
 
  def show_user_json
    get_user_and_progressions
    {:days => @progressions.map { |p| p.to_s }, :longest_streak => @user.longest_streak, :current_streak => @user.current_streak}.to_json
  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
end