Skip to content
Browse files

Add country_code to TwitterAccount.

  • Loading branch information...
1 parent d1ff23e commit 56b18700e4f6df9ce01e8e213f18b5f72cfd4850 @ordinaryzelig committed Mar 18, 2012
View
5 db/migrations/17_add_country_code_to_twitter_accounts.rb
@@ -0,0 +1,5 @@
+class AddCountryCodeToTwitterAccounts < ActiveRecord::Migration
+ def change
+ add_column :twitter_accounts, :country_code, :string
+ end
+end
View
9 models/social/twitter_account.rb
@@ -8,7 +8,7 @@ class TwitterAccount < ActiveRecord::Base
validates :screen_name, presence: true, uniqueness: true
validates :followed, inclusion: {in: [true, false]}
- after_validation :geocode, if: :postal_code_changed?
+ after_validation :geocode_with_country_code, if: :postal_code_changed?
scope :followed, proc { |bool| where(followed: bool) }
scope :not_promted_for_postal_code, where(prompted_for_postal_code_at: nil)
@@ -158,6 +158,13 @@ def theaters_not_tracking_for_movie(movie)
ticket_notifications.for(movie).includes(:theater).map(&:theater)
end
+ def geocode_with_country_code
+ geocoder_result = Geocoder.search(self.postal_code).first
+ self.latitude = geocoder_result.latitude
+ self.longitude = geocoder_result.longitude
+ self.country_code = geocoder_result.country_code
+ end
+
private
# Send a DM with message. Return true if successful.
View
9 spec/models/social/twitter_account.spec.rb
@@ -175,4 +175,13 @@
account.theaters_not_tracking_for_movie(ticket_notification.movie).must_equal [ticket_notification.theater]
end
+ it '#geocode_with_country_code assigns coordinates and country' do
+ VCR.use_cassette 'geocoder/73142' do
+ account = FactoryGirl.build(:redningja, postal_code: 73142, country_code: nil, latitude: nil, longitude: nil)
+ account.geocode_with_country_code
+ account.coordinates.must_equal [35.6131551, -97.6385368]
+ account.country_code.must_equal 'US'
+ end
+ end
+
end
View
13 spec/support/factories/twitter_accounts.rb
@@ -3,12 +3,13 @@
factory :twitter_account do
factory :redningja do
- screen_name 'redningja'
- user_id 29482029
- location 'oklahoma city'
- latitude 35.6131551
- longitude -97.6385368
- followed true
+ screen_name 'redningja'
+ user_id 29482029
+ location 'oklahoma city'
+ latitude 35.6131551
+ longitude -97.6385368
+ country_code 'US'
+ followed true
end
end
View
14 spec/support/macros/stubbing.rb
@@ -21,15 +21,13 @@ def stub_geocoder_with_counter
Geocoder.stubs(:coordinates).returns(*100.times.map { |i| [i, i] })
end
- # Stub Geocoder gem to return 0, 0 as coordinates.
+ # Stub Geocoder gem to return 0, 0 as coordinates and 'US' as country_code.
def disable_geocoding
- Geocoder.stubs(:coordinates).returns([0, 0])
- # Stub geocode on models that call geocoded_by.
- ActiveRecord::Base.descendants.each do |model|
- if model._validation_callbacks.detect { |vc| vc.filter == :geocode }
- model.any_instance.stubs(:geocode).returns([0, 0])
- end
- end
+ require 'geocoder/results/base'
+ stubbed_result = Geocoder::Result::Base.new({})
+ stubbed_result.stubs(:coordinates).returns([0.0, 0.0])
+ stubbed_result.stubs(:country_code).returns('US')
+ Geocoder.stubs(:search).returns([stubbed_result])
end
end
View
51 spec/support/vcr_cassettes/geocoder/73142.yml
@@ -0,0 +1,51 @@
+---
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :get
+ uri: http://maps.googleapis.com:80/maps/api/geocode/json?address=73142&language=en&sensor=false
+ body: !!null
+ headers:
+ accept-encoding:
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ content-type:
+ - application/json; charset=UTF-8
+ date:
+ - Sun, 18 Mar 2012 19:09:27 GMT
+ expires:
+ - Mon, 19 Mar 2012 19:09:27 GMT
+ cache-control:
+ - public, max-age=86400
+ server:
+ - mafe
+ x-xss-protection:
+ - 1; mode=block
+ x-frame-options:
+ - SAMEORIGIN
+ body: ! "{\n \"results\" : [\n {\n \"address_components\" : [\n
+ \ {\n \"long_name\" : \"73142\",\n \"short_name\"
+ : \"73142\",\n \"types\" : [ \"postal_code\" ]\n },\n
+ \ {\n \"long_name\" : \"Oklahoma City\",\n \"short_name\"
+ : \"Oklahoma City\",\n \"types\" : [ \"locality\", \"political\"
+ ]\n },\n {\n \"long_name\" : \"Oklahoma\",\n
+ \ \"short_name\" : \"OK\",\n \"types\" : [ \"administrative_area_level_1\",
+ \"political\" ]\n },\n {\n \"long_name\"
+ : \"United States\",\n \"short_name\" : \"US\",\n \"types\"
+ : [ \"country\", \"political\" ]\n }\n ],\n \"formatted_address\"
+ : \"Oklahoma City, OK 73142, USA\",\n \"geometry\" : {\n \"bounds\"
+ : {\n \"northeast\" : {\n \"lat\" : 35.6397830,\n
+ \ \"lng\" : -97.59998490\n },\n \"southwest\"
+ : {\n \"lat\" : 35.5899320,\n \"lng\" : -97.67468090000001\n
+ \ }\n },\n \"location\" : {\n \"lat\"
+ : 35.61315510,\n \"lng\" : -97.63853680\n },\n \"location_type\"
+ : \"APPROXIMATE\",\n \"viewport\" : {\n \"northeast\"
+ : {\n \"lat\" : 35.6397830,\n \"lng\" : -97.59998490\n
+ \ },\n \"southwest\" : {\n \"lat\"
+ : 35.5899320,\n \"lng\" : -97.67468090000001\n }\n
+ \ }\n },\n \"types\" : [ \"postal_code\" ]\n }\n
+ \ ],\n \"status\" : \"OK\"\n}\n"
+ http_version: '1.1'

0 comments on commit 56b1870

Please sign in to comment.
Something went wrong with that request. Please try again.