Class: Blather::Client

Inherits:
Object show all
Defined in:
lib/blather/client/client.rb

Overview

Blather Client

Blather’s Client class provides a set of helpers for working with common XMPP tasks such as setting up and starting the connection, settings status, registering and dispatching filters and handlers and roster management.

Client can be used separately from the DSL if you’d like to implement your own DSL Here’s the echo example using the client without the DSL:

require 'blather/client/client'
client = Client.setup '[email protected]', 'echo'

client.register_handler(:ready) { puts "Connected ! send messages to #{client.jid.stripped}." }

client.register_handler :subscription, :request? do |s|
  client.write s.approve!
end

client.register_handler :message, :chat?, :body => 'exit' do |m|
  client.write Blather::Stanza::Message.new(m.from, 'Exiting...')
  client.close
end

client.register_handler :message, :chat?, :body do |m|
  client.write Blather::Stanza::Message.new(m.from, "You sent: #{m.body}")
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

:nodoc:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/blather/client/client.rb', line 45

def initialize # :nodoc:
  @state = :initializing

  @status = Stanza::Presence::Status.new
  @handlers = {}
  @tmp_handlers = {}
  @filters = {:before => [], :after => []}
  @roster = Roster.new self

  setup_initial_handlers
end

Instance Attribute Details

#jidObject (readonly)

Returns the value of attribute jid.



32
33
34
# File 'lib/blather/client/client.rb', line 32

def jid
  @jid
end

#rosterObject (readonly)

Returns the value of attribute roster.



32
33
34
# File 'lib/blather/client/client.rb', line 32

def roster
  @roster
end

Class Method Details

.setup(jid, password, host = nil, port = nil) ⇒ Object

Initialize and setup the client

  • jid - the JID to login with

  • password - password associated with the JID

  • host - hostname or IP to connect to. If nil the stream will look one up based on the domain in the JID

  • port - port to connect to



41
42
43
# File 'lib/blather/client/client.rb', line 41

def self.setup(jid, password, host = nil, port = nil)
  self.new.setup(jid, password, host, port)
end

Instance Method Details

#closeObject

Close the connection



134
135
136
# File 'lib/blather/client/client.rb', line 134

def close
  self.stream.close_connection_after_writing
end

#post_init(stream, jid = nil) ⇒ Object

:nodoc:



138
139
140
141
142
# File 'lib/blather/client/client.rb', line 138

def post_init(stream, jid = nil) # :nodoc:
  @stream = stream
  @jid = JID.new(jid) if jid
  self.jid.node ? client_post_init : ready!
end

#receive_data(stanza) ⇒ Object

:nodoc:



148
149
150
151
152
153
154
# File 'lib/blather/client/client.rb', line 148

def receive_data(stanza) # :nodoc:
  catch(:halt) do
    run_filters :before, stanza
    handle_stanza stanza
    run_filters :after, stanza
  end
end

#register_filter(type, handler = nil, *guards, &filter) ⇒ Object

Register a filter to be run before or after the handler chain is run.

  • type - the type of filter. Must be :before or :after

  • guards - guards that should be checked before the filter is called



89
90
91
92
# File 'lib/blather/client/client.rb', line 89

def register_filter(type, handler = nil, *guards, &filter)
  raise "Invalid filter: #{type}. Must be :before or :after" unless [:before, :after].include?(type)
  @filters[type] << [guards, handler, filter]
end

#register_handler(type, *guards, &handler) ⇒ Object

Register a handler

  • type - the handler type. Should be registered in Stanza.handler_list. Blather will log a warning if it’s not.

  • guards - the list of guards that must be verified before the handler will be called



106
107
108
109
110
# File 'lib/blather/client/client.rb', line 106

def register_handler(type, *guards, &handler)
  check_handler type, guards
  @handlers[type] ||= []
  @handlers[type] << [guards, handler]
end

#register_tmp_handler(id, &handler) ⇒ Object

Register a temporary handler. Temporary handlers are based on the ID of the JID and live only until a stanza with said ID is received.

  • id - the ID of the stanza that should be handled



98
99
100
# File 'lib/blather/client/client.rb', line 98

def register_tmp_handler(id, &handler)
  @tmp_handlers[id] = handler
end

#runObject Also known as: connect

Start the connection.



78
79
80
81
82
# File 'lib/blather/client/client.rb', line 78

def run
  raise 'not setup!' unless setup?
  klass = @setup[0].node ? Blather::Stream::Client : Blather::Stream::Component
  klass.start self, *@setup
end

#setup(jid, password, host = nil, port = nil) ⇒ Object

:nodoc:



160
161
162
163
164
165
166
# File 'lib/blather/client/client.rb', line 160

def setup(jid, password, host = nil, port = nil) # :nodoc:
  @jid = JID.new(jid)
  @setup = [@jid, password]
  @setup << host if host
  @setup << port if port
  self
end

#setup?Boolean

:nodoc:

Returns:

  • (Boolean)


156
157
158
# File 'lib/blather/client/client.rb', line 156

def setup? # :nodoc:
  @setup.is_a? Array
end

#statusObject

Get the current status. Taken from the state attribute of Status



59
60
61
# File 'lib/blather/client/client.rb', line 59

def status
  @status.state
end

#status=(state) ⇒ Object

Set the status. Status can be set with either a single value or an array containing [state, message, to].



66
67
68
69
70
71
72
73
74
# File 'lib/blather/client/client.rb', line 66

def status=(state)
  state, msg, to = state

  status = Stanza::Presence::Status.new state, msg
  status.to = to
  @status = status unless to

  write status
end

#unbindObject

:nodoc:



144
145
146
# File 'lib/blather/client/client.rb', line 144

def unbind # :nodoc:
  call_handler_for(:disconnected, nil) || (EM.reactor_running? && EM.stop)
end

#write(stanza) ⇒ Object

Write data to the stream



114
115
116
# File 'lib/blather/client/client.rb', line 114

def write(stanza)
  self.stream.send(stanza)
end

#write_with_handler(stanza, &handler) ⇒ Object

Helper that will create a temporary handler for the stanza being sent before writing it to the stream.

client.write_with_handler(stanza) { |s| "handle stanza here" }

is equivalent to:

client.register_tmp_handler(stanza.id) { |s| "handle stanza here" }
client.write stanza


127
128
129
130
# File 'lib/blather/client/client.rb', line 127

def write_with_handler(stanza, &handler)
  register_tmp_handler stanza.id, &handler
  write stanza
end