bosh4r 0.0.2

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bosh4r.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ganesh Gunaegaran
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Bosh
2
+
3
+ ## Status
4
+
5
+ The following features are currently implemented
6
+ ### Session
7
+
8
+ Initializes a BOSH session with an XMPP server
9
+ and returns the session identifier and request identifier.
10
+
11
+ Session identifier and request identifier are typically used to
12
+ attach another client side session with the XMPP server
13
+ Ex: http://strophe.im/strophejs/doc/1.0.2/files2/strophe-js.html#Strophe.Connection.attach
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'bosh4r'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install bosh4r
28
+
29
+ ## Usage
30
+
31
+ Bosh4r::Session.new('gg@air.local', 'password').sid
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
40
+
41
+ ## License
42
+
43
+ Copyright (C) 2012 Ganesh Gunasegaran
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
46
+ associated documentation files (the "Software"), to deal in the Software without restriction, including
47
+ without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
48
+ copies of the Software, and to permit persons to whom the Software is furnished to do so,
49
+ subject to the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be included in all copies or substantial
52
+ portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
55
+ LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
56
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
57
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
58
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bosh4r.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/bosh4r/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ganesh Gunaegaran"]
6
+ gem.email = ["me@itsgg.com"]
7
+ gem.description = %q{BOSH ruby client}
8
+ gem.summary = %q{XMPP BOSH ruby client}
9
+ gem.homepage = "https://github.com/itsgg/bosh4r"
10
+ gem.add_dependency 'rest-client', '>= 1.6.7'
11
+ gem.add_dependency 'builder', '>= 3.0.0'
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "bosh4r"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Bosh4r::VERSION
19
+ end
@@ -0,0 +1,4 @@
1
+ require 'bosh4r'
2
+
3
+ session = Bosh4r::Session.new('gg@localhost', 'password')
4
+ p session.sid
@@ -0,0 +1,3 @@
1
+ module Bosh4r
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,107 @@
1
+ # Middleware that initializes a BOSH session with an XMPP server
2
+ # and returns the session identifier and request identifier.
3
+ #
4
+ # Session identifier and request identifier are typically used to
5
+ # attach another client side session with the XMPP server.
6
+ # Ex: http://strophe.im/strophejs/doc/1.0.2/files2/strophe-js.html#Strophe.Connection.attach
7
+ #
8
+ # Author : Ganesh Gunasegaran <me@itsgg.com>
9
+ # Last Modified : June 16, 2012
10
+ # Website : https://github.com/itsgg/bosh_session
11
+ # Copyright : MIT License
12
+
13
+ require 'rexml/document'
14
+ require 'rexml/xpath'
15
+ require 'base64'
16
+ require_relative 'error'
17
+ require_relative 'utils'
18
+
19
+ module Bosh4r
20
+ class Session
21
+ include Bosh4r::Utils
22
+
23
+ attr_reader :sid, :rid
24
+
25
+ def initialize(jabber_id, password, options = {})
26
+ split_jabber_id = jabber_id.split('/')
27
+ bare_jabber_id = split_jabber_id.first
28
+ resource_name = split_jabber_id.last if split_jabber_id.size > 1
29
+
30
+ @jabber_id = bare_jabber_id
31
+ @password = password
32
+ @host = @jabber_id.split('@').last
33
+
34
+ @bosh_url = options[:bosh_url] || 'http://localhost:5280/http-bind'
35
+ @timeout = options[:timeout] || 5 # Network timeout
36
+ @wait = options[:wait] || 5 # Longest time the connection manager is allowed to wait before responding
37
+ @hold = options[:hold] || 1 # Number of connections that the connection manager can hold
38
+ @version = options[:version] || '1.0' # BOSH protocol version
39
+ @rid = rand(1000)
40
+ @resource_name = resource_name.nil? ? "bosh_#{Time.now.to_i.to_s(36)}" : resource_name
41
+ connect
42
+ end
43
+
44
+ def connected?
45
+ @connected
46
+ end
47
+
48
+ protected
49
+ def connect
50
+ throw Bosh4r::Error.new('Failed to initiate BOSH session') unless initiate_session
51
+ throw Bosh4r::Error.new('Failed to authenticate BOSH session') unless authenticate_session
52
+ throw Bosh4r::Error.new('Failed to restart BOSH stream') unless restart_stream
53
+ throw Bosh4r::Error.new('Failed to bind resource') unless bind_resource
54
+ throw Bosh4r::Error.new('Failed to request BOSH session') unless request_session
55
+ @connected = true
56
+ self
57
+ end
58
+
59
+ def initiate_session
60
+ params = build_xml(:wait => @wait, :to => @host, :hold => @hold,
61
+ 'xmpp:version' => @version, :rid => @rid += 1)
62
+ parsed_response = send_bosh_request(@bosh_url, params)
63
+ sid = (REXML::XPath.first parsed_response, '/body').attribute('sid').value()
64
+ @sid = sid
65
+ end
66
+
67
+ def authenticate_session
68
+ # Use SASL with base64 encoding
69
+ username = @jabber_id.split('@').first
70
+ # admin@imac.local
71
+ # admin
72
+ # password
73
+ auth_str = "#{@jabber_id}\u0000#{username}\u0000#{@password}"
74
+ auth_key = Base64.encode64(auth_str).gsub(/\s/, '')
75
+ params = build_xml(:sid => @sid, 'xmpp:version' => @version, :rid => @rid += 1) do |body|
76
+ body.auth(auth_key, :xmlns => 'urn:ietf:params:xml:ns:xmpp-sasl', :mechanism => 'PLAIN')
77
+ end
78
+ REXML::XPath.first send_bosh_request(@bosh_url, params), '/body/success'
79
+ end
80
+
81
+ def restart_stream
82
+ params = build_xml(:sid => @sid, 'xmpp:restart' => true,
83
+ 'xmpp:version' => @version, :rid => @rid += 1)
84
+ REXML::XPath.first send_bosh_request(@bosh_url, params), '/body/stream:features'
85
+ end
86
+
87
+ def bind_resource
88
+ params = build_xml(:sid => @sid, 'xmpp:version' => @version, :rid => @rid += 1) do |body|
89
+ body.iq(:id => "bind_#{rand(1000000)}", :type => 'set', :xmlns => "jabber:client") do |iq|
90
+ iq.bind(:xmlns => 'urn:ietf:params:xml:ns:xmpp-bind') do |bind|
91
+ bind.resource(@resource_name)
92
+ end
93
+ end
94
+ end
95
+ REXML::XPath.first send_bosh_request(@bosh_url, params), '//jid'
96
+ end
97
+
98
+ def request_session
99
+ params = build_xml(:sid => @sid, 'xmpp:version' => @version, :rid => @rid += 1) do |body|
100
+ body.iq(:id => "session_#{rand(1000000)}", :type => 'set', :xmlns => 'jabber:client') do |iq|
101
+ iq.session(:xmlns => 'urn:ietf:params:xml:ns:xmpp-session')
102
+ end
103
+ end
104
+ REXML::XPath.first send_bosh_request(@bosh_url, params), '/body/iq/session'
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,36 @@
1
+ require 'rest_client'
2
+ require 'builder'
3
+ require_relative 'error'
4
+
5
+ module Bosh4r
6
+ module Utils
7
+ def build_xml(options = {}, &callback)
8
+ builder = Builder::XmlMarkup.new
9
+ params = {
10
+ :xmlns => 'http://jabber.org/protocol/httpbind',
11
+ 'xmlns:xmpp' => 'urn:xmpp:xbosh'
12
+ }.merge(options)
13
+ if block_given?
14
+ builder.body(params) { |body|
15
+ yield(body)
16
+ }
17
+ else
18
+ builder.body(params)
19
+ end
20
+ end
21
+
22
+ # Sends bosh request
23
+ def send_bosh_request(url, params)
24
+ response = RestClient.post(url, params, {
25
+ 'Content-Type' => 'text/xml; charset=utf-8',
26
+ 'Accept' => 'text/xml'
27
+ })
28
+ parsed_response = REXML::Document.new(response)
29
+ terminate = (REXML::XPath.first parsed_response, '/body').attribute('type') == 'terminate'
30
+ throw Bosh4r::Error.new 'Check your BOSH endpoint' if terminate
31
+ parsed_response
32
+ rescue => e
33
+ throw Bosh4r::Error.new(e.message)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Bosh4r
2
+ VERSION = "0.0.2"
3
+ end
data/lib/bosh4r.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative "bosh4r/version"
2
+ require_relative "bosh4r/error"
3
+ require_relative "bosh4r/session"
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bosh4r
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ganesh Gunaegaran
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: &70104704864100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70104704864100
25
+ - !ruby/object:Gem::Dependency
26
+ name: builder
27
+ requirement: &70104704848500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70104704848500
36
+ description: BOSH ruby client
37
+ email:
38
+ - me@itsgg.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - bosh4r.gemspec
49
+ - examples/sample.rb
50
+ - lib/bosh4r.rb
51
+ - lib/bosh4r/error.rb
52
+ - lib/bosh4r/session.rb
53
+ - lib/bosh4r/utils.rb
54
+ - lib/bosh4r/version.rb
55
+ homepage: https://github.com/itsgg/bosh4r
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.10
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: XMPP BOSH ruby client
79
+ test_files: []