Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.sw?
.DS_Store
coverage
coverage
.idea
pkg
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (c) 2008 Pradeep Elankumaran
Copyright (c) 2008 Pradeep Elankumaran.
Copyright (c) 2018 Mike Polischuk.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
40 changes: 27 additions & 13 deletions README → README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
ruby_bosh
=========
# RubyBosh

The RubyBOSH library handles creating and pre-authenticating BOSH streams inside your Ruby application before passing them off to your template engine.

This method allows you to hide authentication details for your users' XMPP accounts.

Tested on Rails 2.x with eJabberd 1.2+
Tested on Rails 4.2, Ruby 2.1.2 with eJabberd 16.04

## References

References
==========
BOSH: http://xmpp.org/extensions/xep-0124.html

XMPP via BOSH: http://xmpp.org/extensions/xep-0206.html

Example
=======
## Example

In your Ruby app controller (or equivalent):

```ruby
@session_jid, @session_id, @session_random_id =
RubyBOSH.initialize_session("me@jabber.org", "my_password", "http://localhost:5280/http-bind")
```

If you want to define your own resource name, include it within the jid. RubyBOSH will create a random one if none is supplied.
To define a resource name of 'home', do the following:

```ruby
@session_jid, @session_id, @session_random_id =
RubyBOSH.initialize_session("me@jabber.org/home", "my_password", "http://localhost:5280/http-bind")
```

In your template, you would then pass these directly to your javascript BOSH connector:

``` erb
var bosh_jid = '<%= @session_jid %>';
var bosh_sid = '<%= @session_id %>';
var bosh_rid = '<%= @session_random_id %>';
```

``` js
// using Strophe:
connect.attach(bosh_jid, bosh_sid, bosh_rid, onConnectHandlerFunction);
connect.attach(bosh_jid, bosh_sid, bosh_rid, onConnectHandlerFunction);

Acknowledgements
================
Jack Moffit
- thanks for the nice Django example :)
#=> http://metajack.im/2008/10/03/getting-attached-to-strophe/
// using ConverseJS:
converse.initialize({
jid: bosh_jid, sid: bosh_sid, rid: bosh_rid
// ... the rest of the initialization data
})
```

## Acknowledgements

Jack Moffit - thanks for the [nice Django example](http://metajack.im/2008/10/03/getting-attached-to-strophe/)

Copyright (c) 2008 Pradeep Elankumaran. See LICENSE for details.

Copyright (c) 2016 Mike Polischuk. See LICENSE for details.
59 changes: 6 additions & 53 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,55 +1,8 @@
require 'rake'
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rspec/core/rake_task"

begin
require 'jeweler'
Jeweler::Tasks.new do |s|
s.name = "ruby_bosh"
s.summary = %Q{A BOSH session pre-initializer for Ruby web applications}
s.email = "pradeepe@gmail.com"
s.homepage = "http://github.com/skyfallsin/ruby_bosh"
s.description = "An XMPP BOSH session pre-initializer for Ruby web applications"
s.authors = ["Pradeep Elankumaran"]

s.add_dependency("builder")
s.add_dependency("rest-client")
s.add_dependency("hpricot")
end
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
end
RSpec::Core::RakeTask.new

require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ruby_bosh'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end

require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib' << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end

begin
require 'rcov/rcovtask'
Rcov::RcovTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
end
rescue LoadError
puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
end

begin
require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features)
rescue LoadError
puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
end

task :default => :test
task :default => :spec
task :test => :spec
2 changes: 0 additions & 2 deletions TODO

This file was deleted.

5 changes: 0 additions & 5 deletions VERSION.yml

This file was deleted.

16 changes: 0 additions & 16 deletions lib/ruby_bosh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,6 @@ def parse(_response)
end

begin
require 'system_timer'
def deliver(xml)
SystemTimer.timeout(@timeout) do
send(xml)
recv(RestClient.post(@service_url, xml, @headers))
end
rescue Timeout::Error => e
raise RubyBOSH::TimeoutError, e.message
rescue Errno::ECONNREFUSED => e
raise RubyBOSH::ConnFailed, "could not connect to #{@host}\n#{e.message}"
rescue Exception => e
raise RubyBOSH::Error, e.message
end
rescue LoadError
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/

require "timeout"
def deliver(xml)
::Timeout.timeout(@timeout) do
Expand Down
56 changes: 20 additions & 36 deletions ruby_bosh.gemspec
Original file line number Diff line number Diff line change
@@ -1,56 +1,40 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: ruby_bosh 0.11.0 ruby lib

