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:



14
15
16
17
18
19
20
21
# File 'lib/camptweet/bot.rb', line 14

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

Instance Attribute Details

#campfireObject (readonly)

Returns the value of attribute campfire.



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

def campfire
  @campfire
end

#campfire_emailObject

Returns the value of attribute campfire_email.



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

def campfire_email
  @campfire_email
end

#campfire_passwordObject

Returns the value of attribute campfire_password.



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

def campfire_password
  @campfire_password
end

#campfire_roomObject

Returns the value of attribute campfire_room.



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

def campfire_room
  @campfire_room
end

#campfire_subdomainObject

Returns the value of attribute campfire_subdomain.



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

def campfire_subdomain
  @campfire_subdomain
end

#campfire_use_sslObject

Returns the value of attribute campfire_use_ssl.



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

def campfire_use_ssl
  @campfire_use_ssl
end

#logObject (readonly)

Returns the value of attribute log.



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

def log
  @log
end

#logfileObject

Returns the value of attribute logfile.



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

def logfile
  @logfile
end

#roomObject (readonly)

Returns the value of attribute room.



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

def room
  @room
end

#twitterObject (readonly)

Returns the value of attribute twitter.



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

def twitter
  @twitter
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.



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

def verbose
  @verbose
end

Instance Method Details

#runObject



23
24
25
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
# File 'lib/camptweet/bot.rb', line 23

def run
  last_statuses = initial_statuses
  
  loop do
    begin
      new_statuses = []
      checking_twitter_timelines do |user, status|
        if last_statuses[user].nil?
          # Only broadcast this tweet if we have an initial status against
          # which we can compare it.
          last_statuses[user] = status
        elsif status.created_at > last_statuses[user].created_at
          # Only consider the most recent tweet.
          new_statuses << status
          last_statuses[user] = status
        end
      end
          
      new_statuses.sort_by(&:created_at).each do |status|
        begin
          message = "[#{status.user.name}] #{status.text}"
          log.info message
          room.speak message
          log.debug "(Campfire updated)"
        rescue Timeout::Error => e
          log.info "Campfire timeout: (#{e.message})"
        ensure
          sleep 2
        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 (10s)"
    sleep 10
  end
end