Class: RubyBOSHClient
- Inherits:
-
Object
- Object
- RubyBOSHClient
- Defined in:
- lib/ruby_bosh.rb
Overview
Constant Summary collapse
- BOSH_XMLNS =
'http://jabber.org/protocol/httpbind'- TLS_XMLNS =
'urn:ietf:params:xml:ns:xmpp-tls'- SASL_XMLNS =
'urn:ietf:params:xml:ns:xmpp-sasl'- BIND_XMLNS =
'urn:ietf:params:xml:ns:xmpp-bind'- SESSION_XMLNS =
'urn:ietf:params:xml:ns:xmpp-session'
Instance Attribute Summary collapse
-
#jid ⇒ Object
Returns the value of attribute jid.
-
#rid ⇒ Object
Returns the value of attribute rid.
-
#sid ⇒ Object
Returns the value of attribute sid.
-
#success ⇒ Object
Returns the value of attribute success.
Instance Method Summary collapse
- #connect ⇒ Object
- #construct_body(params = {}, &block) ⇒ Object
-
#initialize(jid, pw, service_url) ⇒ RubyBOSHClient
constructor
A new instance of RubyBOSHClient.
- #initialize_bosh_session ⇒ Object
- #parse(_response) ⇒ Object
- #request_resource_binding ⇒ Object
- #send_auth_request ⇒ Object
- #send_restart_request ⇒ Object
- #send_session_request ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(jid, pw, service_url) ⇒ RubyBOSHClient
Returns a new instance of RubyBOSHClient.
17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby_bosh.rb', line 17 def initialize(jid, pw, service_url) @service_url = service_url @jid, @pw = jid, pw @host = jid.split("@").last @success = false @headers = {"Content-Type" => "text/xml; charset=utf-8", "Accept" => "text/xml"} connect end |
Instance Attribute Details
#jid ⇒ Object
Returns the value of attribute jid.
16 17 18 |
# File 'lib/ruby_bosh.rb', line 16 def jid @jid end |
#rid ⇒ Object
Returns the value of attribute rid.
16 17 18 |
# File 'lib/ruby_bosh.rb', line 16 def rid @rid end |
#sid ⇒ Object
Returns the value of attribute sid.
16 17 18 |
# File 'lib/ruby_bosh.rb', line 16 def sid @sid end |
#success ⇒ Object
Returns the value of attribute success.
16 17 18 |
# File 'lib/ruby_bosh.rb', line 16 def success @success end |
Instance Method Details
#connect ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ruby_bosh.rb', line 27 def connect initialize_bosh_session if send_auth_request send_restart_request request_resource_binding @success = send_session_request end if @success @rid+=1 #send this directly to the browser end end |
#construct_body(params = {}, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruby_bosh.rb', line 47 def construct_body(params={}, &block) @rid ? @rid+=1 : @rid=rand(100000) builder = Builder::XmlMarkup.new parameters = {:rid => @rid, :xmlns => BOSH_XMLNS}.merge(params) if block_given? builder.body(parameters) {|body| yield(body)} else builder.body(parameters) end end |
#initialize_bosh_session ⇒ Object
40 41 42 43 44 45 |
# File 'lib/ruby_bosh.rb', line 40 def initialize_bosh_session response = deliver(construct_body(:wait => 5, :to => @host, :hold => 3, :window => 5, "xmpp:version" => '1.0')) parse(response) end |
#parse(_response) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/ruby_bosh.rb', line 103 def parse(_response) doc = Hpricot(_response) doc.search("//body").each do |body| @sid = body.attributes["sid"].to_s end _response end |
#request_resource_binding ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ruby_bosh.rb', line 77 def request_resource_binding request = construct_body(:sid => @sid) do |body| body.iq(:id => "bind_#{rand(100000)}", :type => "set", :xmlns => "jabber:client") do |iq| iq.bind(:xmlns => BIND_XMLNS) do |bind| bind.resource('presently') end end end response = deliver(request) response.include?("<jid>") end |
#send_auth_request ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruby_bosh.rb', line 60 def send_auth_request request = construct_body(:sid => @sid) do |body| auth_string = "#{@jid}\x00#{@jid.split("@").first.strip}\x00#{@pw}" body.auth(Base64.encode64(auth_string).gsub(/\s/,''), :xmlns => SASL_XMLNS, :mechanism => 'PLAIN') end response = deliver(request) # TODO: make the following more robust response.include?("success") end |
#send_restart_request ⇒ Object
72 73 74 75 |
# File 'lib/ruby_bosh.rb', line 72 def send_restart_request request = construct_body(:sid => @sid, "xmpp:restart" => true, "xmlns:xmpp" => 'urn:xmpp:xbosh') deliver(request).include?("stream:features") end |
#send_session_request ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby_bosh.rb', line 91 def send_session_request request = construct_body(:sid => @sid) do |body| body.iq(:xmlns => "jabber:client", :type => "set", :id => "sess_#{rand(100000)}") do |iq| iq.session(:xmlns => SESSION_XMLNS) end end response = deliver(request) response.include?("body") end |
#success? ⇒ Boolean
111 112 113 |
# File 'lib/ruby_bosh.rb', line 111 def success? success == true end |