blather 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -57,6 +57,18 @@ Setup handlers by calling their names as methods.
57
57
  # and #body == 'exit'
58
58
  message :chat?, :body => 'exit'
59
59
 
60
+ === Non-Stanza Handlers
61
+
62
+ So far there are two non-stanza related handlers.
63
+
64
+ when_ready (or handle(:ready) {})
65
+ Called after the connection has been connected. It's good for initializing your system.
66
+
67
+ disconnected (or handle(:disconnected) {})
68
+ Called after the connection has been terminated. Good for teardown or automatic reconnection.
69
+ The following will reconnect every time the connection is lost:
70
+ disconnected { client.connect }
71
+
60
72
  === Handler Guards
61
73
 
62
74
  Guards act like AND statements. Each condition must be met if the handler is to be used.
@@ -80,6 +80,7 @@ module Blather #:nodoc:
80
80
  klass = @setup[0].node ? Blather::Stream::Client : Blather::Stream::Component
81
81
  klass.start self, *@setup
82
82
  end
83
+ alias_method :connect, :run
83
84
 
84
85
  ##
85
86
  # Register a filter to be run before or after the handler chain is run.
@@ -141,7 +142,7 @@ module Blather #:nodoc:
141
142
  end
142
143
 
143
144
  def unbind # :nodoc:
144
- EM.stop if EM.reactor_running?
145
+ call_handler_for(:disconnected, nil) || (EM.reactor_running? && EM.stop)
145
146
  end
146
147
 
147
148
  def receive_data(stanza) # :nodoc:
@@ -176,7 +177,7 @@ module Blather #:nodoc:
176
177
  end
177
178
 
178
179
  def current_handlers
179
- [:ready] + Stanza.handler_list + BlatherError.handler_list
180
+ [:ready, :disconnected] + Stanza.handler_list + BlatherError.handler_list
180
181
  end
181
182
 
182
183
  def setup_initial_handlers # :nodoc:
@@ -184,9 +185,9 @@ module Blather #:nodoc:
184
185
  raise err
185
186
  end
186
187
 
187
- register_handler :iq, :type => [:get, :set] do |iq|
188
- write StanzaError.new(iq, 'service-unavailable', :cancel).to_node
189
- end
188
+ # register_handler :iq, :type => [:get, :set] do |iq|
189
+ # write StanzaError.new(iq, 'service-unavailable', :cancel).to_node
190
+ # end
190
191
 
191
192
  register_handler :status do |status|
192
193
  roster[status.from].status = status if roster[status.from]
@@ -11,7 +11,7 @@ module Blather
11
11
  module_function :client
12
12
 
13
13
  def pubsub
14
- @pubsub ||= PubSub.new jid.domain
14
+ @pubsub ||= PubSub.new client, jid.domain
15
15
  end
16
16
 
17
17
  ##
@@ -62,6 +62,12 @@ module Blather
62
62
  handle :ready, &block
63
63
  end
64
64
 
65
+ ##
66
+ # Wrapper for "handle :disconnected"
67
+ def disconnected(&block)
68
+ handle :disconnected, &block
69
+ end
70
+
65
71
  ##
66
72
  # Set current status
67
73
  def set_status(state = nil, msg = nil)
@@ -4,7 +4,8 @@ module DSL
4
4
  class PubSub
5
5
  attr_accessor :host
6
6
 
7
- def initialize(host)
7
+ def initialize(client, host)
8
+ @client = client
8
9
  @host = host
9
10
  end
10
11
 
@@ -60,7 +61,7 @@ module DSL
60
61
  # * +node+ is the node to subscribe to
61
62
  # * +jid+ is the jid that should be used. Defaults to the stripped current JID
62
63
  def subscribe(node, jid = nil, host = nil)
63
- jid ||= DSL.client.jid.stripped
64
+ jid ||= client.jid.stripped
64
65
  request(Stanza::PubSub::Subscribe.new(:set, send_to(host), node, jid)) { |n| yield n if block_given? }
65
66
  end
66
67
 
@@ -70,7 +71,7 @@ module DSL
70
71
  # * +node+ is the node to subscribe to
71
72
  # * +jid+ is the jid that should be used. Defaults to the stripped current JID
72
73
  def unsubscribe(node, jid = nil, host = nil)
73
- jid ||= DSL.client.jid.stripped
74
+ jid ||= client.jid.stripped
74
75
  request(Stanza::PubSub::Unsubscribe.new(:set, send_to(host), node, jid)) { |n| yield n if block_given? }
75
76
  end
76
77
 
@@ -120,13 +121,17 @@ module DSL
120
121
  private
121
122
  def request(node, method = nil, callback = nil, &block)
122
123
  block = lambda { |node| callback.call(method ? node.__send__(method) : node) } unless block_given?
123
- DSL.client.write_with_handler(node, &block)
124
+ client.write_with_handler(node, &block)
124
125
  end
125
126
 
126
127
  def send_to(host = nil)
127
128
  raise 'You must provide a host' unless (host ||= @host)
128
129
  host
129
130
  end
131
+
132
+ def client
133
+ @client
134
+ end
130
135
  end
131
136
 
132
137
  end
@@ -76,6 +76,32 @@ describe Blather::Client do
76
76
  @client.unbind
77
77
  end
78
78
 
