Class: Del::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/del/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, users:, rooms:) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
# File 'lib/del/connection.rb', line 5

def initialize(configuration:, users:, rooms:)
  @configuration = configuration
  @rooms = rooms
  @users = users
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



3
4
5
# File 'lib/del/connection.rb', line 3

def configuration
  @configuration
end

#roomsObject (readonly)

Returns the value of attribute rooms.



3
4
5
# File 'lib/del/connection.rb', line 3

def rooms
  @rooms
end

#usersObject (readonly)

Returns the value of attribute users.



3
4
5
# File 'lib/del/connection.rb', line 3

def users
  @users
end

Instance Method Details

#connect(robot) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/del/connection.rb', line 11

def connect(robot)
  client.on_exception do |error, connection, error_source|
    disconnect
  end
  client.connect(configuration[:host])
  sleep 0.0001 until client.is_connected?
  client.auth(configuration[:password])
  roster = Jabber::Roster::Helper.new(client, false)
  roster.add_update_callback do |old_item, item|
    users.create(item) if item
  end
  roster.get_roster
  roster.wait_for_roster
  #@mention_name = roster[jid].attributes["mention_name"]
  client.add_message_callback do |message|
    robot.receive(message)
  end
  client.send(Jabber::Presence.new(:chat))
  list_rooms(configuration[:muc_domain]).each do |room|
    rooms.upsert(room)
  end
end

#deliver(jid, message) ⇒ Object



34
35
36
37
38
# File 'lib/del/connection.rb', line 34

def deliver(jid, message)
  message = Jabber::Message.new(jid, encode_string(message))
  message.type = :chat
  client.send(message)
end

#disconnectObject



40
41
42
43
# File 'lib/del/connection.rb', line 40

def disconnect
  puts "byte me!"
  client.close
end