Skip to content
Browse files

Merge branch 'release/0.5.3'

  • Loading branch information...
2 parents cdd9ffb + 61dd4e4 commit 5fc5fef772b66ee7044cfa0736cef0c6c1079c65 @benlangfeld benlangfeld committed Jun 26, 2011
View
3 CHANGELOG
@@ -1,3 +1,6 @@
+v0.5.3
+ Feature(benlangfeld): Add XMPP Ping (XEP-0199) support
+
v0.5.2
Bugfix(benlangfeld): Remove specs for the Nokogiri extensions which were moved out
View
5 lib/blather.rb
@@ -28,10 +28,11 @@
blather/stanza
blather/stanza/iq
- blather/stanza/iq/query
blather/stanza/iq/command
- blather/stanza/iq/roster
blather/stanza/iq/ibb
+ blather/stanza/iq/ping
+ blather/stanza/iq/query
+ blather/stanza/iq/roster
blather/stanza/iq/s5b
blather/stanza/iq/si
blather/stanza/iq/vcard
View
4 lib/blather/client/client.rb
@@ -226,6 +226,10 @@ def setup_initial_handlers
# write StanzaError.new(iq, 'service-unavailable', :cancel).to_node
# end
+ register_handler :ping, :type => :get do |ping|
+ write ping.reply
+ end
+
register_handler :status do |status|
roster[status.from].status = status if roster[status.from]
nil
View
50 lib/blather/stanza/iq/ping.rb
@@ -0,0 +1,50 @@
+module Blather
+class Stanza
+class Iq
+
+ # # Ping Stanza
+ #
+ # [XEP-0199: XMPP Ping](http://xmpp.org/extensions/xep-0199.html)
+ #
+ # This is a base class for any Ping based Iq stanzas.
+ #
+ # @handler :ping
+ class Ping < Iq
+ # @private
+ register :ping, :ping, 'urn:xmpp:ping'
+
+ # Overrides the parent method to ensure a ping node is created
+ #
+ # @see Blather::Stanza::Iq.new
+ def self.new(type = :get, to = nil, id = nil)
+ node = super
+ node.ping
+ node
+ end
+
+ # Overrides the parent method to ensure the current ping node is destroyed
+ #
+ # @see Blather::Stanza::Iq#inherit
+ def inherit(node)
+ ping.remove
+ super
+ end
+
+ # Ping node accessor
+ # If a ping node exists it will be returned.
+ # Otherwise a new node will be created and returned
+ #
+ # @return [Balather::XMPPNode]
+ def ping
+ p = find_first 'ns:ping', :ns => self.class.registered_ns
+
+ unless p
+ (self << (p = XMPPNode.new('ping', self.document)))
+ p.namespace = self.class.registered_ns
+ end
+ p
+ end
+ end
+end
+end
+end
View
2 lib/blather/version.rb
@@ -1,4 +1,4 @@
module Blather
# Blather version number
- VERSION = '0.5.2'
+ VERSION = '0.5.3'
end
View
7 spec/blather/client/client_spec.rb
@@ -291,6 +291,13 @@
# @client.receive_data get
# end
+ it 'responds to s2c pings with a pong' do
+ ping = Blather::Stanza::Iq::Ping.new :get
+ pong = ping.reply
+ @client.expects(:write).with { |n| n.to_s.must_equal pong.to_s }
+ @client.receive_data ping
+ end
+
it 'handles status changes by updating the roster if the status is from a Blather::JID in the roster' do
jid = 'friend@jabber.local'
status = Blather::Stanza::Presence::Status.new :away
View
41 spec/blather/stanza/iq/ping_spec.rb
@@ -0,0 +1,41 @@
+require 'spec_helper'
+
+def ping_xml
+<<-XML
+<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>
+ <ping xmlns='urn:xmpp:ping'/>
+</iq>
+XML
+end
+
+describe Blather::Stanza::Iq::Ping do
+ it 'registers itself' do
+ Blather::XMPPNode.class_from_registration(:ping, 'urn:xmpp:ping').must_equal Blather::Stanza::Iq::Ping
+ end
+
+ it 'can be imported' do
+ doc = parse_stanza ping_xml
+ node = Blather::XMPPNode.import(doc.root)
+ node.must_be_instance_of Blather::Stanza::Iq::Ping
+ end
+
+ it 'ensures a ping node is present on create' do
+ iq = Blather::Stanza::Iq::Ping.new
+ iq.xpath('ns:ping', :ns => 'urn:xmpp:ping').wont_be_empty
+ end
+
+ it 'ensures a ping node exists when calling #ping' do
+ iq = Blather::Stanza::Iq::Ping.new
+ iq.ping.remove
+ iq.xpath('ns:ping', :ns => 'urn:xmpp:ping').must_be_empty
+
+ iq.ping.wont_be_nil
+ iq.xpath('ns:ping', :ns => 'urn:xmpp:ping').wont_be_empty
+ end
+
+ it 'responds with an empty IQ' do
+ ping = Blather::Stanza::Iq::Ping.new :get, 'one@example.com', 'abc123'
+ ping.from = 'two@example.com'
+ ping.reply.must_equal Blather::Stanza::Iq.new(:result, 'two@example.com', 'abc123')
+ end
+end

0 comments on commit 5fc5fef

Please sign in to comment.
Something went wrong with that request. Please try again.