Class: Campfire::Bot
- Inherits:
-
Object
- Object
- Campfire::Bot
- Defined in:
- lib/campfire/bot.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#campfire ⇒ Object
Returns the value of attribute campfire.
-
#config ⇒ Object
Returns the value of attribute config.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#name ⇒ Object
Returns the value of attribute name.
-
#room ⇒ Object
Returns the value of attribute room.
Instance Method Summary collapse
- #base_uri ⇒ Object
-
#initialize(config) ⇒ Bot
constructor
A new instance of Bot.
- #logger ⇒ Object
-
#method_missing(m, *args) ⇒ Object
Proxy everything to the room.
-
#other_person(exclude = nil) ⇒ Object
return a random person out of the list of users logged in to this room.
-
#say(*args) ⇒ Object
convenience method so I don’t have to change all the old #say method to #speak.
-
#say_random(sayings) ⇒ Object
pick something at random from an array of sayings.
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 = {:token => config.api_token, :ssl_verify => config.ssl_verify } self.campfire = Tinder::Campfire.new(config.subdomain, ) 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
#campfire ⇒ Object
Returns the value of attribute campfire.
5 6 7 |
# File 'lib/campfire/bot.rb', line 5 def campfire @campfire end |
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/campfire/bot.rb', line 5 def config @config end |
#debug ⇒ Object
Returns the value of attribute debug.
5 6 7 |
# File 'lib/campfire/bot.rb', line 5 def debug @debug end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/campfire/bot.rb', line 5 def name @name end |
#room ⇒ Object
Returns the value of attribute room.
5 6 7 |
# File 'lib/campfire/bot.rb', line 5 def room @room end |
Instance Method Details
#base_uri ⇒ Object
22 23 24 |
# File 'lib/campfire/bot.rb', line 22 def base_uri campfire.connection.uri.to_s end |
#logger ⇒ Object
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 = room.users.reject{|u| u[:name] =~ /^(#{exclude}|#{self.name})/ } # return the other person's first name, or nil if we didn't find one .any? ? [rand(.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() say([rand(.size)]) end |