Class: Campfire::Bot

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

Direct Known Subclasses

PollingBot

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Bot

Returns a new instance of Bot.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/campfire/bot.rb', line 7

def initialize(config)
  self.config = config
  options = {:token => config.api_token, :ssl_verify => config.ssl_verify }
  self.campfire = Tinder::Campfire.new(config.subdomain, options)
  begin
    self.name = campfire.me['name']
    self.room = campfire.find_room_by_name(config.room) or
      raise ConfigurationError, "Could not find a room named '#{config.room}'"
  rescue Tinder::AuthenticationFailed => e
    raise # maybe do some friendlier error handling later
  end
  room.join
  say("hey guys")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Proxy everything to the room.



49
50
51
# File 'lib/campfire/bot.rb', line 49

def method_missing(m, *args)
  room.send(m, *args)
end

Instance Attribute Details

#campfireObject

Returns the value of attribute campfire.



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

def campfire
  @campfire
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#roomObject

Returns the value of attribute room.



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

def room
  @room
end

Instance Method Details

#base_uriObject



22
23
24
# File 'lib/campfire/bot.rb', line 22

def base_uri
  campfire.connection.uri.to_s
end

#loggerObject



44
45
46
# File 'lib/campfire/bot.rb', line 44

def logger
  config.logger
end

#other_person(exclude = nil) ⇒ Object

return a random person out of the list of users logged in to this room



37
38
39
40
41
42
# File 'lib/campfire/bot.rb', line 37

def other_person(exclude = nil)
  # don't choose the excluded person or ourself
  options = room.users.reject{|u| u[:name] =~ /^(#{exclude}|#{self.name})/ }
  # return the other person's first name, or nil if we didn't find one
  options.any? ? options[rand(options.size)][:name].split(' ').first : nil
end

#say(*args) ⇒ Object

convenience method so I don’t have to change all the old #say method to #speak



27
28
29
# File 'lib/campfire/bot.rb', line 27

def say(*args)
  room.speak(*args)
end

#say_random(sayings) ⇒ Object

pick something at random from an array of sayings



32
33
34
# File 'lib/campfire/bot.rb', line 32

def say_random(sayings)
  say(sayings[rand(sayings.size)])
end