Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Send message when country code unsupported.

  • Loading branch information...
commit e30130f3e8a6004f36e8c7787be6fc2cc7c9d2e5 1 parent 0ed8fe4
@ordinaryzelig authored
View
9 models/social/tweets/unsupported_country_tweet.rb
@@ -0,0 +1,9 @@
+class UnsupportedCountryTweet < TweetString
+
+ def initialize(country_code)
+ super <<-END
+Sorry, but I am not yet tracking #{country_code}. Only US and Canada currently. UK is on the way. Stay tuned.
+END
+ end
+
+end
View
22 models/social/twitter_account.rb
@@ -7,7 +7,6 @@ class TwitterAccount < ActiveRecord::Base
validates :user_id, presence: true, uniqueness: true
validates :screen_name, presence: true, uniqueness: true
validates :followed, inclusion: {in: [true, false]}
- validates :country_code, inclusion: {in: ['US', 'CA', 'UK'], allow_blank: true}
after_validation :geocode_with_country_code, if: :postal_code_changed?
@@ -50,13 +49,7 @@ def prompt_for_postal_codes
end
# List DMs, process each for postal_code.
- # If postal_code successfully extracted, assign to twitter account if different.
# If postal_code cannot be processed, deny_postal_code.
- # If postal_code extracted
- # If theaters found, DM confirmation.
- # If no theaters found, DM no theaters found.
- # Make sure to only find/assign theaters if postal_code is different
- # so we don't sound like a broken record.
def process_DMs_for_postal_codes
Twitter.direct_messages.each do |dm|
postal_code = dm.extract_postal_code
@@ -146,13 +139,22 @@ def deny_postal_code
dm! InvalidatePostalCodeTweet.new
end
+ def send_unsupported_country_message
+ dm! UnsupportedCountryTweet.new(self.country_code)
+ end
+
# Set postal_code. if different than before, find theaters and confirm/deny location.
# Wrap argument in PostalCode object and convert to string to ensure whitespaces removed.
+ # Make sure to only find/assign theaters if postal_code is different so we don't sound like a broken record.
def process_postal_code(postal_code)
self.postal_code = PostalCode.new(postal_code.to_s).to_s
if postal_code_changed?
save!
- find_theaters_and_confirm_or_deny_location
+ if in_supported_country?
+ find_theaters_and_confirm_or_deny_location
+ else
+ send_unsupported_country_message
+ end
end
end
@@ -167,6 +169,10 @@ def geocode_with_country_code
self.country_code = geocoder_result.country_code
end
+ def in_supported_country?
+ TicketSources.support_country_code?(self.country_code)
+ end
+
private
# Send a DM with message. Return true if successful.
View
2  models/sources/cineworld.rb
@@ -2,7 +2,7 @@ module CineWorld
extend TicketSources::CountryMethods
- serves_country_codes 'UK'
+ supports_country_codes # UK, eventually.
class << self
View
2  models/sources/fandango.rb
@@ -2,7 +2,7 @@ module Fandango
extend TicketSources::CountryMethods
- serves_country_codes 'US', 'CA'
+ supports_country_codes 'US', 'CA'
class << self
View
2  models/sources/movie_tickets.rb
@@ -2,7 +2,7 @@ module MovieTickets
extend TicketSources::CountryMethods
- serves_country_codes 'US', 'CA'
+ supports_country_codes 'US', 'CA'
class << self
View
16 models/ticket_sources.rb
@@ -16,6 +16,14 @@ def all
]
end
+ def support_country_code?(country_code)
+ country_codes.include?(country_code)
+ end
+
+ def country_codes
+ @country_codes ||= all.map(&:country_codes).flatten.uniq
+ end
+
end
# Kind of like ActiveRecord::Scope.
@@ -28,7 +36,7 @@ def initialize(country_code)
# Return list of ticket sources scoped to country.
def all
- @all ||= TicketSources.all.select { |ticket_source| ticket_source.serves_country_code?(@country_code) }
+ @all ||= TicketSources.all.select { |ticket_source| ticket_source.supports_country_code?(@country_code) }
end
def find_theaters_near(postal_code)
@@ -47,12 +55,12 @@ def find_theaters_selling_at(movie, postal_code)
module CountryMethods
- def serves_country_code?(country_code)
+ def supports_country_code?(country_code)
@country_codes.include?(country_code)
end
- # Define list of country codes this ticket source serves.
- def serves_country_codes(*country_codes)
+ # Define list of country codes this ticket source supports.
+ def supports_country_codes(*country_codes)
@country_codes = country_codes
end
View
13 spec/integration/twitter.spec.rb
@@ -70,6 +70,19 @@
end
end
+ scenario 'user replies with DM with postal code for country not yet supported' do
+ it 'Twitter client DMs user saying country is not yet supported' do
+ VCR.use_cassette 'twitter/list_DMs_with_postal_code_in_unsupported_country' do
+ country_code = 'XY'
+ stub_geocoder 0.0, 0.0, country_code
+ account = FactoryGirl.create(:redningja, postal_code: nil)
+ UnsupportedCountryTweet.expects(:new).with(country_code)
+ TwitterAccount.any_instance.expects(:dm!)
+ TwitterAccount.process_DMs_for_postal_codes
+ end
+ end
+ end
+
end
scenario 'when tickets found at theater' do
View
9 spec/models/social/tweets/unsupported_country_tweet.spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+describe UnsupportedCountryTweet do
+
+ it 'has a valid TweetString' do
+ UnsupportedCountryTweet.new('UK').validate!
+ end
+
+end
View
14 spec/models/social/twitter_account.spec.rb
@@ -168,6 +168,13 @@
account.process_postal_code(account.postal_code)
end
+ it 'sends DM if country code is not supported' do
+ stub_geocoder 0.0, 0.0, 'XY'
+ account = FactoryGirl.build(:redningja, postal_code: nil)
+ account.expects(:send_unsupported_country_message)
+ account.process_postal_code('123')
+ end
+
end
it '#theaters_not_tracking_for_movie returns theaters that account has already been notified about' do
@@ -185,4 +192,11 @@
end
end
+ it '#send_unsupported_country_message DMs twitter account saying country code is not supported' do
+ account = FactoryGirl.build(:redningja)
+ UnsupportedCountryTweet.expects(:new).with(account.country_code)
+ account.expects(:dm!)
+ account.send_unsupported_country_message
+ end
+
end
View
8 spec/models/ticket_sources.spec.rb
@@ -34,4 +34,12 @@
ticket_sources.must_equal [Fandango, MovieTickets]
end
+ it 'supports certain countries' do
+ TicketSources.country_codes.must_equal ['US', 'CA']
+ end
+
+ it 'does not support certain countries' do
+ TicketSources.support_country_code?('UK').must_equal false
+ end
+
end
View
8 spec/support/macros/stubbing.rb
@@ -23,10 +23,14 @@ def stub_geocoder_with_counter
# Stub Geocoder gem to return 0, 0 as coordinates and 'US' as country_code.
def disable_geocoding
+ stub_geocoder 0.0, 0.0, 'US'
+ end
+
+ def stub_geocoder(latitude, longitude, country_code)
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')
+ stubbed_result.stubs(:coordinates).returns([latitude, longitude])
+ stubbed_result.stubs(:country_code).returns(country_code)
Geocoder.stubs(:search).returns([stubbed_result])
end
View
7 spec/support/shared_behaviors/must_behave_like_ticket_source.rb
@@ -12,8 +12,11 @@ def it_must_behave_like_ticket_source
ticket_source.must_respond_to(:diagnostics)
end
- it 'serves countries' do
- ticket_source.country_codes.wont_be_empty
+ it 'supports countries' do
+ ticket_source.country_codes.must_be_kind_of Array
+ ticket_source.country_codes.each do |country_code|
+ ticket_source.supports_country_code?(country_code).must_equal true
+ end
end
end
View
179 spec/support/vcr_cassettes/twitter/list_DMs_with_postal_code_in_unsupported_country.yml
@@ -0,0 +1,179 @@
+---
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :get
+ uri: https://api.twitter.com:443/1/direct_messages.json
+ body: !!null
+ headers:
+ accept:
+ - application/json
+ user-agent:
+ - Twitter Ruby Gem 2.0.2
+ authorization:
+ - OAuth oauth_consumer_key="<FILTERED>", oauth_nonce="2d2863e44f4813ea0c2080e01c2710a7",
+ oauth_signature="DN9gEMACjMLvRHR1LF7GX8M1Qwo%3D", oauth_signature_method="HMAC-SHA1",
+ oauth_timestamp="1332176067", oauth_token="<FILTERED>", oauth_version="1.0"
+ 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:
+ date:
+ - Mon, 19 Mar 2012 16:54:28 GMT
+ status:
+ - 200 OK
+ x-runtime:
+ - '0.08862'
+ pragma:
+ - no-cache
+ content-type:
+ - application/json; charset=utf-8
+ x-mid:
+ - 043e56168b936bdba32a729035d2af6a10f5e152
+ expires:
+ - Tue, 31 Mar 1981 05:00:00 GMT
+ x-access-level:
+ - read-write-directmessages
+ x-transaction:
+ - 53bcd15864dcf734
+ etag:
+ - ! '"6c7556b27c100d9ab031a7daf1be604c"'
+ x-ratelimit-limit:
+ - '350'
+ x-ratelimit-remaining:
+ - '342'
+ cache-control:
+ - no-cache, no-store, must-revalidate, pre-check=0, post-check=0
+ x-frame-options:
+ - SAMEORIGIN
+ x-ratelimit-class:
+ - api_identified
+ x-transaction-mask:
+ - a6183ffa5f8ca943ff1b53b5644ef114fd7b66ab
+ last-modified:
+ - Mon, 19 Mar 2012 16:54:28 GMT
+ x-ratelimit-reset:
+ - '1332179608'
+ set-cookie:
+ - k=10.34.123.126.1332176068341045; path=/; expires=Mon, 26-Mar-12 16:54:28
+ GMT; domain=.twitter.com
+ - guest_id=v1%3A133217606834519607; domain=.twitter.com; path=/; expires=Thu,
+ 20-Mar-2014 04:54:28 GMT
+ - dnt=; domain=.twitter.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
+ - lang=en; path=/
+ - twid=u%3D468884514%7CevvCPPBMDJKVpC5IrfSi0i%2FMAd4%3D; domain=.twitter.com;
+ path=/; secure
+ - _twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoMY3NyZl9pZCIlY2I2ZjY5YzU0N2RmYTViYTc4%250AYWVhMmI4ZTNjYTI5ZTE6D2NyZWF0ZWRfYXRsKwgMn%252BErNgE6B2lkIiVkZjVm%250AYzNlMjRmZmQ1NDU3YjY0OTg4MzNhYjM0YmMyZA%253D%253D--f8bfffc3a46cbd60978044b6022b64b98dedbee0;
+ domain=.twitter.com; path=/; HttpOnly
+ vary:
+ - Accept-Encoding
+ content-encoding:
+ - gzip
+ content-length:
+ - '835'
+ server:
+ - tfe
+ body: !binary |-
+ H4sIAAAAAAAAA7WWwXLTMBCGX0WjAwNDaGQ7cWzfIKEHGC5QToTRKLaSqJWl
+ IMkt0OHdWbmxY8eBFgq62N7Rrnb/XX3jT7fYclVwQ0WBszCdJCEJ09HeiLNb
+ 3LfnhjPHC8oczvBFxdHLnUFkhoI4C+MsCtFzAguFhKQYojjmKsstzXWlwCOI
+ o2CEpbA+RGODbRCVK6pYySGq4YUSanPJfICtvqFMSiqUFIrTkheC4WzNpOUj
+ vDN6LSSnVhR8xQxdaeMrybXUkDo+rxdEWRsB5bRZTKCOyuVUr9eWw/eLMIgJ
+ gW1aSn0DR+PMmaoTf8Xyq40B54I6+B6cL0q24bQykm6d21k4un5my/FybAU5
+ czei3JzlulyOey52OQ7CWUCSWThbjpl0HGqnSpuSybMd5DEsET7kocDZ4mUa
+ wa69cpcM/JEXD2xS58wJrcCuryTb6pKhXLhvuCmUGv6l4tZR6LVrayr4mlXS
+ 0V6i3Yodz6F7rUVY6gxTVjLnc9pb1+xaV0a4TutPytlUQuqF/fEwDGK3Txyf
+ dGrl3gtd68xOynzs5RWPZtMwTadkOV5tzi53XZVhxq7anObzKKpz+l0KD+/4
+ A1NR2om1uGud/fWk3V/6QyfsqOGHFtZDws3h2oQj7ETJ6Xet/LTNYWwMk+gC
+ bOjpxw/oCZozxQr2zE8f89cIcwXvuYaNYlXBgFjKFVvJzvw06QImBhI191AU
+ 1DrfkoZDEHTD9SFW/7o6/tUNJ+uaGxC2c3Ktoqqk/DEC5uRiB5CAy9Bj0cXi
+ 7fuclTtdOVrwa9zd6cE4iZMkmUyDSSfJIAlmyTSOEzKNozRNZlHXrUFqx7PH
+ 1HMj0BumgKCIkGw6yYK0ZWoQnmLqMVDJMVCHRTwKq3OyeP16McRq0KeqV7bH
+ 1KPo/xCq1on86ttyfJJddmgnf8DYBRR7Hh8Y68WcozsZO4j9O6q2c/uXUCW/
+ hWrbqH8E1UZOt+Vl+wxqdvWF7GGUkGTyavJojN57+KPB+cgpGnD0rrkDjAY9
+ it7dkv9DyxYxx7g8CtbjZVSv+3jZQ9Y7rdA7ZhCQCn4DPbKmfWR53g2p2Pxk
+ 0l///vnMwPQqilDitvjH559c11CPrwoAAA==
+ http_version: '1.1'
+- !ruby/struct:VCR::HTTPInteraction
+ request: !ruby/struct:VCR::Request
+ method: :delete
+ uri: https://api.twitter.com:443/1/direct_messages/destroy/181785668056399873.json
+ body: !!null
+ headers:
+ accept:
+ - application/json
+ user-agent:
+ - Twitter Ruby Gem 2.0.2
+ authorization:
+ - OAuth oauth_consumer_key="<FILTERED>", oauth_nonce="8c0826475896327ddd672259ec493a39",
+ oauth_signature="tfxyYXZC%2B98gzvqDvahqUF2sOD0%3D", oauth_signature_method="HMAC-SHA1",
+ oauth_timestamp="1332176068", oauth_token="<FILTERED>", oauth_version="1.0"
+ response: !ruby/struct:VCR::Response
+ status: !ruby/struct:VCR::ResponseStatus
+ code: 200
+ message: OK
+ headers:
+ date:
+ - Mon, 19 Mar 2012 16:54:31 GMT
+ status:
+ - 200 OK
+ cache-control:
+ - no-cache, no-store, must-revalidate, pre-check=0, post-check=0
+ expires:
+ - Tue, 31 Mar 1981 05:00:00 GMT
+ x-ratelimit-reset:
+ - '1332179608'
+ content-type:
+ - application/json; charset=utf-8
+ content-length:
+ - '2733'
+ x-frame-options:
+ - SAMEORIGIN
+ x-ratelimit-class:
+ - api_identified
+ pragma:
+ - no-cache
+ last-modified:
+ - Mon, 19 Mar 2012 16:54:31 GMT
+ etag:
+ - ! '"e5f93e77a266db4976889816d3e6bff2"'
+ x-transaction-mask:
+ - a6183ffa5f8ca943ff1b53b5644ef114fd7b66ab
+ x-access-level:
+ - read-write-directmessages
+ x-ratelimit-limit:
+ - '350'
+ x-runtime:
+ - '0.06774'
+ x-mid:
+ - fb2bbc6698f38a5e1acd88330ff04138678ce1e2
+ x-transaction:
+ - 66a9a5b4f7df3b0e
+ x-ratelimit-remaining:
+ - '341'
+ set-cookie:
+ - k=10.35.22.108.1332176071128990; path=/; expires=Mon, 26-Mar-12 16:54:31 GMT;
+ domain=.twitter.com
+ - guest_id=v1%3A133217607113237702; domain=.twitter.com; path=/; expires=Thu,
+ 20-Mar-2014 04:54:31 GMT
+ - dnt=; domain=.twitter.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
+ - lang=en; path=/
+ - twid=u%3D468884514%7CevvCPPBMDJKVpC5IrfSi0i%2FMAd4%3D; domain=.twitter.com;
+ path=/; secure
+ - _twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoMY3NyZl9pZCIlNTAzNzQxNWViODg1NDJjODIx%250ANTZmNTA2MmM2ZDM3NDQ6D2NyZWF0ZWRfYXRsKwjuqeErNgE6B2lkIiU0NTI3%250AZjgzMDFkOTYxNjYxNTQwM2EwZGFjY2Q4OTE2OQ%253D%253D--42087304f1a1632843cb4a14f5bbf1734886bcbe;
+ domain=.twitter.com; path=/; HttpOnly
+ vary:
+ - Accept-Encoding
+ server:
+ - tfe
+ body: ! '{"sender_id":29482029,"sender":{"id":29482029,"created_at":"Tue Apr 07
+ 16:26:32 +0000 2009","statuses_count":1631,"listed_count":1,"screen_name":"redningja","show_all_inline_media":false,"profile_sidebar_border_color":"FFFFFF","friends_count":49,"utc_offset":-21600,"following":true,"profile_background_tile":false,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/127108727\/altered_normal.png","profile_sidebar_fill_color":"F7DA93","name":"jared
+ ning","location":"oklahoma city","follow_request_sent":false,"default_profile_image":false,"protected":false,"is_translator":false,"favourites_count":1,"profile_background_color":"000000","description":"","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/137529950\/bg.jpg","profile_link_color":"CC3300","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/137529950\/bg.jpg","notifications":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/127108727\/altered_normal.png","default_profile":false,"followers_count":42,"time_zone":"Central
+ Time (US & Canada)","lang":"en","contributors_enabled":false,"profile_use_background_image":true,"id_str":"29482029","geo_enabled":true,"profile_text_color":"000000","verified":false,"url":null},"recipient_screen_name":"TDKRcampout_dev","recipient_id":468884514,"id_str":"181785668056399873","recipient":{"id":468884514,"created_at":"Fri
+ Jan 20 00:54:19 +0000 2012","statuses_count":1,"listed_count":0,"screen_name":"TDKRcampout_dev","show_all_inline_media":false,"profile_sidebar_border_color":"C0DEED","friends_count":1,"utc_offset":null,"following":false,"profile_background_tile":false,"profile_image_url_https":"https:\/\/si0.twimg.com\/sticky\/default_profile_images\/default_profile_0_normal.png","profile_sidebar_fill_color":"DDEEF6","name":"TDKRC
+ dev","location":"","follow_request_sent":false,"default_profile_image":true,"protected":false,"is_translator":false,"favourites_count":0,"profile_background_color":"C0DEED","description":"","profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png","profile_link_color":"0084B4","profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png","notifications":false,"profile_image_url":"http:\/\/a0.twimg.com\/sticky\/default_profile_images\/default_profile_0_normal.png","default_profile":true,"followers_count":1,"time_zone":null,"lang":"en","contributors_enabled":false,"profile_use_background_image":true,"id_str":"468884514","geo_enabled":false,"profile_text_color":"333333","verified":false,"url":null},"created_at":"Mon
+ Mar 19 16:54:15 +0000 2012","id":181785668056399873,"sender_screen_name":"redningja","text":"B33
+ 8th"}'
+ http_version: '1.1'
Please sign in to comment.
Something went wrong with that request. Please try again.