Browse Source

create TwitterSearchAgent

Brian Petro 9 years ago
parent
commit
c7c2f72392

+ 97 - 0
app/models/agents/twitter_search_agent.rb

@@ -0,0 +1,97 @@
+module Agents
+  class TwitterSearchAgent < Agent
+    include TwitterConcern
+
+    cannot_receive_events!
+
+    description <<-MD
+      The Twitter Search Agent emits the results of a specified search.
+
+      #{twitter_dependencies_missing if dependencies_missing?}
+
+      To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.
+
+      You must provide the desired `search`.
+      
+      Set `result_type` (String) — Specifies what type of search results you would prefer to receive. Options are "mixed", "recent", and "popular". (default: `mixed`)
+
+      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](https://dev.twitter.com/rest/reference/get/search/tweets). Should look something like:
+
+          {
+             ... every Tweet field, including ...
+            "text": "something",
+            "user": {
+              "name": "Mr. Someone",
+              "screen_name": "Someone",
+              "location": "Vancouver BC Canada",
+              "description":  "...",
+              "followers_count": 486,
+              "friends_count": 1983,
+              "created_at": "Mon Aug 29 23:38:14 +0000 2011",
+              "time_zone": "Pacific Time (US & Canada)",
+              "statuses_count": 3807,
+              "lang": "en"
+            },
+            "retweet_count": 0,
+            "entities": ...
+            "lang": "en"
+          }
+    MD
+
+    default_schedule "every_1h"
+
+    def working?
+      event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
+    end
+
+    def default_options
+      {
+        'search' => 'freebandnames',
+        'expected_update_period_in_days' => '2'
+      }
+    end
+
+    def validate_options
+      errors.add(:base, "search is required") unless options['search'].present?
+      errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
+
+      if options[:starting_at].present?
+        Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
+      end
+    end
+
+    def starting_at
+      if interpolated[:starting_at].present?
+        Time.parse(interpolated[:starting_at]) rescue created_at
+      else
+        created_at
+      end
+    end
+
+    def check
+      since_id = memory['since_id'] || nil
+      opts = {:include_entities => true}
+      opts.merge! result_type: interpolated[:result_type] if interpolated[:result_type].present?
+      opts.merge! :since_id => since_id unless since_id.nil?
+
+      # http://www.rubydoc.info/gems/twitter/Twitter/REST/Search
+      tweets = twitter.search(interpolated['search'], opts).take(100)
+
+      tweets.each do |tweet|
+        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
+        end
+      end
+
+      save!
+    end
+  end
+end

File diff suppressed because it is too large
+ 0 - 0
spec/data_fixtures/search_tweets.json


+ 43 - 0
spec/models/agents/twitter_search_agent_spec.rb

@@ -0,0 +1,43 @@
+require 'spec_helper'
+
+describe Agents::TwitterSearchAgent do
+  before do
+    # intercept the twitter API request
+    stub_request(:any, /freebandnames/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/search_tweets.json")), :status => 200)
+
+    @opts = {
+      :search => "freebandnames",
+      :expected_update_period_in_days => "2",
+      :starting_at => "Jan 01 00:00:01 +0000 2000",
+      :consumer_key => "---",
+      :consumer_secret => "---",
+      :oauth_token => "---",
+      :oauth_token_secret => "---"
+    }
+
+    @checker = Agents::TwitterSearchAgent.new(:name => "search freebandnames", :options => @opts)
+    @checker.service = services(:generic)
+    @checker.user = users(:bob)
+    @checker.save!
+  end
+
+  describe "#check" do
+    it "should check for changes" do
+      expect { @checker.check }.to change { Event.count }.by(100)
+    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::TwitterSearchAgent.new(:name => "searching freebandnames", :options => opts)
+      checker.service = services(:generic)
+      checker.user = users(:bob)
+      checker.save!
+
+      expect { checker.check }.to change { Event.count }.by(0)
+    end
+  end
+
+end

Some files were not shown because too many files changed in this diff