From a6167d5803e8b4c6b3245c8e294583fb8f149837 Mon Sep 17 00:00:00 2001 From: Albert Sun Date: Mon, 8 Apr 2013 12:10:42 -0400 Subject: [PATCH] added one spec for twitter user agent --- app/models/agents/twitter_user_agent.rb | 22 --------------- spec/data_fixtures/user_tweets.json | 1 + spec/models/agents/twitter_user_agent_spec.rb | 28 +++++++++++++++++++ 3 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 spec/data_fixtures/user_tweets.json create mode 100644 spec/models/agents/twitter_user_agent_spec.rb diff --git a/app/models/agents/twitter_user_agent.rb b/app/models/agents/twitter_user_agent.rb index 8743e992..215659c3 100644 --- a/app/models/agents/twitter_user_agent.rb +++ b/app/models/agents/twitter_user_agent.rb @@ -129,19 +129,6 @@ module Agents } end - # def process_tweet(filter, status) - # if options[:generate] == "counts" - # # Avoid memory pollution - # me = Agent.find(id) - # me.memory[:filter_counts] ||= {} - # me.memory[:filter_counts][filter.to_sym] ||= 0 - # me.memory[:filter_counts][filter.to_sym] += 1 - # me.save! - # else - # create_event :payload => status.merge(:filter => filter.to_s) - # end - # end - def check Twitter.configure do |config| config.consumer_key = options[:consumer_key] @@ -151,10 +138,8 @@ module Agents end since_id = memory[:since_id] || nil - max_id = memory[:max_id] || nil opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true} opts.merge! :since_id => since_id unless since_id.nil? - opts.merge! :max_id => max_id unless max_id.nil? tweets = Twitter.user_timeline(options[:username], opts) @@ -165,13 +150,6 @@ module Agents end save! - # if memory[:filter_counts] && memory[:filter_counts].length > 0 - # memory[:filter_counts].each do |filter, count| - # create_event :payload => { :filter => filter.to_s, :count => count, :time => Time.now.to_i } - # end - # memory[:filter_counts] = {} - # save! - # end end end end \ No newline at end of file diff --git a/spec/data_fixtures/user_tweets.json b/spec/data_fixtures/user_tweets.json new file mode 100644 index 00000000..53233484 --- /dev/null +++ b/spec/data_fixtures/user_tweets.json @@ -0,0 +1 @@ +[{"created_at":"Sun Apr 07 20:34:17 +0000 2013","id":320997982096990208,"id_str":"320997982096990208","text":"@christophe971 Cool project!","source":"web","truncated":false,"in_reply_to_status_id":320990180205002752,"in_reply_to_status_id_str":"320990180205002752","in_reply_to_user_id":12574212,"in_reply_to_user_id_str":"12574212","in_reply_to_screen_name":"christophe971","user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"christophe971","name":"Christophe Maximin","id":12574212,"id_str":"12574212","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"en"},{"created_at":"Sun Apr 07 20:34:06 +0000 2013","id":320997936773349378,"id_str":"320997936773349378","text":"@christophe971 Fair enough. I've previously worked on something very similar and wondered if you're looking for collaborators.","source":"web","truncated":false,"in_reply_to_status_id":320990180205002752,"in_reply_to_status_id_str":"320990180205002752","in_reply_to_user_id":12574212,"in_reply_to_user_id_str":"12574212","in_reply_to_screen_name":"christophe971","user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"christophe971","name":"Christophe Maximin","id":12574212,"id_str":"12574212","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"en"},{"created_at":"Sun Apr 07 19:59:21 +0000 2013","id":320989192488382464,"id_str":"320989192488382464","text":"@christophe971 Is denisthebot open source?","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":12574212,"in_reply_to_user_id_str":"12574212","in_reply_to_screen_name":"christophe971","user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"christophe971","name":"Christophe Maximin","id":12574212,"id_str":"12574212","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"nl"},{"created_at":"Sun Apr 07 00:18:42 +0000 2013","id":320692069385527296,"id_str":"320692069385527296","text":"Netflix won’t allow a password longer than 10 characters. Fail.","source":"Tweetbot for Mac","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":{"id":"8d2591b0d50daf32","url":"https://api.twitter.com/1.1/geo/id/8d2591b0d50daf32.json","place_type":"neighborhood","name":"Lower Haight","full_name":"Lower Haight, San Francisco","country_code":"US","country":"United States","polylines":[],"bounding_box":{"type":"Polygon","coordinates":[[[-122.437382,37.769551],[-122.421482,37.769551],[-122.421482,37.77602],[-122.437382,37.77602]]]},"attributes":{}},"contributors":null,"retweet_count":2,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"en"},{"created_at":"Sat Apr 06 18:42:44 +0000 2013","id":320607521532432384,"id_str":"320607521532432384","text":"@ryanmaxwell @draconisnz let me know if you use it, or have suggestions!","source":"Twitter for iPhone","truncated":false,"in_reply_to_status_id":320454615885807616,"in_reply_to_status_id_str":"320454615885807616","in_reply_to_user_id":14658141,"in_reply_to_user_id_str":"14658141","in_reply_to_screen_name":"ryanmaxwell","user":{"id":9813372,"id_str":"9813372","name":"Andrew Cantino","screen_name":"tectonic","location":"San Francisco, CA","description":"Experimentalist, web developer, and VP of Engineering at @Mavenlink.","url":"http://andrewcantino.com","entities":{"url":{"urls":[{"url":"http://andrewcantino.com","expanded_url":null,"indices":[0,24]}]},"description":{"urls":[]}},"protected":false,"followers_count":999,"friends_count":492,"listed_count":39,"created_at":"Wed Oct 31 03:16:39 +0000 2007","favourites_count":132,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":3433,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http://a0.twimg.com/images/themes/theme5/bg.gif","profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme5/bg.gif","profile_background_tile":false,"profile_image_url":"http://a0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_image_url_https":"https://si0.twimg.com/profile_images/1694984565/me-right_normal.jpg","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"ryanmaxwell","name":"Ryan Maxwell","id":14658141,"id_str":"14658141","indices":[0,12]},{"screen_name":"draconisNZ","name":"Dan","id":14979092,"id_str":"14979092","indices":[13,24]}]},"favorited":false,"retweeted":false,"lang":"en"}] \ No newline at end of file diff --git a/spec/models/agents/twitter_user_agent_spec.rb b/spec/models/agents/twitter_user_agent_spec.rb new file mode 100644 index 00000000..8739e853 --- /dev/null +++ b/spec/models/agents/twitter_user_agent_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe Agents::TwitterUserAgent do + before do + # intercept the twitter API request for @tectonic's user profile + stub_request(:any, /tectonic/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200) + + @opts = { + :username => "tectonic", + :expected_update_period_in_days => "2", + :consumer_key => "---", + :consumer_secret => "---", + :oauth_token => "---", + :oauth_token_secret => "---" + } + + @checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => @opts) + @checker.user = users(:bob) + @checker.save! + end + + describe "#check" do + it "should check for changes" do + lambda { @checker.check }.should change { Event.count }.by(5) + end + end + +end \ No newline at end of file