Class: Tbot::Bot
- Inherits:
-
Object
- Object
- Tbot::Bot
- Defined in:
- lib/tbot/reply.rb,
lib/tbot/bot.rb,
lib/tbot/config.rb,
lib/tbot/follow.rb,
lib/tbot/search.rb
Overview
module Search
Constant Summary collapse
- API_CREDENTIALS =
{ :consumer_key => "", :consumer_secret => "", :oauth_token => "", :oauth_token_secret => "" }
- @@client =
nil
Class Method Summary collapse
Instance Method Summary collapse
- #follow(user) ⇒ Object
-
#format_opts(opts) ⇒ Object
DRY: Ensure correct data is passed to search, set instance variables as required.
-
#initialize ⇒ Bot
constructor
Instance methods.
-
#recurring_search(q, opts = {}) ⇒ Object
Works as search but sets up a thread and calls itself infinitely.
- #reply(tweet, response = "") ⇒ Object
-
#search(q, opts = {}) {|results| ... } ⇒ Object
Clean up options, pass to Twitter search function and yield results for block handling.
-
#stop_search! ⇒ Object
> kill thread loop if it exists.
Constructor Details
#initialize ⇒ Bot
Instance methods.
21 22 23 24 25 26 |
# File 'lib/tbot/bot.rb', line 21 def initialize raise NoClientError unless @@client @last_id = nil @@client end |
Class Method Details
.client ⇒ Object
15 16 17 |
# File 'lib/tbot/config.rb', line 15 def self.client @@client end |
.client=(client) ⇒ Object
11 12 13 |
# File 'lib/tbot/config.rb', line 11 def self.client=(client) @@client = Twitter::Client.new (!!client ? client : API_CREDENTIALS) end |
Instance Method Details
#follow(user) ⇒ Object
3 4 5 |
# File 'lib/tbot/follow.rb', line 3 def follow(user) @@client.follow user.id end |
#format_opts(opts) ⇒ Object
DRY: Ensure correct data is passed to search, set instance variables as required.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/tbot/bot.rb', line 6 def format_opts(opts) if opts[:delay] @delay = opts.delete(:delay) # Minimum 60 seconds. @delay = (@delay < 60) ? 60 : @delay end if !!opts[:location] geo = Geocoder.search(opts.delete(:location)).first.geometry['location'].values.join(',') radius = opts.delete(:radius) || 15 opts[:geocode] = "#{geo},#{radius}mi" end end |
#recurring_search(q, opts = {}) ⇒ Object
Works as search but sets up a thread and calls itself infinitely. Minimum delay between searches 60 seconds.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tbot/search.rb', line 16 def recurring_search(q, opts = {}) format_opts opts # Returns current recurring search, or creates a new one. # This should force a single recurring search per instance. # Client held in class method so new instance created per loop... @thread ||= Thread.new do @last_id = nil while true do results = self.search q, opts.merge({ :since_id => @last_id }) @last_id = results.max_id yield results if block_given? sleep @delay end end end |
#reply(tweet, response = "") ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/tbot/reply.rb', line 4 def reply(tweet, response = "") screen_name = tweet.user.screen_name if block_given? # pass user to block. tweet already present to call reply. # force string response. # logic will be handled within this block. @@client.update "@#{screen_name} #{yield(tweet.user).to_s}", { :in_reply_to_status_id => tweet.id } else @@client.update "@#{screen_name} #{response}", { :in_reply_to_status_id => tweet.id } end end |
#search(q, opts = {}) {|results| ... } ⇒ Object
Clean up options, pass to Twitter search function and yield results for block handling.
5 6 7 8 9 10 11 12 13 |
# File 'lib/tbot/search.rb', line 5 def search(q, opts = {}) format_opts opts results = @@client.search q, opts yield results if block_given? results end |
#stop_search! ⇒ Object
> kill thread loop if it exists.
38 39 40 41 42 43 |
# File 'lib/tbot/search.rb', line 38 def stop_search! false unless !!@thread @thread.exit @thread = nil end |