openfire_api_ruby 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb929d2a86955280a7f98f635d10a772415c1f7c
4
+ data.tar.gz: 02c232c15415593cb71b4c37d57e9dcaea740173
5
+ SHA512:
6
+ metadata.gz: 31e9390dec95fb43e479af9093799932ee55336ab953e29ada6ee4f7239e8c5986e9bfee69434edf2e6e878bd82807e1fafeb2e515e20dc2642caf83c2f67648
7
+ data.tar.gz: 44e99072df465dfe7d711bbdeff2ad9da680fb448661b057757829ba2a4ddbfb52901f5b90eda40ce9919830fc07cee2dfd9308548f2dac35e5bee768001833c
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", ">= 2.6.0"
5
+ gem "shoulda", ">= 0"
6
+ gem "bundler", "~> 1.0.0"
7
+ gem "jeweler", "~> 1.5.2"
8
+ gem 'fakeweb'
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ fakeweb (1.3.0)
6
+ git (1.2.5)
7
+ jeweler (1.5.2)
8
+ bundler (~> 1.0.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rake (0.9.2)
12
+ rspec (2.6.0)
13
+ rspec-core (~> 2.6.0)
14
+ rspec-expectations (~> 2.6.0)
15
+ rspec-mocks (~> 2.6.0)
16
+ rspec-core (2.6.4)
17
+ rspec-expectations (2.6.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.6.0)
20
+ shoulda (2.11.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ fakeweb
28
+ jeweler (~> 1.5.2)
29
+ rspec (>= 2.6.0)
30
+ shoulda
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "openfire_api"
15
+ gem.homepage = "http://github.com/babu3009/openfire_api"
16
+ gem.summary = %Q{ruby client for the openfire userservice api}
17
+ gem.description = %Q{ruby client for the openfire xmpp-server user_service api}
18
+ gem.email = "babu3009@gmail.com"
19
+ gem.authors = ["Babu"]
20
+ end
21
+ Jeweler::RubygemsDotOrgTasks.new
22
+
23
+ require 'rspec'
24
+ require 'rspec/core/rake_task'
25
+ desc "Run all examples"
26
+ task RSpec::Core::RakeTask.new('spec')
27
+
28
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ module OpenfireApi; end
5
+
6
+ require 'openfire_api/user_service'
7
+ require 'openfire_api/group_service'
@@ -0,0 +1,78 @@
1
+ class OpenfireApi::GroupService
2
+
3
+ @@api_path = "plugins/groupService/groupservice"
4
+ @@api_exceptions = %w(GroupServiceDisabled RequestNotAuthorised IllegalArgumentException GroupNotFoundException GroupAlreadyExistsException)
5
+
6
+ class HTTPException < StandardError; end
7
+ class InvalidResponseException < StandardError; end
8
+ class GroupServiceDisabledException < StandardError; end
9
+ class RequestNotAuthorisedException < StandardError; end
10
+ class IllegalArgumentException < StandardError; end
11
+ class GroupNotFoundException < StandardError; end
12
+ class GroupAlreadyExistsException < StandardError; end
13
+
14
+ def initialize(options=Hash.new)
15
+ @options = { :path => @@api_path }.merge(options)
16
+ end
17
+
18
+ def add_group!(opts)
19
+ submit_request(opts.merge(:type => :add))
20
+ end
21
+
22
+ def delete_group!(opts)
23
+ submit_request(opts.merge(:type => :delete))
24
+ end
25
+
26
+ def update_group!(opts)
27
+ submit_request(opts.merge(:type => :update))
28
+ end
29
+
30
+ def lock_group!(opts)
31
+ submit_request(opts.merge(:type => :disable))
32
+ end
33
+
34
+ def unlock_group!(opts)
35
+ submit_request(opts.merge(:type => :enable))
36
+ end
37
+
38
+ private
39
+
40
+ def build_query(params)
41
+ "#{build_query_uri.to_s}?#{build_query_params(params)}"
42
+ end
43
+
44
+ def build_query_uri
45
+ uri = URI.parse(@options[:url])
46
+ uri.path = File.join(uri.path, @@api_path)
47
+ uri
48
+ end
49
+
50
+ def build_query_params(params)
51
+ params.merge!(:secret => @options[:secret])
52
+ params.to_a.map{ |p| "#{p[0]}=#{p[1]}" }.join('&')
53
+ end
54
+
55
+ def submit_request(params)
56
+ data = submit_http_request(build_query_uri, build_query_params(params))
57
+ parse_response(data)
58
+ end
59
+
60
+ def submit_http_request(uri, params)
61
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
62
+ http.get("#{uri.path}?#{params}")
63
+ end
64
+ return res.body
65
+ rescue Exception => e
66
+ raise HTTPException, e.to_s
67
+ end
68
+
69
+ def parse_response(data)
70
+ error = data.match(/<error>(.*)<\/error>/)
71
+ if error && @@api_exceptions.include?(error[1])
72
+ raise eval("#{error[1].gsub('Exception', '')}Exception")
73
+ end
74
+ raise InvalidResponseException unless data.match(/<result>ok<\/result>/)
75
+ return true
76
+ end
77
+
78
+ end
@@ -0,0 +1,78 @@
1
+ class OpenfireApi::UserService
2
+
3
+ @@api_path = "plugins/userService/userservice"
4
+ @@api_exceptions = %w(UserServiceDisabled RequestNotAuthorised IllegalArgumentException UserNotFoundException UserAlreadyExistsException)
5
+
6
+ class HTTPException < StandardError; end
7
+ class InvalidResponseException < StandardError; end
8
+ class UserServiceDisabledException < StandardError; end
9
+ class RequestNotAuthorisedException < StandardError; end
10
+ class IllegalArgumentException < StandardError; end
11
+ class UserNotFoundException < StandardError; end
12
+ class UserAlreadyExistsException < StandardError; end
13
+
14
+ def initialize(options=Hash.new)
15
+ @options = { :path => @@api_path }.merge(options)
16
+ end
17
+
18
+ def add_user!(opts)
19
+ submit_request(opts.merge(:type => :add))
20
+ end
21
+
22
+ def delete_user!(opts)
23
+ submit_request(opts.merge(:type => :delete))
24
+ end
25
+
26
+ def update_user!(opts)
27
+ submit_request(opts.merge(:type => :update))
28
+ end
29
+
30
+ def lock_user!(opts)
31
+ submit_request(opts.merge(:type => :disable))
32
+ end
33
+
34
+ def unlock_user!(opts)
35
+ submit_request(opts.merge(:type => :enable))
36
+ end
37
+
38
+ private
39
+
40
+ def build_query(params)
41
+ "#{build_query_uri.to_s}?#{build_query_params(params)}"
42
+ end
43
+
44
+ def build_query_uri
45
+ uri = URI.parse(@options[:url])
46
+ uri.path = File.join(uri.path, @@api_path)
47
+ uri
48
+ end
49
+
50
+ def build_query_params(params)
51
+ params.merge!(:secret => @options[:secret])
52
+ params.to_a.map{ |p| "#{p[0]}=#{p[1]}" }.join('&')
53
+ end
54
+
55
+ def submit_request(params)
56
+ data = submit_http_request(build_query_uri, build_query_params(params))
57
+ parse_response(data)
58
+ end
59
+
60
+ def submit_http_request(uri, params)
61
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
62
+ http.get("#{uri.path}?#{params}")
63
+ end
64
+ return res.body
65
+ rescue Exception => e
66
+ raise HTTPException, e.to_s
67
+ end
68
+
69
+ def parse_response(data)
70
+ error = data.match(/<error>(.*)<\/error>/)
71
+ if error && @@api_exceptions.include?(error[1])
72
+ raise eval("#{error[1].gsub('Exception', '')}Exception")
73
+ end
74
+ raise InvalidResponseException unless data.match(/<result>ok<\/result>/)
75
+ return true
76
+ end
77
+
78
+ end
@@ -0,0 +1,61 @@
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{openfire_api_ruby}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Asmuth, Babu"]
12
+ s.date = %q{2014-02-19}
13
+ s.description = %q{ruby client for the openfire xmpp-server user_service api}
14
+ s.email = %q{paul@23linesofcode.com, babu309@gmail.com}
15
+ s.files = [
16
+ "Gemfile",
17
+ "Gemfile.lock",
18
+ "Rakefile",
19
+ "VERSION",
20
+ "lib/openfire_api.rb",
21
+ "lib/openfire_api/user_service.rb",
22
+ "lib/openfire_api/group_service.rb",
23
+ "openfire_api.gemspec",
24
+ "readme.rdoc",
25
+ "spec/spec_helper.rb",
26
+ "spec/user_service_spec.rb"
27
+ ]
28
+ s.homepage = %q{http://github.com/babu3009/openfire_api}
29
+ s.require_paths = ["lib"]
30
+ s.rubygems_version = %q{1.6.2}
31
+ s.summary = %q{ruby client for the openfire userservice api}
32
+ s.test_files = [
33
+ "spec/spec_helper.rb",
34
+ "spec/user_service_spec.rb"
35
+ ]
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_development_dependency(%q<rspec>, [">= 2.6.0"])
42
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
43
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
44
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
45
+ s.add_development_dependency(%q<fakeweb>, [">= 0"])
46
+ else
47
+ s.add_dependency(%q<rspec>, [">= 2.6.0"])
48
+ s.add_dependency(%q<shoulda>, [">= 0"])
49
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
51
+ s.add_dependency(%q<fakeweb>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 2.6.0"])
55
+ s.add_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_dependency(%q<fakeweb>, [">= 0"])
59
+ end
60
+ end
61
+
data/readme.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = openfire_api
2
+
3
+ a ruby client for openfire's user_service api
4
+
5
+ {<img src="https://secure.travis-ci.org/babu3009/openfire_api.png" />}[http://travis-ci.org/babu3009/openfire_api]
6
+
7
+
8
+ == Installation
9
+
10
+ In your Gemfile
11
+ gem "mtgox-ruby", :git => "git://github.com/babu3009/openfire_api.git"
12
+
13
+ or
14
+ gem install git://github.com/babu3009/openfire_api.git
15
+
16
+
17
+ == Usage
18
+
19
+ create a new userservice object
20
+
21
+ api = OpenfireApi::UserService.new(:url => "http://localhost:9090/", :secret => "BIGSECRET")
22
+
23
+ register new user
24
+
25
+ api.add_user!(:username => "user", :password => "pass")
26
+ api.add_user!(:username => "user", :password => "pass", :name => ..., :email => ..., :groups => ...)
27
+
28
+ update existing user
29
+
30
+ api.update_user!(:username => "user", :password => "pass")
31
+ api.update_user!(:username => "user", :password => "pass", :name => ..., :email => ..., :groups => ...)
32
+
33
+ delete user
34
+
35
+ api.delete_user!(:username => "user")
36
+
37
+ lock user
38
+
39
+ api.lock_user!(:username => "user")
40
+
41
+ unlock user
42
+
43
+ api.unlock_user!(:username => "user")
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'fakeweb'
4
+
5
+ FakeWeb.allow_net_connect = false
6
+
7
+ $: << ::File.expand_path('../../lib', __FILE__)
8
+ require 'openfire_api'
9
+
@@ -0,0 +1,88 @@
1
+ require ::File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe "User Service" do
4
+
5
+ before :all do
6
+ @user_service = OpenfireApi::UserService.new(:url => "http://fakehost.int:2323/", :secret => "bigsecret")
7
+ end
8
+
9
+ it "should build query urls" do
10
+ url = @user_service.send(:build_query_uri)
11
+ url.to_s.should == "http://fakehost.int:2323/plugins/userService/userservice"
12
+ end
13
+
14
+ it "should build query params" do
15
+ params = @user_service.send(:build_query_params, :type => :add, :username => "user", :password => "pw")
16
+ params.should include("type=add")
17
+ params.should include("username=user")
18
+ params.should include("password=pw")
19
+ params.should include("secret=bigsecret")
20
+ end
21
+
22
+ it "should build queries" do
23
+ url = @user_service.send(:build_query, :type => :add, :username => "user", :password => "pw")
24
+ url.should include("http://fakehost.int:2323/plugins/userService/userservice?")
25
+ url.should include("type=add")
26
+ url.should include("type=add")
27
+ url.should include("username=user")
28
+ url.should include("password=pw")
29
+ url.should include("secret=bigsecret")
30
+ end
31
+
32
+ it "should submit requests" do
33
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?type=add&username=user&password=pw&secret=bigsecret", :body => "<result>ok</result>")
34
+ @user_service.send(:submit_request, :type => :add, :username => "user", :password => "pw").should == true
35
+ end
36
+
37
+ it "should add users" do
38
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?type=add&username=user&password=pw&secret=bigsecret", :body => "<result>ok</result>")
39
+ @user_service.add_user!(:username => "user", :password => "pw").should == true
40
+ end
41
+
42
+ it "should delete users" do
43
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?type=delete&username=user&secret=bigsecret", :body => "<result>ok</result>")
44
+ @user_service.delete_user!(:username => "user").should == true
45
+ end
46
+
47
+ it "should update users" do
48
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?type=update&username=user&password=pw&name=horst&secret=bigsecret", :body => "<result>ok</result>")
49
+ @user_service.update_user!(:username => "user", :password => "pw", :name => "horst").should == true
50
+ end
51
+
52
+ it "should lock users" do
53
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?type=disable&username=user&secret=bigsecret", :body => "<result>ok</result>")
54
+ @user_service.lock_user!(:username => "user").should == true
55
+ end
56
+
57
+ it "should unlock users" do
58
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?type=enable&username=user&secret=bigsecret", :body => "<result>ok</result>")
59
+ @user_service.unlock_user!(:username => "user").should == true
60
+ end
61
+
62
+ it "should handle the error: user service disabled" do
63
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?username=user1&type=disable&secret=bigsecret", :body => "<error>UserServiceDisabled</error>")
64
+ lambda{ @user_service.lock_user!(:username => "user1") }.should raise_error(OpenfireApi::UserService::UserServiceDisabledException)
65
+ end
66
+
67
+ it "should handle the error: request not authorized" do
68
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?username=user1&type=disable&secret=bigsecret", :body => "<error>RequestNotAuthorised</error>")
69
+ lambda{ @user_service.lock_user!(:username => "user1") }.should raise_error(OpenfireApi::UserService::RequestNotAuthorisedException)
70
+ end
71
+
72
+ it "should handle the error: illegal argument" do
73
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?username=user1&type=disable&secret=bigsecret", :body => "<error>IllegalArgumentException</error>")
74
+ lambda{ @user_service.lock_user!(:username => "user1") }.should raise_error(OpenfireApi::UserService::IllegalArgumentException)
75
+ end
76
+
77
+ it "should handle the error: user not found" do
78
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?username=user1&type=disable&secret=bigsecret", :body => "<error>UserNotFoundException</error>")
79
+ lambda{ @user_service.lock_user!(:username => "user1") }.should raise_error(OpenfireApi::UserService::UserNotFoundException)
80
+ end
81
+
82
+ it "should handle the error: user already exists" do
83
+ FakeWeb.register_uri(:get, "http://fakehost.int:2323/plugins/userService/userservice?username=user1&type=disable&secret=bigsecret", :body => "<error>UserAlreadyExistsException</error>")
84
+ lambda{ @user_service.lock_user!(:username => "user1") }.should raise_error(OpenfireApi::UserService::UserAlreadyExistsException)
85
+ end
86
+
87
+ end
88
+
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openfire_api_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Asmuth, Babu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.6.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.6.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: shoulda
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.5.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakeweb
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ruby client for the openfire xmpp-server user_service api
84
+ email: paul@23linesofcode.com, babu309@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - Gemfile
90
+ - Gemfile.lock
91
+ - Rakefile
92
+ - VERSION
93
+ - lib/openfire_api.rb
94
+ - lib/openfire_api/group_service.rb
95
+ - lib/openfire_api/user_service.rb
96
+ - openfire_api.gemspec
97
+ - readme.rdoc
98
+ - spec/spec_helper.rb
99
+ - spec/user_service_spec.rb
100
+ homepage: http://github.com/babu3009/openfire_api
101
+ licenses: []
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.2.2
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: ruby client for the openfire userservice api
123
+ test_files:
124
+ - spec/spec_helper.rb
125
+ - spec/user_service_spec.rb
126
+ has_rdoc: