Class: Camptweet::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/camptweet/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Bot

Returns a new instance of Bot.

Yields:

  • (_self)

Yield Parameters:



16
17
18
19
20
21
22
23
24
# File 'lib/camptweet/bot.rb', line 16

def initialize(&block)
  yield self if block_given?
  init_log
  add_twitter_search_urls_to_feed_urls
  connect_to_twitter
  connect_to_campfire
  
  connect_to_campfire_room
end

Instance Attribute Details

#campfireObject (readonly)

Returns the value of attribute campfire.



14
15
16
# File 'lib/camptweet/bot.rb', line 14

def campfire
  @campfire
end

#campfire_emailObject

Returns the value of attribute campfire_email.



10
11
12
# File 'lib/camptweet/bot.rb', line 10

def campfire_email
  @campfire_email
end

#campfire_passwordObject

Returns the value of attribute campfire_password.



11
12
13
# File 'lib/camptweet/bot.rb', line 11

def campfire_password
  @campfire_password
end

#campfire_roomObject

Returns the value of attribute campfire_room.



9
10
11
# File 'lib/camptweet/bot.rb', line 9

def campfire_room
  @campfire_room
end

#campfire_subdomainObject

Returns the value of attribute campfire_subdomain.



7
8
9
# File 'lib/camptweet/bot.rb', line 7

def campfire_subdomain
  @campfire_subdomain
end

#campfire_use_sslObject

Returns the value of attribute campfire_use_ssl.



8
9
10
# File 'lib/camptweet/bot.rb', line 8

def campfire_use_ssl
  @campfire_use_ssl
end

#feed_urlsObject

Returns the value of attribute feed_urls.



6
7
8
# File 'lib/camptweet/bot.rb', line 6

def feed_urls
  @feed_urls
end

#logObject (readonly)

Returns the value of attribute log.



14
15
16
# File 'lib/camptweet/bot.rb', line 14

def log
  @log
end

#logfileObject

Returns the value of attribute logfile.



13
14
15
# File 'lib/camptweet/bot.rb', line 13

def logfile
  @logfile
end

#roomObject (readonly)

Returns the value of attribute room.



14
15
16
# File 'lib/camptweet/bot.rb', line 14

def room
  @room
end

#twitterObject (readonly)

Returns the value of attribute twitter.



14
15
16
# File 'lib/camptweet/bot.rb', line 14

def twitter
  @twitter
end

#twitter_search_termsObject

Returns the value of attribute twitter_search_terms.



5
6
7
# File 'lib/camptweet/bot.rb', line 5

def twitter_search_terms
  @twitter_search_terms
end

#twitter_usersObject

Returns the value of attribute twitter_users.



4
5
6
# File 'lib/camptweet/bot.rb', line 4

def twitter_users
  @twitter_users
end

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/camptweet/bot.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/camptweet/bot.rb', line 26

def run
  last_statuses = {}
  last_feed_items = {}
  
  polling_interval = twitter_users.empty? ? 30 : min_unthrottled_interval
  log.info "Polling interval: #{polling_interval}s"
  
  loop do
    begin
      new_statuses = []
      new_feed_items = []
      
      # check for updated tweets
      checking_twitter_timelines do |user, status|
        if last_statuses[user].nil?
          last_statuses[user] = status
        elsif status.created_at > last_statuses[user].created_at
          new_statuses << status
          last_statuses[user] = status
        end
      end
      
      # post any updated tweets to campfire
      new_statuses.sort_by(&:created_at).each do |status|
        begin
          send_message_to_campfire "[#{status.user.name}] #{status.text}"
        rescue Timeout::Error => e
          log.info "Campfire timeout: (#{e.message})"
        ensure
          sleep 1
        end
      end
      
      # check for updated rss feed items and post them to campfire
      checking_feeds do |feed_url, feed, item|
        log.debug "...checking last_feed_item for this feed: #{last_feed_items[feed_url].blank? ? 'no item' : last_feed_items[feed_url].title}"
        if last_feed_items[feed_url].blank?
          last_feed_items[feed_url] = item
        elsif timestamp_for(item) > timestamp_for(last_feed_items[feed_url])
          last_feed_items[feed_url] = item
          send_message_to_campfire feed_item_message_for(feed, item)
        end          
      end
          
    rescue => e
      log.error e.message
      log.error e.backtrace
      # re-establish potentially lost connection to Twitter
      connect_to_twitter
    end
    log.debug "Sleeping (#{polling_interval}s)"
    sleep polling_interval
  end
end