Permalink
Browse files

yay, the app runs

  • Loading branch information...
1 parent f7a87f3 commit 89e942b8350e9bab4d867030dc139b716b1a52c3 @technoweenie committed May 2, 2010
Showing with 184 additions and 9 deletions.
  1. +5 −0 Rakefile
  2. +3 −5 config.ru
  3. +7 −1 config/database.sample.yml
  4. +1 −1 lib/seinfeld.rb
  5. +166 −0 lib/seinfeld/app.rb
  6. +2 −2 lib/seinfeld/calendar_helper.rb
View
5 Rakefile
@@ -28,6 +28,11 @@ namespace :seinfeld do
Seinfeld.configure
end
+ desc "Start CalendarAboutNothing for development"
+ task :start do
+ system "shotgun config.ru"
+ end
+
desc "Inspect USER."
task :show => :init do
raise "Need USER=" if ENV['USER'].to_s.size.zero?
View
8 config.ru
@@ -1,6 +1,4 @@
-require File.dirname(__FILE__) + "/../seinfeld_calendar.rb"
+require ::File.dirname(__FILE__) + "/lib/seinfeld.rb"
-set :run, false
-set :env, ENV['APP_ENV'] || :production
-
-run Sinatra::Application
+Seinfeld::App.set :environment, ENV['RACK_ENV'] || ENV['RAILS_ENV'] || :production
+run Seinfeld::App
View
8 config/database.sample.yml
@@ -1,11 +1,17 @@
+production:
+ adapter: mysql
+ encoding: utf8
+ database: seinfeld
+ username: root
+ password:
+ host: localhost
development:
adapter: mysql
encoding: utf8
database: seinfeld
username: root
password:
host: localhost
-
test:
adapter: sqlite3
encoding: utf8
View
2 lib/seinfeld.rb
@@ -28,7 +28,7 @@ class << self
attr_accessor :logger
end
- [:User, :Progression, :Feed, :Streak].each do |const|
+ [:App, :User, :Progression, :Feed, :Streak, :CalendarHelper].each do |const|
autoload const, "seinfeld/#{const.to_s.underscore}"
end
View
166 lib/seinfeld/app.rb
@@ -0,0 +1,166 @@
+require 'sinatra/base'
+require 'json'
+
+class Seinfeld
+ class App < Sinatra::Base
+ error do
+ e = request.env['sinatra.error']
+ puts "#{e.class}: #{e.message}\n#{e.backtrace.join("\n ")}"
+ end
+
+ configure do
+ Seinfeld.configure
+ end
+
+ set :root, Seinfeld.root
+ set :app_file, __FILE__
+ enable :static
+ configure :development do
+ enable :show_exceptions, :dump_errors
+ disable :raise_errors, :clean_trace
+ 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.where(:login => params[:name]).first
+ 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
+
+ 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.where(:login => name).first
+ 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
+ end
+end
View
4 lib/seinfeld/calendar_helper.rb
@@ -1,6 +1,6 @@
require 'date'
-module Seinfeld
+class Seinfeld
# CalendarHelper allows you to draw a databound calendar with fine-grained CSS formatting
#
# http://topfunky.net/svn/plugins/calendar_helper/lib/calendar_helper.rb
@@ -151,7 +151,7 @@ def calendar(options = {}, &block)
end
end unless last.wday == last_weekday
cal << "</tr></tbody></table>"
- cal.to_s
+ cal.join("\n")
end
private

0 comments on commit 89e942b

Please sign in to comment.