Class: Blather::Client
- Inherits:
-
Object
- Object
- Blather::Client
- 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) do
puts "Connected ! send messages to #{client.jid.stripped}."
end
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
-
#jid ⇒ Object
readonly
Returns the value of attribute jid.
-
#roster ⇒ Object
readonly
Returns the value of attribute roster.
Class Method Summary collapse
-
.setup(jid, password, host = nil, port = nil) ⇒ Blather::Client
Create a new client and set it up.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection.
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #post_init(stream, jid = nil) ⇒ Object
- #receive_data(stanza) ⇒ Object
-
#register_filter(type, handler = nil, *guards) {|Blather::Stanza| ... } ⇒ Object
Register a filter to be run before or after the handler chain is run.
-
#register_handler(type, *guards, &handler) {|Blather::Stanza| ... } ⇒ Object
Register a handler.
-
#register_tmp_handler(id) {|Blather::Stanza| ... } ⇒ Object
Register a temporary handler.
-
#run ⇒ Object
(also: #connect)
Start the connection.
- #setup(jid, password, host = nil, port = nil) ⇒ Object
- #setup? ⇒ Boolean
-
#status ⇒ Object
Get the current status.
-
#status=(state) ⇒ Object
Set the status.
- #unbind ⇒ Object
-
#write(stanza) ⇒ Object
Write data to the stream.
-
#write_with_handler(stanza) {|Blather::Stanza| ... } ⇒ Object
Helper that will create a temporary handler for the stanza being sent before writing it to the stream.
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/blather/client/client.rb', line 51 def initialize # @private @state = :initializing @status = Stanza::Presence::Status.new @handlers = {} @tmp_handlers = {} @filters = {:before => [], :after => []} @roster = Roster.new self setup_initial_handlers end |
Instance Attribute Details
#jid ⇒ Object (readonly)
Returns the value of attribute jid.
35 36 37 |
# File 'lib/blather/client/client.rb', line 35 def jid @jid end |
#roster ⇒ Object (readonly)
Returns the value of attribute roster.
35 36 37 |
# File 'lib/blather/client/client.rb', line 35 def roster @roster end |
Class Method Details
.setup(jid, password, host = nil, port = nil) ⇒ Blather::Client
Create a new client and set it up
domain
47 48 49 |
# File 'lib/blather/client/client.rb', line 47 def self.setup(jid, password, host = nil, port = nil) self.new.setup(jid, password, host, port) end |
Instance Method Details
#close ⇒ Object
Close the connection
151 152 153 |
# File 'lib/blather/client/client.rb', line 151 def close self.stream.close_connection_after_writing end |
#post_init(stream, jid = nil) ⇒ Object
155 156 157 158 159 |
# File 'lib/blather/client/client.rb', line 155 def post_init(stream, jid = nil) # @private @stream = stream @jid = JID.new(jid) if jid self.jid.node ? client_post_init : ready! end |
#receive_data(stanza) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/blather/client/client.rb', line 165 def receive_data(stanza) # @private catch(:halt) do run_filters :before, stanza handle_stanza stanza run_filters :after, stanza end end |
#register_filter(type, handler = nil, *guards) {|Blather::Stanza| ... } ⇒ Object
Register a filter to be run before or after the handler chain is run.
99 100 101 102 103 104 |
# File 'lib/blather/client/client.rb', line 99 def register_filter(type, handler = nil, *guards, &filter) unless [:before, :after].include?(type) raise "Invalid filter: #{type}. Must be :before or :after" end @filters[type] << [guards, handler, filter] end |
#register_handler(type, *guards, &handler) {|Blather::Stanza| ... } ⇒ Object
Register a handler
120 121 122 123 124 |
# File 'lib/blather/client/client.rb', line 120 def register_handler(type, *guards, &handler) check_handler type, guards @handlers[type] ||= [] @handlers[type] << [guards, handler] end |
#register_tmp_handler(id) {|Blather::Stanza| ... } ⇒ 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.
111 112 113 |
# File 'lib/blather/client/client.rb', line 111 def register_tmp_handler(id, &handler) @tmp_handlers[id.to_s] = handler end |
#run ⇒ Object Also known as: connect
Start the connection.
The stream type used is based on the JID. If a node exists it uses Blather::Stream::Client otherwise Blather::Stream::Component
86 87 88 89 90 |
# File 'lib/blather/client/client.rb', line 86 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
177 178 179 180 181 182 183 |
# File 'lib/blather/client/client.rb', line 177 def setup(jid, password, host = nil, port = nil) # @private @jid = JID.new(jid) @setup = [@jid, password] @setup << host if host @setup << port if port self end |
#setup? ⇒ Boolean
173 174 175 |
# File 'lib/blather/client/client.rb', line 173 def setup? # @private @setup.is_a? Array end |
#status ⇒ Object
Get the current status. Taken from the ‘state` attribute of Status
64 65 66 |
# File 'lib/blather/client/client.rb', line 64 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].
72 73 74 75 76 77 78 79 80 |
# File 'lib/blather/client/client.rb', line 72 def status=(state) state, msg, to = state status = Stanza::Presence::Status.new state, msg status.to = to @status = status unless to write status end |
#unbind ⇒ Object
161 162 163 |
# File 'lib/blather/client/client.rb', line 161 def unbind # @private call_handler_for(:disconnected, nil) || (EM.reactor_running? && EM.stop) end |
#write(stanza) ⇒ Object
Write data to the stream
129 130 131 |
# File 'lib/blather/client/client.rb', line 129 def write(stanza) self.stream.send(stanza) end |
#write_with_handler(stanza) {|Blather::Stanza| ... } ⇒ 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
145 146 147 148 |
# File 'lib/blather/client/client.rb', line 145 def write_with_handler(stanza, &handler) register_tmp_handler stanza.id, &handler write stanza end |