Class: Robut::Connection
- Inherits:
-
Object
- Object
- Robut::Connection
- Defined in:
- lib/robut/connection.rb
Overview
Handles opening a connection to the HipChat server, and feeds all messages through our Robut::Plugin list.
Class Attribute Summary collapse
-
.config ⇒ Object
Class-level config.
Instance Attribute Summary collapse
-
#client ⇒ Object
The Jabber::Client that’s connected to the HipChat server.
-
#config ⇒ Object
The configuration used by the Robut connection.
-
#rooms ⇒ Object
The rooms that robut is connected to.
-
#roster ⇒ Object
The roster of currently available people.
-
#store ⇒ Object
The storage instance that’s available to plugins.
Class Method Summary collapse
-
.configure {|config| ... } ⇒ Object
Configures the connection at the class level.
Instance Method Summary collapse
-
#connect ⇒ Object
Connects to the specified room with the given credentials, and enters an infinite loop.
-
#initialize(_config = nil) ⇒ Connection
constructor
Initializes the connection.
-
#reply(*args, &block) ⇒ Object
Send a message to all rooms.
Constructor Details
#initialize(_config = nil) ⇒ Connection
Initializes the connection. If no config
is passed, it defaults to the class_level config
instance variable.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/robut/connection.rb', line 64 def initialize(_config = nil) self.config = _config || self.class.config self.client = Jabber::Client.new(self.config.jid) self.store = self.config.store || Robut::Storage::HashStore # default to in-memory store only self.config.rooms ||= Array(self.config.room) # legacy support? self.config.enable_private_messaging = true if self.config.enable_private_messaging.nil? if self.config.logger Jabber.logger = self.config.logger Jabber.debug = true end end |
Class Attribute Details
.config ⇒ Object
Class-level config. This is set by the configure
class method, and is used if no configuration is passed to the initialize
method.
43 44 45 |
# File 'lib/robut/connection.rb', line 43 def config @config end |
Instance Attribute Details
#client ⇒ Object
The Jabber::Client that’s connected to the HipChat server.
28 29 30 |
# File 'lib/robut/connection.rb', line 28 def client @client end |
#config ⇒ Object
The configuration used by the Robut connection.
Parameters:
jid
,password
,nick
-
The HipChat credentials given on www.hipchat.com/account/xmpp
rooms
-
The chat room(s) to join, with each in the format
jabber_name
@conference_server
logger
-
a logger instance to use for debug output.
25 26 27 |
# File 'lib/robut/connection.rb', line 25 def config @config end |
#rooms ⇒ Object
The rooms that robut is connected to.
37 38 39 |
# File 'lib/robut/connection.rb', line 37 def rooms @rooms end |
#roster ⇒ Object
The roster of currently available people
34 35 36 |
# File 'lib/robut/connection.rb', line 34 def roster @roster end |
#store ⇒ Object
The storage instance that’s available to plugins
31 32 33 |
# File 'lib/robut/connection.rb', line 31 def store @store end |
Class Method Details
.configure {|config| ... } ⇒ Object
Configures the connection at the class level. When the robut
bin file is loaded, it evals the file referenced by the first command-line parameter. This file can configure the connection instance later created by robut
by setting parameters in the Robut::Connection.configure block.
51 52 53 54 |
# File 'lib/robut/connection.rb', line 51 def self.configure self.config = OpenStruct.new yield config end |
Instance Method Details
#connect ⇒ Object
Connects to the specified room with the given credentials, and enters an infinite loop. Any messages sent to the room will pass through all the included plugins.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/robut/connection.rb', line 81 def connect client.connect client.auth(config.password) client.send(Jabber::Presence.new.set_type(:available)) self.roster = Jabber::Roster::Helper.new(client) roster.wait_for_roster self.rooms = self.config.rooms.collect do |room_name| Robut::Room.new(self, room_name).tap {|r| r.join } end if self.config.enable_private_messaging Robut::PM.new(self, rooms) end trap_signals self end |
#reply(*args, &block) ⇒ Object
Send a message to all rooms.
102 103 104 105 106 |
# File 'lib/robut/connection.rb', line 102 def reply(*args, &block) self.rooms.each do |room| room.reply(*args, &block) end end |