rest_ejabberd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # RestEjabberd
2
+
3
+ Ruby interface to ejabberd's mod_restful
4
+
5
+ This allows to:
6
+
7
+ * create a user
8
+ * delete a user
9
+ * change password
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rake/testtask'
2
+
3
+ namespace :test do
4
+ Rake::TestTask.new :all do |t|
5
+ t.pattern = "test/**/*_test.rb"
6
+ t.libs = ["lib", "test"]
7
+ end
8
+ end
9
+
10
+ task :test => ["test:all"]
11
+ task default: :test
@@ -0,0 +1,69 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'net/http'
4
+
5
+ class RestEjabberd
6
+ def initialize(attributes = {})
7
+ @api = URI.parse attributes.fetch(:api_url, 'http://localhost:8088/api/')
8
+ @secret = attributes.fetch(:secret)
9
+ @client = Net::HTTP.new(@api.host, @api.port)
10
+ @client.set_debug_output $stdout if ENV['DEBUG']
11
+ end
12
+
13
+ def register(username, password, host = @api.host)
14
+ url = @api + "admin"
15
+ request = prepare_request(Net::HTTP::Post.new(url.request_uri))
16
+ request.body = command('register', username, host, password)
17
+
18
+ JSON.parse @client.request(request).body
19
+ end
20
+
21
+ def unregister(username, password, host = @api.host)
22
+ url = @api + "admin"
23
+ request = prepare_request(Net::HTTP::Post.new(url.request_uri))
24
+ request.body = command('unregister', username, host)
25
+
26
+ response = JSON.parse(@client.request(request).body)
27
+ response.has_key?("ok")
28
+ end
29
+
30
+ def change_password(username, old_password, new_password, host = @api.host)
31
+ url = @api + 'register/change_password'
32
+ request = prepare_request(Net::HTTP::Post.new(url.request_uri))
33
+ request.body = change_password_command(username, host, old_password, new_password)
34
+
35
+ @client.request(request).body == '"ok"'
36
+ end
37
+
38
+ def is_registered?(username, password, host = @api.host)
39
+ url = @api + "register/is_registered?username=#{username}&host=#{host}&key=#{@secret}"
40
+ request = prepare_request(Net::HTTP::Get.new(url.request_uri))
41
+ @client.request(request).body == 'true'
42
+ end
43
+
44
+ private
45
+
46
+ def command(command, *args)
47
+ {
48
+ key: @secret,
49
+ command: command,
50
+ args: args
51
+ }.to_json
52
+ end
53
+
54
+ def change_password_command(username, host, old_password, new_password)
55
+ # { "key":"secret", "username":"t1", "host":"localhost", "old_password":"hejhej", "new_password":"sansan" }
56
+ {
57
+ key: @secret,
58
+ username: username,
59
+ host: host,
60
+ old_password: old_password,
61
+ new_password: new_password
62
+ }.to_json
63
+ end
64
+
65
+ def prepare_request(request)
66
+ request['Content-Type'] = 'application/json'
67
+ request
68
+ end
69
+ end
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "rest_ejabberd"
3
+ s.version = "0.0.1"
4
+ s.summary = "Ruby interface for ejabberd's mod_restful"
5
+ s.description = "Easy tool to use ejabberd's mod_restful from Ruby"
6
+ s.authors = ["elcuervo"]
7
+ s.email = ["yo@brunoaguirre.com"]
8
+ s.homepage = "http://github.com/elcuervo/rest_ejabberd"
9
+ s.files = `git ls-files`.split("\n")
10
+ s.test_files = `git ls-files test`.split("\n")
11
+
12
+ s.add_development_dependency("minitest", "~> 3.1.0")
13
+ s.add_development_dependency("vcr", "~> 2.1.1")
14
+ end
@@ -0,0 +1,158 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost:8088/api/admin
6
+ body:
7
+ encoding: UTF-8
8
+ string: "{\"key\":\"secret\",\"command\":\"register\",\"args\":[\"test\",\"localhost\",\"password\"]}"
9
+ headers:
10
+ accept:
11
+ - "*/*"
12
+ user-agent:
13
+ - Ruby
14
+ content-type:
15
+ - application/json
16
+ connection:
17
+ - close
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ connection:
24
+ - close
25
+ content-length:
26
+ - "52"
27
+ content-type:
28
+ - application/json
29
+ body:
30
+ encoding: US-ASCII
31
+ string: "{\"ok\":\"User test@localhost successfully registered\"}"
32
+ http_version: "1.1"
33
+ recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
34
+ - request:
35
+ method: post
36
+ uri: http://localhost:8088/api/register/change_password
37
+ body:
38
+ encoding: UTF-8
39
+ string: "{\"key\":\"secret\",\"username\":\"test\",\"host\":\"localhost\",\"old_password\":\"password\",\"new_password\":\"newpassword\"}"
40
+ headers:
41
+ accept:
42
+ - "*/*"
43
+ user-agent:
44
+ - Ruby
45
+ content-type:
46
+ - application/json
47
+ connection:
48
+ - close
49
+ response:
50
+ status:
51
+ code: 200
52
+ message: OK
53
+ headers:
54
+ connection:
55
+ - close
56
+ content-length:
57
+ - "4"
58
+ content-type:
59
+ - application/json
60
+ body:
61
+ encoding: US-ASCII
62
+ string: "\"ok\""
63
+ http_version: "1.1"
64
+ recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
65
+ - request:
66
+ method: get
67
+ uri: http://localhost:8088/api/register/is_registered?username=test&host=localhost&key=secret
68
+ body:
69
+ encoding: US-ASCII
70
+ string: ""
71
+ headers:
72
+ accept:
73
+ - "*/*"
74
+ user-agent:
75
+ - Ruby
76
+ content-type:
77
+ - application/json
78
+ connection:
79
+ - close
80
+ response:
81
+ status:
82
+ code: 200
83
+ message: OK
84
+ headers:
85
+ connection:
86
+ - close
87
+ content-length:
88
+ - "4"
89
+ content-type:
90
+ - application/json
91
+ body:
92
+ encoding: US-ASCII
93
+ string: "true"
94
+ http_version: "1.1"
95
+ recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
96
+ - request:
97
+ method: post
98
+ uri: http://localhost:8088/api/admin
99
+ body:
100
+ encoding: UTF-8
101
+ string: "{\"key\":\"secret\",\"command\":\"unregister\",\"args\":[\"test\",\"localhost\"]}"
102
+ headers:
103
+ accept:
104
+ - "*/*"
105
+ user-agent:
106
+ - Ruby
107
+ content-type:
108
+ - application/json
109
+ connection:
110
+ - close
111
+ response:
112
+ status:
113
+ code: 200
114
+ message: OK
115
+ headers:
116
+ connection:
117
+ - close
118
+ content-length:
119
+ - "9"
120
+ content-type:
121
+ - application/json
122
+ body:
123
+ encoding: US-ASCII
124
+ string: "{\"ok\":\"\"}"
125
+ http_version: "1.1"
126
+ recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
127
+ - request:
128
+ method: get
129
+ uri: http://localhost:8088/api/register/is_registered?username=test&host=localhost&key=secret
130
+ body:
131
+ encoding: US-ASCII
132
+ string: ""
133
+ headers:
134
+ accept:
135
+ - "*/*"
136
+ user-agent:
137
+ - Ruby
138
+ content-type:
139
+ - application/json
140
+ connection:
141
+ - close
142
+ response:
143
+ status:
144
+ code: 200
145
+ message: OK
146
+ headers:
147
+ connection:
148
+ - close
149
+ content-length:
150
+ - "5"
151
+ content-type:
152
+ - application/json
153
+ body:
154
+ encoding: US-ASCII
155
+ string: "false"
156
+ http_version: "1.1"
157
+ recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
158
+ recorded_with: VCR 2.1.1
@@ -0,0 +1,65 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost:8088/api/admin
6
+ body:
7
+ encoding: UTF-8
8
+ string: "{\"key\":\"secret\",\"command\":\"register\",\"args\":[\"test\",\"localhost\",\"password\"]}"
9
+ headers:
10
+ accept:
11
+ - "*/*"
12
+ user-agent:
13
+ - Ruby
14
+ content-type:
15
+ - application/json
16
+ connection:
17
+ - close
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ connection:
24
+ - close
25
+ content-length:
26
+ - "52"
27
+ content-type:
28
+ - application/json
29
+ body:
30
+ encoding: US-ASCII
31
+ string: "{\"ok\":\"User test@localhost successfully registered\"}"
32
+ http_version: "1.1"
33
+ recorded_at: Wed, 04 Jul 2012 17:21:37 GMT
34
+ - request:
35
+ method: post
36
+ uri: http://localhost:8088/api/admin
37
+ body:
38
+ encoding: UTF-8
39
+ string: "{\"key\":\"secret\",\"command\":\"unregister\",\"args\":[\"test\",\"localhost\"]}"
40
+ headers:
41
+ accept:
42
+ - "*/*"
43
+ user-agent:
44
+ - Ruby
45
+ content-type:
46
+ - application/json
47
+ connection:
48
+ - close
49
+ response:
50
+ status:
51
+ code: 200
52
+ message: OK
53
+ headers:
54
+ connection:
55
+ - close
56
+ content-length:
57
+ - "9"
58
+ content-type:
59
+ - application/json
60
+ body:
61
+ encoding: US-ASCII
62
+ string: "{\"ok\":\"\"}"
63
+ http_version: "1.1"
64
+ recorded_at: Wed, 04 Jul 2012 17:21:37 GMT
65
+ recorded_with: VCR 2.1.1
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ describe RestEjabberd do
4
+ before do
5
+ @client = RestEjabberd.new secret: 'secret'
6
+ @username = 'test'
7
+ @password = 'password'
8
+ end
9
+
10
+ it 'should register and unregister a user' do
11
+ VCR.use_cassette("create and remove a user") do
12
+ response = @client.register @username, @password
13
+
14
+ assert response.has_key?("ok")
15
+ assert @client.unregister @username, @password
16
+ end
17
+ end
18
+
19
+ it 'should change a user password' do
20
+ VCR.use_cassette("change user password") do
21
+ @client.register @username, @password
22
+
23
+ assert @client.change_password @username, @password, 'newpassword'
24
+ assert @client.is_registered?(@username, @password)
25
+ assert @client.unregister @username, @password
26
+ assert !@client.is_registered?(@username, @password)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ require 'vcr'
2
+ require 'minitest/autorun'
3
+ require 'rest_ejabberd'
4
+
5
+ VCR.configure do |c|
6
+ c.cassette_library_dir = 'test/fixtures/cassettes'
7
+ c.default_cassette_options = { record: :once }
8
+ c.hook_into :fakeweb
9
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest_ejabberd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - elcuervo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: vcr
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.1.1
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.1.1
46
+ description: Easy tool to use ejabberd's mod_restful from Ruby
47
+ email:
48
+ - yo@brunoaguirre.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - README.md
54
+ - Rakefile
55
+ - lib/rest_ejabberd.rb
56
+ - rest_ejabberd.gemspec
57
+ - test/fixtures/cassettes/change_user_password.yml
58
+ - test/fixtures/cassettes/create_and_remove_a_user.yml
59
+ - test/rest_ejabberd_test.rb
60
+ - test/test_helper.rb
61
+ homepage: http://github.com/elcuervo/rest_ejabberd
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.22
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Ruby interface for ejabberd's mod_restful
85
+ test_files:
86
+ - test/fixtures/cassettes/change_user_password.yml
87
+ - test/fixtures/cassettes/create_and_remove_a_user.yml
88
+ - test/rest_ejabberd_test.rb
89
+ - test/test_helper.rb