Skip to content

Commit

Permalink
Added location to User model and added ability to update timezone via…
Browse files Browse the repository at this point in the history
… Geonames
  • Loading branch information
bhb committed Aug 30, 2010
1 parent 4fd334a commit 3673962
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 11 deletions.
9 changes: 9 additions & 0 deletions db/migrate/20100827203011_add_location_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddLocationToUsers < ActiveRecord::Migration
def self.up
add_column :seinfeld_users, :location, :string
end

def self.down
remove_column :seinfeld_users, :location
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100530192310) do
ActiveRecord::Schema.define(:version => 20100827203011) do

create_table "seinfeld_progressions", :force => true do |t|
t.date "created_at"
Expand All @@ -30,6 +30,7 @@
t.string "time_zone", :limit => 50
t.boolean "disabled", :default => false
t.string "etag"
t.string "location"
end

add_index "seinfeld_users", ["current_streak"], :name => "index_seinfeld_users_current_streak"
Expand Down
3 changes: 2 additions & 1 deletion lib/seinfeld.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'erb'
require 'logger'
require 'active_record' # v3
require 'fileutils'

# Gives all the AR models their own tablenamespace:
#
Expand Down Expand Up @@ -81,4 +82,4 @@ def self.path_from_root(path)

Seinfeld.root = File.expand_path(File.join(dir, '..'))
Seinfeld.env = ENV['RACK_ENV'] || ENV['RAILS_ENV']
Seinfeld.db_config_path = 'config/database.yml'
Seinfeld.db_config_path = 'config/database.yml'
2 changes: 1 addition & 1 deletion lib/seinfeld/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,4 @@ def oauth_redirect_url
uri.to_s
end
end
end
end
3 changes: 2 additions & 1 deletion lib/seinfeld/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(user)

def run(today = Date.today)
today ||= Date.today
@user.update_timezone!
Time.zone = @user.time_zone || "UTC"
if feed = Feed.fetch(@user)
@user.etag = feed.etag
Expand All @@ -23,4 +24,4 @@ def run(today = Date.today)
feed
end
end
end
end
40 changes: 39 additions & 1 deletion lib/seinfeld/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'yajl'
require 'faraday'

class Seinfeld
class User < ActiveRecord::Base
has_many :progressions, :order => 'seinfeld_progressions.created_at', :dependent => :delete_all
Expand Down Expand Up @@ -142,5 +145,40 @@ def update_from_streaks(streaks, today)
self.longest_streak_end = highest_streak.ended
end
end

def time_left
require 'ruby-debug'
Time.zone = self.time_zone
now = Time.zone.now
tomorrow = Time.zone.parse(Date.tomorrow.to_s)
seconds_until_tomorrow = (tomorrow - now)
hours = (seconds_until_tomorrow/3600).to_i
minutes = (seconds_until_tomorrow/60 - hours * 60).to_i
'%d h, %d min' % [hours, minutes]
end

# Sets User timezone based on location.
def update_timezone!
user_agent = 'Calendar About Nothing: http://github.com/technoweenie/seinfeld'
connection ||= Faraday::Connection.new(
:headers => {'User-Agent' => user_agent}
) do |b|
b.adapter :typhoeus
end
#data = Yajl::Parser.parse(connection.get("http://github.com/api/v2/json/user/show/#{login}").body)
#location = data["user"]["location"]
return if location.nil? || location.empty?
place_data = Yajl::Parser.parse(connection.get("http://ws.geonames.org/searchJSON?maxRows=1&q=#{location}").body)
location_data = place_data["geonames"].first
lat = location_data["lat"]
lng = location_data["lng"]
computed_time_zone = Yajl::Parser.parse(connection.get("http://ws.geonames.org/timezoneJSON?lat=#{lat}&lng=#{lng}").body)
time_zone_id = computed_time_zone["timezoneId"]
reverse_mapping = ActiveSupport::TimeZone::MAPPING.invert
if reverse_mapping.key?(time_zone_id)
self.time_zone = reverse_mapping[time_zone_id]
end
save!
end
end
end
end
7 changes: 7 additions & 0 deletions public/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ body{
text-transform:none;
}

#zone {
background: #DFDFDF;
margin: 5px 0px 10px 0px;
padding: 5px 0px 5px 0px;
text-align: center;
}

.type{
display:block;
font-size:10px;
Expand Down
6 changes: 3 additions & 3 deletions test/feed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class FeedTest < ActiveSupport::TestCase
data = feed_data(:simple)

setup_once do
Time.zone = 'Pacific Time (US & Canada)'
@feed = Seinfeld::Feed.new :technoweenie, data
Time.zone = 'Pacific Time (US & Canada)'
@feed = Seinfeld::Feed.new :technoweenie, data
end

test "parses JSON data" do
Expand All @@ -28,4 +28,4 @@ class FeedTest < ActiveSupport::TestCase
Date.civil(2009, 12, 16),
Date.civil(2009, 12, 15)], @feed.committed_days
end
end
end
4 changes: 2 additions & 2 deletions test/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class UserTest < ActiveSupport::TestCase
assert_equal Date.civil(2008, 1, 1), @user.streak_end
end

test "#update_progress with user, starting new strea" do
test "#update_progress with user, starting new streak" do
parsed_dates = [Date.civil(2007, 12, 31), Date.civil(2008, 1, 2)]

assert_equal 2, @user.progressions.count
Expand Down Expand Up @@ -152,4 +152,4 @@ class UserTest < ActiveSupport::TestCase
:longest_streak_end => Time.zone.local(2010, 6, 1)
assert_equal "/~bob", user.longest_streak_url
end
end
end
16 changes: 15 additions & 1 deletion views/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
#calendar
= seinfeld
#main
#zone
Location:
== "#{@user.location}"
%a{:rel => "me", :href => "http://github.com/#{@user.login}"}
(from profile page)
|
Zone:
== "#{@user.time_zone}"
|
Time left today:
== #{@user.time_left}
#stats
.current_streak
%span.type
Expand All @@ -35,4 +46,7 @@
#content.footnote
Put the
%a{:href=> "/~#{@user.login}/widget"} JavaScript widget
on your site!
on your site!
#content.footnote
Geographical information provided by
%a{:href => 'http://www.geonames.org'} GeoNames

0 comments on commit 3673962

Please sign in to comment.