skyfallsin-ruby_bosh 0.0.0 → 0.1.0

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.
Files changed (3) hide show
  1. data/VERSION.yml +4 -0
  2. data/lib/ruby_bosh.rb +27 -19
  3. metadata +1 -1
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 1
data/lib/ruby_bosh.rb CHANGED
@@ -3,6 +3,7 @@ require 'builder'
3
3
  require 'rexml/document'
4
4
  require 'base64'
5
5
  require 'hpricot'
6
+ require 'system_timer'
6
7
 
7
8
  # based on
8
9
  # http://code.stanziq.com/svn/strophe/trunk/strophejs/examples/attach/boshclient.py
@@ -13,17 +14,33 @@ class RubyBOSHClient
13
14
  BIND_XMLNS = 'urn:ietf:params:xml:ns:xmpp-bind'
14
15
  SESSION_XMLNS = 'urn:ietf:params:xml:ns:xmpp-session'
15
16
 
17
+ class Timeout < StandardError; end
18
+
16
19
  attr_accessor :jid, :rid, :sid, :success
17
- def initialize(jid, pw, service_url)
20
+ def initialize(jid, pw, service_url, opts={})
18
21
  @service_url = service_url
19
22
  @jid, @pw = jid, pw
20
23
  @host = jid.split("@").last
21
24
  @success = false
25
+ @timeout = opts[:timeout] || 3 #seconds
22
26
  @headers = {"Content-Type" => "text/xml; charset=utf-8",
23
27
  "Accept" => "text/xml"}
24
28
  connect
25
29
  end
26
30
 
31
+ def initialize_bosh_session
32
+ response = deliver(construct_body(:wait => 5, :to => @host,
33
+ :hold => 3, :window => 5,
34
+ "xmpp:version" => '1.0'))
35
+ parse(response)
36
+ end
37
+
38
+ def success?
39
+ success == true
40
+ end
41
+
42
+ private
43
+
27
44
  def connect
28
45
  initialize_bosh_session
29
46
  if send_auth_request
@@ -36,13 +53,6 @@ class RubyBOSHClient
36
53
  @rid+=1 #send this directly to the browser
37
54
  end
38
55
  end
39
-
40
- def initialize_bosh_session
41
- response = deliver(construct_body(:wait => 5, :to => @host,
42
- :hold => 3, :window => 5,
43
- "xmpp:version" => '1.0'))
44
- parse(response)
45
- end
46
56
 
47
57
  def construct_body(params={}, &block)
48
58
  @rid ? @rid+=1 : @rid=rand(100000)
@@ -65,7 +75,6 @@ class RubyBOSHClient
65
75
  end
66
76
 
67
77
  response = deliver(request)
68
- # TODO: make the following more robust
69
78
  response.include?("success")
70
79
  end
71
80
 
@@ -108,14 +117,13 @@ class RubyBOSHClient
108
117
  _response
109
118
  end
110
119
 
111
- def success?
112
- success == true
113
- end
114
-
115
- private
116
120
  def deliver(xml)
117
- send(xml)
118
- recv(RestClient.post(@service_url, xml, @headers))
121
+ SystemTimer.timeout(@timeout) do
122
+ send(xml)
123
+ recv(RestClient.post(@service_url, xml, @headers))
124
+ end
125
+ rescue SystemTimer::Error => e
126
+ raise RubyBOSHClient::Timeout, e.message
119
127
  end
120
128
 
121
129
  def send(msg)
@@ -129,8 +137,8 @@ end
129
137
 
130
138
 
131
139
  class RubyBOSH
132
- def self.initialize_session(jid, pw, service_url)
133
- conn = RubyBOSHClient.new(jid, pw, service_url)
140
+ def self.initialize_session(jid, pw, service_url, opts={})
141
+ conn = RubyBOSHClient.new(jid, pw, service_url, opts)
134
142
  if conn.success?
135
143
  [conn.jid, conn.sid, conn.rid]
136
144
  else
@@ -140,6 +148,6 @@ class RubyBOSH
140
148
  end
141
149
 
142
150
  if __FILE__ == $0
143
- p RubyBOSH.initialize_session("skyfallsin@localhost", "skyfallsin",
151
+ p RubyBOSH.initialize_session(ARGV[0], ARGV[1],
144
152
  "http://localhost:5280/http-bind")
145
153
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skyfallsin-ruby_bosh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pradeep Elankumaran