diff --git a/.gitignore b/.gitignore index 0e33e78..9c78f29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.sw? .DS_Store -coverage \ No newline at end of file +coverage +.idea +pkg \ No newline at end of file diff --git a/LICENSE b/LICENSE index eeab8d6..62d5c6d 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README b/README.md similarity index 71% rename from README rename to README.md index f322cc5..2fa109b 100644 --- a/README +++ b/README.md @@ -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. diff --git a/Rakefile b/Rakefile index 4209bec..a62de9f 100644 --- a/Rakefile +++ b/Rakefile @@ -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 \ No newline at end of file diff --git a/TODO b/TODO deleted file mode 100644 index 7902d01..0000000 --- a/TODO +++ /dev/null @@ -1,2 +0,0 @@ -write basic tests - diff --git a/VERSION.yml b/VERSION.yml deleted file mode 100644 index c149eb9..0000000 --- a/VERSION.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -:major: 0 -:minor: 11 -:patch: 0 -:build: diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index 74be172..871679a 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -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 diff --git a/ruby_bosh.gemspec b/ruby_bosh.gemspec index 1ee3dee..87bf119 100644 --- a/ruby_bosh.gemspec +++ b/ruby_bosh.gemspec @@ -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, [">= 0"]) - s.add_runtime_dependency(%q, [">= 0"]) - s.add_runtime_dependency(%q, [">= 0"]) - else - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - end - else - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - end end diff --git a/spec/ruby_bosh_spec.rb b/spec/ruby_bosh_spec.rb index 1b7d70e..f600800 100644 --- a/spec/ruby_bosh_spec.rb +++ b/spec/ruby_bosh_spec.rb @@ -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("") + + @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("") end it "should set the sid attribute after the session creation request" do @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d55e5f0..8b4e348 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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