openfire_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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
@@ -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/paulasmuth/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 = "paul@23linesofcode.com"
19
+ gem.authors = ["Paul Asmuth"]
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,6 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ module OpenfireApi; end
5
+
6
+ require 'openfire_api/user_service'
@@ -0,0 +1,77 @@
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
+ http = Net::HTTP.new(uri.host, uri.port)
62
+ resp, data = http.get("#{uri.path}?#{params}")
63
+ return data
64
+ rescue Exception => e
65
+ raise HTTPException, e.to_s
66
+ end
67
+
68
+ def parse_response(data)
69
+ error = data.match(/<error>(.*)<\/error>/)
70
+ if error && @@api_exceptions.include?(error[1])
71
+ raise eval("#{error[1].gsub('Exception', '')}Exception")
72
+ end
73
+ raise InvalidResponseException unless data.match(/<result>ok<\/result>/)
74
+ return true
75
+ end
76
+
77
+ end
@@ -0,0 +1,60 @@
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}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Asmuth"]
12
+ s.date = %q{2011-06-26}
13
+ s.description = %q{ruby client for the openfire xmpp-server user_service api}
14
+ s.email = %q{paul@23linesofcode.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
+ "openfire_api.gemspec",
23
+ "readme.rdoc",
24
+ "spec/spec_helper.rb",
25
+ "spec/user_service_spec.rb"
26
+ ]
27
+ s.homepage = %q{http://github.com/paulasmuth/openfire_api}
28
+ s.require_paths = ["lib"]
29
+ s.rubygems_version = %q{1.6.2}
30
+ s.summary = %q{ruby client for the openfire userservice api}
31
+ s.test_files = [
32
+ "spec/spec_helper.rb",
33
+ "spec/user_service_spec.rb"
34
+ ]
35
+
36
+ if s.respond_to? :specification_version then
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ s.add_development_dependency(%q<rspec>, [">= 2.6.0"])
41
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
42
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
43
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
44
+ s.add_development_dependency(%q<fakeweb>, [">= 0"])
45
+ else
46
+ s.add_dependency(%q<rspec>, [">= 2.6.0"])
47
+ s.add_dependency(%q<shoulda>, [">= 0"])
48
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
50
+ s.add_dependency(%q<fakeweb>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 2.6.0"])
54
+ s.add_dependency(%q<shoulda>, [">= 0"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
57
+ s.add_dependency(%q<fakeweb>, [">= 0"])
58
+ end
59
+ end
60
+
@@ -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/paulasmuth/openfire_api.png" />}[http://travis-ci.org/paulasmuth/openfire_api]
6
+
7
+
8
+ == Installation
9
+
10
+ In your Gemfile
11
+ gem "mtgox-ruby", :git => "git://github.com/paulasmuth/openfire_api.git"
12
+
13
+ or
14
+ gem install git://github.com/paulasmuth/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,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openfire_api
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Paul Asmuth
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-26 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.6.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: shoulda
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.0.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 1.5.2
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: fakeweb
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ type: :development
70
+ version_requirements: *id005
71
+ description: ruby client for the openfire xmpp-server user_service api
72
+ email: paul@23linesofcode.com
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files: []
78
+
79
+ files:
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - Rakefile
83
+ - VERSION
84
+ - lib/openfire_api.rb
85
+ - lib/openfire_api/user_service.rb
86
+ - openfire_api.gemspec
87
+ - readme.rdoc
88
+ - spec/spec_helper.rb
89
+ - spec/user_service_spec.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/paulasmuth/openfire_api
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: "0"
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.6.2
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: ruby client for the openfire userservice api
118
+ test_files:
119
+ - spec/spec_helper.rb
120
+ - spec/user_service_spec.rb