79
+ it 'calls the :disconnected handler with #unbind is called' do
80
+ EM.expects(:reactor_running?).returns false
81
+ disconnected = mock()
82
+ disconnected.expects(:call)
83
+ @client.register_handler(:disconnected) { disconnected.call }
84
+ @client.unbind
85
+ end
86
+
87
+ it 'does not call EM.stop on #unbind if a handler returns positive' do
88
+ EM.expects(:reactor_running?).never
89
+ EM.expects(:stop).never
90
+ disconnected = mock()
91
+ disconnected.expects(:call).returns true
92
+ @client.register_handler(:disconnected) { disconnected.call }
93
+ @client.unbind
94
+ end
95
+
96
+ it 'calls EM.stop on #unbind if a handler returns negative' do
97
+ EM.expects(:reactor_running?).returns true
98
+ EM.expects(:stop)
99
+ disconnected = mock()
100
+ disconnected.expects(:call).returns false
101
+ @client.register_handler(:disconnected) { disconnected.call }
102
+ @client.unbind
103
+ end
104
+
79
105
  it 'can register a temporary handler based on stanza ID' do
80
106
  stanza = Blather::Stanza::Iq.new
81
107
  response = mock()
@@ -212,26 +238,26 @@ describe 'Blather::Client default handlers' do
212
238
  lambda { @client.receive_data err }.must_raise Blather::BlatherError
213
239
  end
214
240
 
215
- it 'responds to iq:get with a "service-unavailable" error' do
216
- get = Blather::Stanza::Iq.new :get
217
- err = Blather::StanzaError.new(get, 'service-unavailable', :cancel).to_node
218
- @client.expects(:write).with err
219
- @client.receive_data get
220
- end
221
-
222
- it 'responds to iq:get with a "service-unavailable" error' do
223
- get = Blather::Stanza::Iq.new :get
224
- err = Blather::StanzaError.new(get, 'service-unavailable', :cancel).to_node
225
- @client.expects(:write).with { |n| n.to_s.must_equal err.to_s }
226
- @client.receive_data get
227
- end
228
-
229
- it 'responds to iq:set with a "service-unavailable" error' do
230
- get = Blather::Stanza::Iq.new :set
231
- err = Blather::StanzaError.new(get, 'service-unavailable', :cancel).to_node
232
- @client.expects(:write).with { |n| n.to_s.must_equal err.to_s }
233
- @client.receive_data get
234
- end
241
+ # it 'responds to iq:get with a "service-unavailable" error' do
242
+ # get = Blather::Stanza::Iq.new :get
243
+ # err = Blather::StanzaError.new(get, 'service-unavailable', :cancel).to_node
244
+ # @client.expects(:write).with err
245
+ # @client.receive_data get
246
+ # end
247
+
248
+ # it 'responds to iq:get with a "service-unavailable" error' do
249
+ # get = Blather::Stanza::Iq.new :get
250
+ # err = Blather::StanzaError.new(get, 'service-unavailable', :cancel).to_node
251
+ # @client.expects(:write).with { |n| n.to_s.must_equal err.to_s }
252
+ # @client.receive_data get
253
+ # end
254
+
255
+ # it 'responds to iq:set with a "service-unavailable" error' do
256
+ # get = Blather::Stanza::Iq.new :set
257
+ # err = Blather::StanzaError.new(get, 'service-unavailable', :cancel).to_node
258
+ # @client.expects(:write).with { |n| n.to_s.must_equal err.to_s }
259
+ # @client.receive_data get
260
+ # end
235
261
 
236
262
  it 'handles status changes by updating the roster if the status is from a Blather::JID in the roster' do
237
263
  jid = 'friend@jabber.local'
@@ -5,10 +5,9 @@ require 'blather/client/dsl'
5
5
  describe Blather::DSL::PubSub do
6
6
  before do
7
7
  @host = 'host.name'
8
- @pubsub = Blather::DSL::PubSub.new @host
9
8
  @client = mock()
10
9
  @client.stubs(:jid).returns Blather::JID.new('n@d/r')
11
- Blather::DSL.stubs(:client).returns @client
10
+ @pubsub = Blather::DSL::PubSub.new @client, @host
12
11
  end
13
12
 
14
13
  it 'raises an error when trying to send a stanza without a host' do
@@ -273,9 +272,8 @@ end
273
272
  describe 'Blather::DSL::PubSub callbacks' do
274
273
  before do
275
274
  @host = 'host.name'
276
- @pubsub = Blather::DSL::PubSub.new @host
277
275
  @client = Blather::Client.setup Blather::JID.new('n@d/r'), 'pass'
278
- Blather::DSL.stubs(:client).returns @client
276
+ @pubsub = Blather::DSL::PubSub.new @client, @host
279
277
  end
280
278
 
281
279
  it 'returns a list of affiliations when requesting affiliations' do
@@ -63,6 +63,11 @@ describe Blather::DSL do
63
63
  @dsl.when_ready
64
64
  end
65
65
 
66
+ it 'provides a helper for disconnected' do
67
+ @client.expects(:register_handler).with :disconnected
68
+ @dsl.disconnected
69
+ end
70
+
66
71
  it 'sets the initial status' do
67
72
  state = :away
68
73
  msg = 'do not disturb'
@@ -79,9 +79,9 @@ describe Blather::Stream::Parser do
79
79
  '<error type="modify">',
80
80
  '<undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>',
81
81
  '<text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">Some special application diagnostic information...</text>',
82
- '<special-application-condition xmlns="application-ns"/>',
82
+ '<special-application-condition xmlns="special:application-ns"/>',
83
83
  "</error>",
84
- "</message>"
84
+ "</message>",
85
85
  ]
86
86
  data.each { |d| @parser.receive_data d }
87
87
  @client.data.size.must_equal 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blather
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Smick
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-18 00:00:00 -07:00
12
+ date: 2009-08-12 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  requirements: []
131
131
 
132
132
  rubyforge_project: squishtech
133
- rubygems_version: 1.3.4
133
+ rubygems_version: 1.3.5
134
134
  signing_key:
135
135
  specification_version: 3
136
136
  summary: Simpler XMPP built for speed