Class: Robut::Connection

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
77
# 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?
  self.config.port ||= 5222

  if self.config.logger
    Jabber.logger = self.config.logger
    Jabber.debug = true
  end
end

Class Attribute Details

.configObject

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

#clientObject

The Jabber::Client that's connected to the HipChat server.


28
29
30
# File 'lib/robut/connection.rb', line 28

def client
  @client
end

#configObject

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

#roomsObject

The rooms that robut is connected to.


37
38
39
# File 'lib/robut/connection.rb', line 37

def rooms
  @rooms
end

#rosterObject

The roster of currently available people


34
35
36
# File 'lib/robut/connection.rb', line 34

def roster
  @roster
end

#storeObject

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.

Yields:


51
52
53
54
# File 'lib/robut/connection.rb', line 51

def self.configure
  self.config = OpenStruct.new
  yield config
end

Instance Method Details

#connectObject

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.


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/robut/connection.rb', line 82

def connect
  client.connect(config.host, config.port)
  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.


103
104
105
106
107
# File 'lib/robut/connection.rb', line 103

def reply(*args, &block)
  self.rooms.each do |room|
    room.reply(*args, &block)
  end
end