ruby_new_bosh 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Pradeep Elankumaran
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,43 @@
1
+ ruby_bosh
2
+ =========
3
+
4
+ The RubyBOSH library handles creating and pre-authenticating BOSH streams inside your Ruby application before passing them off to your template engine.
5
+
6
+ This method allows you to hide authentication details for your users' XMPP accounts.
7
+
8
+ Tested on Rails 2.x with eJabberd 1.2+
9
+
10
+ References
11
+ ==========
12
+ BOSH: http://xmpp.org/extensions/xep-0124.html
13
+ XMPP via BOSH: http://xmpp.org/extensions/xep-0206.html
14
+
15
+ Example
16
+ =======
17
+ In your Ruby app controller (or equivalent):
18
+
19
+ @session_jid, @session_id, @session_random_id =
20
+ RubyBOSH.initialize_session("me@jabber.org", "my_password", "http://localhost:5280/http-bind")
21
+
22
+ If you want to define your own resource name, include it within the jid. RubyBOSH will create a random one if none is supplied.
23
+ To define a resource name of 'home', do the following:
24
+
25
+ @session_jid, @session_id, @session_random_id =
26
+ RubyBOSH.initialize_session("me@jabber.org/home", "my_password", "http://localhost:5280/http-bind")
27
+
28
+ In your template, you would then pass these directly to your javascript BOSH connector:
29
+
30
+ var bosh_jid = '<%= @session_jid %>';
31
+ var bosh_sid = '<%= @session_id %>';
32
+ var bosh_rid = '<%= @session_random_id %>';
33
+
34
+ // using Strophe:
35
+ connect.attach(bosh_jid, bosh_sid, bosh_rid, onConnectHandlerFunction);
36
+
37
+ Acknowledgements
38
+ ================
39
+ Jack Moffit
40
+ - thanks for the nice Django example :)
41
+ #=> http://metajack.im/2008/10/03/getting-attached-to-strophe/
42
+
43
+ Copyright (c) 2008 Pradeep Elankumaran. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |s|
6
+ s.name = "ruby_bosh"
7
+ s.summary = %Q{A BOSH session pre-initializer for Ruby web applications}
8
+ s.email = "pradeep@intridea.com"
9
+ s.homepage = "http://github.com/skyfallsin/ruby_bosh"
10
+ s.description = "An XMPP BOSH session pre-initializer for Ruby web applications"
11
+ s.authors = ["Pradeep Elankumaran"]
12
+
13
+ s.add_dependency("builder")
14
+ s.add_dependency("rest-client")
15
+ s.add_dependency("hpricot")
16
+ s.add_dependency("SystemTimer")
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
20
+ end
21
+
22
+ require 'rake/rdoctask'
23
+ Rake::RDocTask.new do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'ruby_bosh'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README*')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib' << 'test'
34
+ t.pattern = 'test/**/*_test.rb'
35
+ t.verbose = false
36
+ end
37
+
38
+ begin
39
+ require 'rcov/rcovtask'
40
+ Rcov::RcovTask.new do |t|
41
+ t.libs << 'test'
42
+ t.test_files = FileList['test/**/*_test.rb']
43
+ t.verbose = true
44
+ end
45
+ rescue LoadError
46
+ puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
47
+ end
48
+
49
+ begin
50
+ require 'cucumber/rake/task'
51
+ Cucumber::Rake::Task.new(:features)
52
+ rescue LoadError
53
+ puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
54
+ end
55
+
56
+ task :default => :test
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ write basic tests
2
+
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 0
3
+ :build:
4
+ :minor: 7
5
+ :patch: 0
@@ -0,0 +1,5 @@
1
+ $:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
2
+
3
+ Autotest.add_discovery do
4
+ "rspec"
5
+ end
data/lib/ruby_bosh.rb ADDED
@@ -0,0 +1,196 @@
1
+ require 'rest_client'
2
+ require 'builder'
3
+ require 'rexml/document'
4
+ require 'base64'
5
+ require 'hpricot'
6
+
7
+ class RubyBOSH
8
+ BOSH_XMLNS = 'http://jabber.org/protocol/httpbind'
9
+ TLS_XMLNS = 'urn:ietf:params:xml:ns:xmpp-tls'
10
+ SASL_XMLNS = 'urn:ietf:params:xml:ns:xmpp-sasl'
11
+ BIND_XMLNS = 'urn:ietf:params:xml:ns:xmpp-bind'
12
+ SESSION_XMLNS = 'urn:ietf:params:xml:ns:xmpp-session'
13
+ CLIENT_XMLNS = 'jabber:client'
14
+
15
+ class Error < StandardError; end
16
+ class Timeout < RubyBOSH::Error; end
17
+ class AuthFailed < RubyBOSH::Error; end
18
+ class ConnFailed < RubyBOSH::Error; end
19
+
20
+ @@logging = true
21
+ def self.logging=(value)
22
+ @@logging = value
23
+ end
24
+
25
+ attr_accessor :jid, :rid, :sid, :success , :custom_resource
26
+ def initialize(jid, pw, service_url, opts={})
27
+ @service_url = service_url
28
+ # Extract the resource if present
29
+ split_jid = jid.split("/")
30
+ @jid = split_jid.first
31
+ @custom_resource = split_jid.last if split_jid.length > 1
32
+ @pw = pw
33
+ @host = @jid.split("@").last
34
+ @success = false
35
+ @timeout = opts[:timeout] || 3 #seconds
36
+ @headers = {"Content-Type" => "text/xml; charset=utf-8",
37
+ "Accept" => "text/xml"}
38
+ @wait = opts[:wait] || 5
39
+ @hold = opts[:hold] || 3
40
+ @window = opts[:window] || 5
41
+ end
42
+
43
+ def success?
44
+ @success == true
45
+ end
46
+
47
+ def self.initialize_session(*args)
48
+ new(*args).connect
49
+ end
50
+
51
+ def connect
52
+ initialize_bosh_session
53
+ if send_auth_request
54
+ send_restart_request
55
+ request_resource_binding
56
+ @success = send_session_request
57
+ end
58
+
59
+ raise RubyBOSH::AuthFailed, "could not authenticate #{@jid}" unless success?
60
+ @rid += 1 #updates the rid for the next call from the browser
61
+
62
+ [@jid, @sid, @rid]
63
+ end
64
+
65
+ private
66
+ def initialize_bosh_session
67
+ response = deliver(construct_body(:wait => @wait, :to => @host,
68
+ :hold => @hold, :window => @window,
69
+ "xmpp:version" => '1.0'))
70
+ parse(response)
71
+ end
72
+
73
+ def construct_body(params={}, &block)
74
+ @rid ? @rid+=1 : @rid=rand(100000)
75
+
76
+ builder = Builder::XmlMarkup.new
77
+ parameters = {:rid => @rid, :xmlns => BOSH_XMLNS,
78
+ "xmpp:version" => "1.0",
79
+ "xmlns:xmpp" => "urn:xmpp:xbosh"}.merge(params)
80
+
81
+ if block_given?
82
+ builder.body(parameters) {|body| yield(body)}
83
+ else
84
+ builder.body(parameters)
85
+ end
86
+ end
87
+
88
+ def send_auth_request
89
+ request = construct_body(:sid => @sid) do |body|
90
+ auth_string = "#{@jid}\x00#{@jid.split("@").first.strip}\x00#{@pw}"
91
+ body.auth(Base64.encode64(auth_string).gsub(/\s/,''),
92
+ :xmlns => SASL_XMLNS, :mechanism => 'PLAIN')
93
+ end
94
+
95
+ response = deliver(request)
96
+ response.include?("success")
97
+ end
98
+
99
+ def send_restart_request
100
+ request = construct_body(:sid => @sid, "xmpp:restart" => true, "xmlns:xmpp" => 'urn:xmpp:xbosh')
101
+ deliver(request).include?("stream:features")
102
+ end
103
+
104
+ def request_resource_binding
105
+ request = construct_body(:sid => @sid) do |body|
106
+ body.iq(:id => "bind_#{rand(100000)}", :type => "set",
107
+ :xmlns => "jabber:client") do |iq|
108
+ iq.bind(:xmlns => BIND_XMLNS) do |bind|
109
+ bind.resource(resource_name)
110
+ end
111
+ end
112
+ end
113
+
114
+ response = deliver(request)
115
+ response.include?("<jid>")
116
+ end
117
+
118
+ def send_session_request
119
+ request = construct_body(:sid => @sid) do |body|
120
+ body.iq(:xmlns => CLIENT_XMLNS, :type => "set",
121
+ :id => "sess_#{rand(100000)}") do |iq|
122
+ iq.session(:xmlns => SESSION_XMLNS)
123
+ end
124
+ end
125
+
126
+ response = deliver(request)
127
+ response.include?("body")
128
+ end
129
+
130
+ def parse(_response)
131
+ doc = Hpricot(_response.to_s)
132
+ doc.search("//body").each do |body|
133
+ @sid = body.attributes["sid"].to_s
134
+ end
135
+ _response
136
+ end
137
+
138
+ begin
139
+ require 'system_timer'
140
+ def deliver(xml)
141
+ SystemTimer.timeout(@timeout) do
142
+ send(xml)
143
+ recv(RestClient.post(@service_url, xml, @headers))
144
+ end
145
+ rescue ::Timeout::Error => e
146
+ raise RubyBOSH::Timeout, e.message
147
+ rescue Errno::ECONNREFUSED => e
148
+ raise RubyBOSH::ConnFailed, "could not connect to #{@host}\n#{e.message}"
149
+ rescue Exception => e
150
+ raise RubyBOSH::Error, e.message
151
+ end
152
+ rescue LoadError
153
+ warn "WARNING: using the built-in Timeout class which is known to have issues when used for opening connections. Install the SystemTimer gem if you want to make sure the Redis client will not hang." unless RUBY_VERSION >= "1.9" || RUBY_PLATFORM =~ /java/
154
+
155
+ require "timeout"
156
+ def deliver(xml)
157
+ ::Timeout::timeout(@timeout) do
158
+ send(xml)
159
+ recv(RestClient.post(@service_url, xml, @headers))
160
+ end
161
+ rescue ::Timeout::Error => e
162
+ raise RubyBOSH::Timeout, e.message
163
+ rescue Errno::ECONNREFUSED => e
164
+ raise RubyBOSH::ConnFailed, "could not connect to #{@host}\n#{e.message}"
165
+ rescue Exception => e
166
+ raise RubyBOSH::Error, e.message
167
+ end
168
+ end
169
+
170
+ def send(msg)
171
+ puts("Ruby-BOSH - SEND\n[#{now}]: #{msg}") if @@logging; msg
172
+ end
173
+
174
+ def recv(msg)
175
+ puts("Ruby-BOSH - RECV\n[#{now}]: #{msg}") if @logging; msg
176
+ end
177
+
178
+ private
179
+ def now
180
+ Time.now.strftime("%a %b %d %H:%M:%S %Y")
181
+ end
182
+
183
+ def resource_name
184
+ if @custom_resource.nil?
185
+ "bosh_#{rand(10000)}"
186
+ else
187
+ @custom_resource
188
+ end
189
+ end
190
+ end
191
+
192
+
193
+ if __FILE__ == $0
194
+ p RubyBOSH.initialize_session(ARGV[0], ARGV[1],
195
+ "http://localhost:5280/http-bind")
196
+ end
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ruby_new_bosh}
8
+ s.version = "0.7.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pradeep Elankumaran"]
12
+ s.date = %q{2011-05-03}
13
+ s.description = %q{An XMPP BOSH session pre-initializer for Ruby web applications}
14
+ s.email = %q{pradeep@intridea.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README",
18
+ "TODO"
19
+ ]
20
+ s.files = [
21
+ "LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "TODO",
25
+ "VERSION.yml",
26
+ "autotest/discover.rb",
27
+ "lib/ruby_bosh.rb",
28
+ "ruby_new_bosh.gemspec",
29
+ "spec/ruby_bosh_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/skyfallsin/ruby_bosh}
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.6.2}
35
+ s.summary = %q{A BOSH session pre-initializer for Ruby web applications}
36
+ s.test_files = [
37
+ "spec/ruby_bosh_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<builder>, [">= 0"])
46
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
47
+ s.add_runtime_dependency(%q<hpricot>, [">= 0"])
48
+ if RUBY_VERSION < "1.9"
49
+ s.add_runtime_dependency(%q<SystemTimer>, [">= 0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<builder>, [">= 0"])
53
+ s.add_dependency(%q<rest-client>, [">= 0"])
54
+ s.add_dependency(%q<hpricot>, [">= 0"])
55
+ if RUBY_VERSION < "1.9"
56
+ s.add_dependency(%q<SystemTimer>, [">= 0"])
57
+ end
58
+ end
59
+ else
60
+ s.add_dependency(%q<builder>, [">= 0"])
61
+ s.add_dependency(%q<rest-client>, [">= 0"])
62
+ s.add_dependency(%q<hpricot>, [">= 0"])
63
+ if RUBY_VERSION < "1.9"
64
+ s.add_dependency(%q<SystemTimer>, [">= 0"])
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,64 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe RubyBOSH do
4
+ before(:each) do
5
+ RubyBOSH.logging = false
6
+ @rbosh = RubyBOSH.new("skyfallsin@localhost", "skyfallsin",
7
+ "http://localhost:5280/http-bind")
8
+ #@rbosh.stub!(:success?).and_return(true)
9
+ #@rbosh.stub!(:initialize_bosh_session).and_return(true)
10
+ @rbosh.stub!(:send_auth_request).and_return(true)
11
+ @rbosh.stub!(:send_restart_request).and_return(true)
12
+ @rbosh.stub!(:request_resource_binding).and_return(true)
13
+ @rbosh.stub!(:send_session_request).and_return(true)
14
+ RestClient.stub!(:post).and_return("<body sid='123456'></body>")
15
+ end
16
+
17
+ it "should set the sid attribute after the session creation request" do
18
+ @rbosh.connect
19
+ @rbosh.sid.should == '123456'
20
+ end
21
+
22
+ it "should update the rid on every call to the BOSH server" do
23
+ @rbosh.rid = 100
24
+ @rbosh.connect
25
+ @rbosh.rid.should > 100
26
+ end
27
+
28
+ it "should return an array with [jid, sid, rid] on success" do
29
+ s = @rbosh.connect
30
+ s.should be_kind_of(Array)
31
+ s.size.should == 3
32
+ s.first.should == 'skyfallsin@localhost'
33
+ s.last.should be_kind_of(Fixnum)
34
+ s[1].should == '123456'
35
+ end
36
+
37
+ describe "Errors" do
38
+ it "should crash with AuthFailed when its not a success?" do
39
+ @rbosh.stub!(:send_session_request).and_return(false)
40
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::AuthFailed)
41
+ end
42
+
43
+ it "should raise a ConnFailed if a connection could not be made to the XMPP server" do
44
+ RestClient.stub!(:post).and_raise(Errno::ECONNREFUSED)
45
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::ConnFailed)
46
+ end
47
+
48
+ it "should raise a Timeout::Error if the BOSH call takes forever" do
49
+ SystemTimer.stub!(:timeout).and_raise(::Timeout::Error)
50
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::Timeout)
51
+ end
52
+
53
+ it "should crash with a generic error on any other problem" do
54
+ [RestClient::ServerBrokeConnection, RestClient::RequestTimeout].each{|err|
55
+ RestClient.stub!(:post).and_raise(err)
56
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::Error)
57
+ }
58
+ end
59
+
60
+ after(:each) do
61
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::Error)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname(__FILE__), '..', "lib", "ruby_bosh")
3
+ require 'spec'
4
+
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_new_bosh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pradeep Elankumaran
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: builder
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: hpricot
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: An XMPP BOSH session pre-initializer for Ruby web applications
63
+ email: pradeep@intridea.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - LICENSE
68
+ - README
69
+ - TODO
70
+ files:
71
+ - LICENSE
72
+ - README
73
+ - Rakefile
74
+ - TODO
75
+ - VERSION.yml
76
+ - autotest/discover.rb
77
+ - lib/ruby_bosh.rb
78
+ - ruby_new_bosh.gemspec
79
+ - spec/ruby_bosh_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: http://github.com/skyfallsin/ruby_bosh
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: A BOSH session pre-initializer for Ruby web applications
105
+ test_files:
106
+ - spec/ruby_bosh_spec.rb
107
+ - spec/spec_helper.rb