Module: Blather::DSL
- Defined in:
- lib/blather/client/dsl.rb,
lib/blather/client/dsl/pubsub.rb
Overview
# Blather DSL
The DSL is a set of methods that enables you to write cleaner code. Being a module means it can be included in or extend any class you may want to create.
Every stanza handler is registered as a method on the DSL.
Defined Under Namespace
Classes: PubSub
Class Method Summary collapse
-
.client ⇒ Blather::Client
The actual client connection.
Instance Method Summary collapse
-
#<<(stanza) ⇒ self
Push data to the stream This works such that it can be chained: self << stanza1 << stanza2 << “raw data”.
-
#after(handler = nil, *guards) {|Blather::Stanza| ... } ⇒ Object
Setup an after filter.
-
#before(handler = nil, *guards) {|Blather::Stanza| ... } ⇒ Object
Setup a before filter.
-
#disconnected(&block) ⇒ Object
Wrapper for “handle :disconnected”.
-
#discover(what, who, where, &callback) ⇒ Object
Request items or info from an entity discover (items|info), [jid], [node] do |response| end.
-
#halt ⇒ Object
Halt the handler chain.
-
#handle(handler, *guards) {|Blather::Stanza| ... } ⇒ Object
Set handler for a stanza type.
-
#jid ⇒ Blather::JID
The JID according to the server.
-
#my_roster ⇒ Blather::Roster
Direct access to the roster.
-
#pass ⇒ Object
Pass responsibility to the next handler.
-
#pubsub ⇒ Blather::PubSub
A pubsub helper.
-
#say(to, msg) ⇒ Object
Helper method to make sending basic messages easier.
-
#set_status(state = nil, msg = nil) ⇒ Object
Set current status.
-
#setup(jid, password, host = nil, port = nil) ⇒ Object
Prepare server settings.
-
#shutdown ⇒ Object
Shutdown the connection.
-
#when_ready(&block) ⇒ Object
Wrapper for “handle :ready” (just a bit of syntactic sugar).
-
#write_to_stream(stanza) ⇒ Object
Write data to the stream.
Class Method Details
.client ⇒ Blather::Client
The actual client connection
89 90 91 |
# File 'lib/blather/client/dsl.rb', line 89 def client @client ||= Client.new end |
Instance Method Details
#<<(stanza) ⇒ self
Push data to the stream This works such that it can be chained:
self << stanza1 << stanza2 << "raw data"
107 108 109 110 |
# File 'lib/blather/client/dsl.rb', line 107 def <<(stanza) client.write stanza self end |
#after(handler = nil, *guards) {|Blather::Stanza| ... } ⇒ Object
Setup an after filter
run after against
147 148 149 |
# File 'lib/blather/client/dsl.rb', line 147 def after(handler = nil, *guards, &block) client.register_filter :after, handler, *guards, &block end |
#before(handler = nil, *guards) {|Blather::Stanza| ... } ⇒ Object
Setup a before filter
run before against
136 137 138 |
# File 'lib/blather/client/dsl.rb', line 136 def before(handler = nil, *guards, &block) client.register_filter :before, handler, *guards, &block end |
#disconnected(&block) ⇒ Object
Wrapper for “handle :disconnected”
This is run after the connection has been shut down.
174 175 176 |
# File 'lib/blather/client/dsl.rb', line 174 def disconnected(&block) handle :disconnected, &block end |
#discover(what, who, where, &callback) ⇒ Object
Request items or info from an entity
discover (items|info), [jid], [node] do |response|
end
246 247 248 249 250 251 252 253 |
# File 'lib/blather/client/dsl.rb', line 246 def discover(what, who, where, &callback) stanza = Blather::Stanza.class_from_registration(:query, "http://jabber.org/protocol/disco##{what}").new stanza.to = who stanza.node = where client.register_tmp_handler stanza.id, &callback client.write stanza end |
#halt ⇒ Object
Halt the handler chain
Use this to stop the propogation of the stanza though the handler chain.
Ignore all IQ stanzas
before(:iq) { halt }
224 225 226 |
# File 'lib/blather/client/dsl.rb', line 224 def halt throw :halt end |
#handle(handler, *guards) {|Blather::Stanza| ... } ⇒ Object
Set handler for a stanza type
against
157 158 159 |
# File 'lib/blather/client/dsl.rb', line 157 def handle(handler, *guards, &block) client.register_handler handler, *guards, &block end |
#jid ⇒ Blather::JID
The JID according to the server
212 213 214 |
# File 'lib/blather/client/dsl.rb', line 212 def jid client.jid end |
#my_roster ⇒ Blather::Roster
Direct access to the roster
190 191 192 |
# File 'lib/blather/client/dsl.rb', line 190 def my_roster client.roster end |
#pass ⇒ Object
Pass responsibility to the next handler
Use this to jump out of the current handler and let the next registered handler take care of the stanza
This is contrive and should be handled with guards, but pass a message to the next handler based on the content
{ |s| puts "message caught" }
{ |s| pass if s.body =~ /pass along/ }
239 240 241 |
# File 'lib/blather/client/dsl.rb', line 239 def pass throw :pass end |
#pubsub ⇒ Blather::PubSub
A pubsub helper
97 98 99 |
# File 'lib/blather/client/dsl.rb', line 97 def pubsub @pubsub ||= PubSub.new client, jid.domain end |
#say(to, msg) ⇒ Object
Helper method to make sending basic messages easier
205 206 207 |
# File 'lib/blather/client/dsl.rb', line 205 def say(to, msg) client.write Blather::Stanza::Message.new(to, msg) end |
#set_status(state = nil, msg = nil) ⇒ Object
Set current status
state
183 184 185 |
# File 'lib/blather/client/dsl.rb', line 183 def set_status(state = nil, msg = nil) client.status = state, msg end |
#setup(jid, password, host = nil, port = nil) ⇒ Object
Prepare server settings
this is ‘nil` the domain on the JID will be used
119 120 121 |
# File 'lib/blather/client/dsl.rb', line 119 def setup(jid, password, host = nil, port = nil) client.setup(jid, password, host, port) end |
#shutdown ⇒ Object
Shutdown the connection. Flushes the write buffer then stops EventMachine
125 126 127 |
# File 'lib/blather/client/dsl.rb', line 125 def shutdown client.close end |
#when_ready(&block) ⇒ Object
Wrapper for “handle :ready” (just a bit of syntactic sugar)
This is run after the connection has been completely setup
164 165 166 |
# File 'lib/blather/client/dsl.rb', line 164 def when_ready(&block) handle :ready, &block end |