Gem::Specification.new do |s|
s.name = "ruby_bosh"
s.version = "0.11.0"
s.name = "mikemarsian-ruby_bosh"
s.version = "0.13.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib"]
s.authors = ["Pradeep Elankumaran"]
s.date = "2014-12-13"
s.authors = ["Original author: Pradeep Elankumaran. Update for ruby 2: Mike Polischuk"]
s.date = "2016-07-27"
s.description = "An XMPP BOSH session pre-initializer for Ruby web applications"
s.email = "pradeepe@gmail.com"
s.email = "mike@polischuk.com"
s.extra_rdoc_files = [
"LICENSE",
"README",
"TODO"
"README.md"
]
s.licenses = ['MIT']
s.files = [
"LICENSE",
"README",
"README.md",
"Rakefile",
"TODO",
"VERSION.yml",
"autotest/discover.rb",
"lib/ruby_bosh.rb",
"ruby_bosh.gemspec",
"spec/ruby_bosh_spec.rb",
"spec/spec_helper.rb"
]
s.homepage = "http://github.com/skyfallsin/ruby_bosh"
s.rubygems_version = "2.4.1"
s.summary = "A BOSH session pre-initializer for Ruby web applications"
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.homepage = "https://github.com/mikemarsian/ruby_bosh"
s.summary = "A BOSH session pre-initializer for Ruby web applications (for Ruby 2+)"
s.required_ruby_version = '>= 1.9.3'

s.add_development_dependency "bundler", "~> 1.10"
s.add_development_dependency "rake", "~> 10.0"
s.add_development_dependency "rspec", '~> 3.0'
s.add_development_dependency "rspec_junit_formatter", '0.2.2'

if s.respond_to? :specification_version then
s.specification_version = 4
s.add_dependency "builder", "~> 3.0"
s.add_dependency "rest-client" , ">= 1.8"
s.add_dependency "hpricot", "~> 0.8"

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<builder>, [">= 0"])
s.add_runtime_dependency(%q<rest-client>, [">= 0"])
s.add_runtime_dependency(%q<hpricot>, [">= 0"])
else
s.add_dependency(%q<builder>, [">= 0"])
s.add_dependency(%q<rest-client>, [">= 0"])
s.add_dependency(%q<hpricot>, [">= 0"])
end
else
s.add_dependency(%q<builder>, [">= 0"])
s.add_dependency(%q<rest-client>, [">= 0"])
s.add_dependency(%q<hpricot>, [">= 0"])
end
end

26 changes: 10 additions & 16 deletions spec/ruby_bosh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
RubyBOSH.logging = false
@rbosh = RubyBOSH.new("skyfallsin@localhost", "skyfallsin",
"http://localhost:5280/http-bind")
#@rbosh.stub!(:success?).and_return(true)
#@rbosh.stub!(:initialize_bosh_session).and_return(true)
@rbosh.stub!(:send_auth_request).and_return(true)
@rbosh.stub!(:send_restart_request).and_return(true)
@rbosh.stub!(:request_resource_binding).and_return(true)
@rbosh.stub!(:send_session_request).and_return(true)
RestClient.stub!(:post).and_return("<body sid='123456'></body>")

@rbosh.stub(:send_auth_request).and_return(true)
@rbosh.stub(:send_restart_request).and_return(true)
@rbosh.stub(:request_resource_binding).and_return(true)
@rbosh.stub(:send_session_request).and_return(true)
RestClient.stub(:post).and_return("<body sid='123456'></body>")
end

it "should set the sid attribute after the session creation request" do
Expand All @@ -30,29 +29,24 @@
s.should be_kind_of(Array)
s.size.should == 3
s.first.should == 'skyfallsin@localhost'
s.last.should be_kind_of(Fixnum)
s.last.should be_kind_of(Integer)
s[1].should == '123456'
end

describe "Errors" do
it "should crash with AuthFailed when its not a success?" do
@rbosh.stub!(:send_session_request).and_return(false)
@rbosh.stub(:send_session_request).and_return(false)
lambda { @rbosh.connect }.should raise_error(RubyBOSH::AuthFailed)
end

it "should raise a ConnFailed if a connection could not be made to the XMPP server" do
RestClient.stub!(:post).and_raise(Errno::ECONNREFUSED)
RestClient.stub(:post).and_raise(Errno::ECONNREFUSED)
lambda { @rbosh.connect }.should raise_error(RubyBOSH::ConnFailed)
end

it "should raise a Timeout::Error if the BOSH call takes forever" do
SystemTimer.stub!(:timeout).and_raise(Timeout::Error)
lambda { @rbosh.connect }.should raise_error(RubyBOSH::TimeoutError)
end

it "should crash with a generic error on any other problem" do
[RestClient::ServerBrokeConnection, RestClient::RequestTimeout].each{|err|
RestClient.stub!(:post).and_raise(err)
RestClient.stub(:post).and_raise(err)
lambda { @rbosh.connect }.should raise_error(RubyBOSH::Error)
}
end
Expand Down
13 changes: 12 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
require 'rubygems'
require 'rspec'
require File.join(File.dirname(__FILE__), '..', "lib", "ruby_bosh")
require 'spec'

RSpec.configure do |config|

config.expect_with :rspec do |c|
c.syntax = :should
end
config.mock_with :rspec do |c|
c.syntax = :should
end

end