class Tbot::Bot
module Config
module Reply
module Search
Constants
- API_CREDENTIALS
Public Class Methods
client()
click to toggle source
# File lib/tbot/config.rb, line 15 def self.client @@client end
client=(client)
click to toggle source
# File lib/tbot/config.rb, line 11 def self.client=(client) @@client = Twitter::Client.new (!!client ? client : API_CREDENTIALS) end
new()
click to toggle source
Instance methods.
# File lib/tbot/bot.rb, line 21 def initialize raise NoClientError unless @@client @last_id = nil @@client end
Public Instance Methods
follow(user)
click to toggle source
# File lib/tbot/follow.rb, line 3 def follow(user) @@client.follow user.id end
format_opts(opts)
click to toggle source
DRY: Ensure correct data is passed to search, set instance variables as required.
# 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 = {}) { |results| ... }
click to toggle source
Works as search but sets up a thread and calls itself infinitely. Minimum delay between searches 60 seconds.
# 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 = "")
click to toggle source
# 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| ... }
click to toggle source
Clean up options, pass to Twitter search function and yield results for block handling.
# 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