Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added tests

  • Loading branch information...
commit d3f6508ed3b23013d57ba6221adedbd9c038d9e7 1 parent 3673962
@bhb bhb authored
View
13 db/schema.rb
@@ -1,10 +1,11 @@
-# This file is auto-generated from the current state of the database. Instead of editing this file,
-# please use the migrations feature of Active Record to incrementally modify your database, and
-# then regenerate this schema definition.
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
#
-# Note that this schema.rb definition is the authoritative source for your database schema. If you need
-# to create the application database on another system, you should be using db:schema:load, not running
-# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
View
1  lib/seinfeld/updater.rb
@@ -12,6 +12,7 @@ def initialize(user)
def run(today = Date.today)
today ||= Date.today
+ @user.update_location!
@user.update_timezone!
Time.zone = @user.time_zone || "UTC"
if feed = Feed.fetch(@user)
View
31 lib/seinfeld/user.rb
@@ -156,24 +156,35 @@ def time_left
minutes = (seconds_until_tomorrow/60 - hours * 60).to_i
'%d h, %d min' % [hours, minutes]
end
+
+ attr_accessor :http_conn
+ def http_conn
+ @http_conn ||= begin
+ user_agent = 'Calendar About Nothing: http://github.com/technoweenie/seinfeld'
+ Faraday::Connection.new(:headers => {'User-Agent' => user_agent}) do |b|
+ b.adapter :typhoeus
+ end
+ end
+ end
+
+ def update_location!
+ data = Yajl::Parser.parse(http_conn.get("http://github.com/api/v2/json/user/show/#{login}").body)
+ self.location = data["user"]["location"]
+ save!
+ 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)
+ place_data = Yajl::Parser.parse(http_conn.get("http://ws.geonames.org/searchJSON?maxRows=1&q=#{location}").body)
location_data = place_data["geonames"].first
+ return if location_data.nil?
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)
+ return if lat.nil? || lng.nil?
+ computed_time_zone = Yajl::Parser.parse(http_conn.get("http://ws.geonames.org/timezoneJSON?lat=#{lat}&lng=#{lng}").body)
time_zone_id = computed_time_zone["timezoneId"]
+ return if time_zone_id.nil?
reverse_mapping = ActiveSupport::TimeZone::MAPPING.invert
if reverse_mapping.key?(time_zone_id)
self.time_zone = reverse_mapping[time_zone_id]
View
114 test/user_test.rb
@@ -152,4 +152,118 @@ class UserTest < ActiveSupport::TestCase
:longest_streak_end => Time.zone.local(2010, 6, 1)
assert_equal "/~bob", user.longest_streak_url
end
+
+ test "#update_location! if location is missing" do
+ user = Seinfeld::User.new :login => 'bob'
+ user.http_conn = \
+ Faraday::Connection.new do |builder|
+ builder.adapter :test do |stub|
+ stub.get('/api/v2/json/user/show/bob') do
+ [200, {}, '{"user":{"name":"Bob"}}']
+ end
+ end
+ end
+ user.update_location!
+ assert_equal nil, user.location
+ end
+
+ test "#update_location! if location is present" do
+ user = Seinfeld::User.new :login => 'bob'
+ user.http_conn = \
+ Faraday::Connection.new do |builder|
+ builder.adapter :test do |stub|
+ stub.get('/api/v2/json/user/show/bob') do
+ [200, {}, '{"user":{"name":"Bob","location":"Boulder, CO"}}']
+ end
+ end
+ end
+ user.update_location!
+ assert_equal "Boulder, CO", user.location
+ end
+
+ test "#update_timezone! if location is empty" do
+ user = Seinfeld::User.new :login => 'bob', :location => ''
+ user.update_timezone!
+ assert_equal nil, user.time_zone
+ end
+
+ test "#update_timezone! if no location can be found" do
+ user = Seinfeld::User.new :login => 'bob', :location => 'gibberish'
+ user.http_conn = \
+ Faraday::Connection.new do |builder|
+ builder.adapter :test do |stub|
+ stub.get('/searchJSON?q=gibberish&maxRows=1') do
+ [200, {}, '{"totalResultsCount":0,"geonames":[]}']
+ end
+ end
+ end
+ user.update_timezone!
+ assert_equal nil, user.time_zone
+ end
+
+ test "#update_timezone! if lat/lng are missing" do
+ user = Seinfeld::User.new :login => 'bob', :location => 'gibberish'
+ user.http_conn = \
+ Faraday::Connection.new do |builder|
+ builder.adapter :test do |stub|
+ stub.get('/searchJSON?q=gibberish&maxRows=1') do
+ [200, {}, '{"geonames":[{"countryName":"United States","adminCode1":"CO"}]}']
+ end
+ end
+ end
+ user.update_timezone!
+ assert_equal nil, user.time_zone
+ end
+
+ test "#update_timezone! if timezone_id is missing" do
+ user = Seinfeld::User.new :login => 'bob', :location => 'gibberish'
+ user.http_conn = \
+ Faraday::Connection.new do |builder|
+ builder.adapter :test do |stub|
+ stub.get('/searchJSON?q=gibberish&maxRows=1') do
+ [200, {}, '{"geonames":[{"lng":-105.2705456,"lat":40.0149856}]}']
+ end
+ stub.get('/timezoneJSON?lng=-105.2705456&lat=40.0149856') do
+ [200, {}, '{"rawOffset":0,"dstOffset":0,"gmtOffset":0,"lng":0,"lat":0}']
+ end
+ end
+ end
+ user.update_timezone!
+ assert_equal nil, user.time_zone
+ end
+
+ test "#update_timezone if unrecognized timezone" do
+ user = Seinfeld::User.new :login => 'bob', :location => 'gibberish'
+ user.http_conn = \
+ Faraday::Connection.new do |builder|
+ builder.adapter :test do |stub|
+ stub.get('/searchJSON?q=gibberish&maxRows=1') do
+ [200, {}, '{"geonames":[{"lng":-105.2705456,"lat":40.0149856}]}']
+ end
+ stub.get('/timezoneJSON?lng=-105.2705456&lat=40.0149856') do
+ [200, {}, '{"timezoneId":"Mars/Space"}']
+ end
+ end
+ end
+ user.update_timezone!
+ assert_equal nil, user.time_zone
+ end
+
+ test "#update_timezone! if all data is present" do
+ user = Seinfeld::User.new :login => 'bob', :location => 'gibberish'
+ user.http_conn = \
+ Faraday::Connection.new do |builder|
+ builder.adapter :test do |stub|
+ stub.get('/searchJSON?q=gibberish&maxRows=1') do
+ [200, {}, '{"geonames":[{"lng":-105.2705456,"lat":40.0149856}]}']
+ end
+ stub.get('/timezoneJSON?lng=-105.2705456&lat=40.0149856') do
+ [200, {}, '{"timezoneId":"America/Denver"}']
+ end
+ end
+ end
+ user.update_timezone!
+ assert_equal "Mountain Time (US & Canada)", user.time_zone
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.