Skip to content

Commit

Permalink
Merge pull request #363 from alias1/twitteruseragent-tweaks
Browse files Browse the repository at this point in the history
TwitterUserAgent Tweaks
  • Loading branch information
0xdevalias committed Jun 11, 2014
2 parents 44834cb + 1bf0e3b commit 94a8465
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
54 changes: 40 additions & 14 deletions app/models/agents/twitter_user_agent.rb
Expand Up @@ -17,11 +17,15 @@ class TwitterUserAgent < Agent
You must also provide the `username` of the Twitter user to monitor.
Set `include_retweets` to `false` to not include retweets (default: `true`)
Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
Set `starting_at` to the date/time (eg. `Mon Jun 02 00:38:12 +0000 2014`) you want to start receiving tweets from (default: agent's `created_at`)
MD

event_description <<-MD
Events are the raw JSON provided by the Twitter API. Should look something like:
Events are the raw JSON provided by the [Twitter API](https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline). Should look something like:
{
... every Tweet field, including ...
Expand All @@ -46,38 +50,60 @@ class TwitterUserAgent < Agent

default_schedule "every_1h"

def validate_options
unless options['username'].present? &&
options['expected_update_period_in_days'].present?
errors.add(:base, "username and expected_update_period_in_days are required")
end
end

def working?
event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
end

def default_options
{
'username' => "tectonic",
'expected_update_period_in_days' => "2"
'username' => 'tectonic',
'include_retweets' => 'true',
'expected_update_period_in_days' => '2'
}
end

def validate_options
errors.add(:base, "username is required") unless options['username'].present?
errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?

if options[:include_retweets].present? && !%w[true false].include?(options[:include_retweets])
errors.add(:base, "include_retweets must be a boolean value string (true/false)")
end

if options[:starting_at].present?
Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
end
end

def starting_at
if options[:starting_at].present?
Time.parse(options[:starting_at]) rescue created_at
else
created_at
end
end

def include_retweets?
options[:include_retweets] != "false"
end

def check
since_id = memory['since_id'] || nil
opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
opts = {:count => 200, :include_rts => include_retweets?, :exclude_replies => false, :include_entities => true, :contributor_details => true}
opts.merge! :since_id => since_id unless since_id.nil?

# http://rdoc.info/gems/twitter/Twitter/REST/Timelines#user_timeline-instance_method
tweets = twitter.user_timeline(options['username'], opts)

tweets.each do |tweet|
memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
if tweet.created_at >= starting_at
memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])

create_event :payload => tweet.attrs
create_event :payload => tweet.attrs
end
end

save!
end
end
end
end
17 changes: 15 additions & 2 deletions spec/models/agents/twitter_user_agent_spec.rb
Expand Up @@ -4,10 +4,11 @@
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",
:starting_at => "Jan 01 00:00:01 +0000 2000",
:consumer_key => "---",
:consumer_secret => "---",
:oauth_token => "---",
Expand All @@ -25,4 +26,16 @@
end
end

end
describe "#check with starting_at=future date" do
it "should check for changes starting_at a future date, thus not find any" do
opts = @opts.merge({ :starting_at => "Jan 01 00:00:01 +0000 2999", })

checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
checker.user = users(:bob)
checker.save!

lambda { checker.check }.should change { Event.count }.by(0)
end
end

end

0 comments on commit 94a8465

Please sign in to comment.