Merge branch 'master' into scenarios

This commit is contained in:
Andrew Cantino 2014-06-13 23:18:42 -07:00
commit cca78da090
2 changed files with 55 additions and 16 deletions

View file

@ -17,11 +17,15 @@ module Agents
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 ...
@ -46,38 +50,60 @@ module Agents
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

View file

@ -4,10 +4,11 @@ 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",
:starting_at => "Jan 01 00:00:01 +0000 2000",
:consumer_key => "---",
:consumer_secret => "---",
:oauth_token => "---",
@ -25,4 +26,16 @@ describe Agents::TwitterUserAgent do
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