rest_ejabberd 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,9 +1,21 @@
1
1
  # RestEjabberd
2
2
 
3
- Ruby interface to ejabberd's mod_restful
3
+ Ruby interface to ejabberd's [https://github.com/jadahl/mod_restful](mod_restful)
4
4
 
5
5
  This allows to:
6
6
 
7
7
  * create a user
8
8
  * delete a user
9
+ * check existance
9
10
  * change password
11
+
12
+ ```ruby
13
+ client = RestEjabberd.new secret: 'shhhhh', host: 'chat.server.com/api/'
14
+ client.register 'me@localhost', '123123'
15
+ client.is_registered? 'me@localhost'
16
+ #=> true
17
+ client.change_password 'me@localhost', '123123', 'asdasd'
18
+ #=> true
19
+ client.unregister 'me@localhost', 'asdasd'
20
+ #=> true
21
+ ```
@@ -10,16 +10,21 @@ class RestEjabberd
10
10
  @client.set_debug_output $stdout if ENV['DEBUG']
11
11
  end
12
12
 
13
- def register(username, password, host = @api.host)
13
+ def register(username, password)
14
+ check_for_host username
14
15
  url = @api + "admin"
16
+ username, host = username.split('@')
17
+
15
18
  request = prepare_request(Net::HTTP::Post.new(url.request_uri))
16
19
  request.body = command('register', username, host, password)
17
20
 
18
21
  JSON.parse @client.request(request).body
19
22
  end
20
23
 
21
- def unregister(username, password, host = @api.host)
24
+ def unregister(username, password)
25
+ check_for_host username
22
26
  url = @api + "admin"
27
+ username, host = username.split('@')
23
28
  request = prepare_request(Net::HTTP::Post.new(url.request_uri))
24
29
  request.body = command('unregister', username, host)
25
30
 
@@ -27,15 +32,19 @@ class RestEjabberd
27
32
  response.has_key?("ok")
28
33
  end
29
34
 
30
- def change_password(username, old_password, new_password, host = @api.host)
35
+ def change_password(username, old_password, new_password)
36
+ check_for_host username
31
37
  url = @api + 'register/change_password'
38
+ username, host = username.split('@')
32
39
  request = prepare_request(Net::HTTP::Post.new(url.request_uri))
33
40
  request.body = change_password_command(username, host, old_password, new_password)
34
41
 
35
42
  @client.request(request).body == '"ok"'
36
43
  end
37
44
 
38
- def is_registered?(username, password, host = @api.host)
45
+ def is_registered?(username)
46
+ check_for_host username
47
+ username, host = username.split('@')
39
48
  url = @api + "register/is_registered?username=#{username}&host=#{host}&key=#{@secret}"
40
49
  request = prepare_request(Net::HTTP::Get.new(url.request_uri))
41
50
  @client.request(request).body == 'true'
@@ -43,6 +52,10 @@ class RestEjabberd
43
52
 
44
53
  private
45
54
 
55
+ def check_for_host(username)
56
+ raise IndexError unless username.include?("@")
57
+ end
58
+
46
59
  def command(command, *args)
47
60
  {
48
61
  key: @secret,
@@ -52,7 +65,6 @@ class RestEjabberd
52
65
  end
53
66
 
54
67
  def change_password_command(username, host, old_password, new_password)
55
- # { "key":"secret", "username":"t1", "host":"localhost", "old_password":"hejhej", "new_password":"sansan" }
56
68
  {
57
69
  key: @secret,
58
70
  username: username,
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rest_ejabberd"
3
- s.version = "0.0.1"
3
+ s.version = "0.0.2"
4
4
  s.summary = "Ruby interface for ejabberd's mod_restful"
5
5
  s.description = "Easy tool to use ejabberd's mod_restful from Ruby"
6
6
  s.authors = ["elcuervo"]
@@ -30,7 +30,7 @@ http_interactions:
30
30
  encoding: US-ASCII
31
31
  string: "{\"ok\":\"User test@localhost successfully registered\"}"
32
32
  http_version: "1.1"
33
- recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
33
+ recorded_at: Fri, 06 Jul 2012 15:25:32 GMT
34
34
  - request:
35
35
  method: post
36
36
  uri: http://localhost:8088/api/register/change_password
@@ -61,7 +61,7 @@ http_interactions:
61
61
  encoding: US-ASCII
62
62
  string: "\"ok\""
63
63
  http_version: "1.1"
64
- recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
64
+ recorded_at: Fri, 06 Jul 2012 15:25:32 GMT
65
65
  - request:
66
66
  method: get
67
67
  uri: http://localhost:8088/api/register/is_registered?username=test&host=localhost&key=secret
@@ -92,7 +92,7 @@ http_interactions:
92
92
  encoding: US-ASCII
93
93
  string: "true"
94
94
  http_version: "1.1"
95
- recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
95
+ recorded_at: Fri, 06 Jul 2012 15:25:32 GMT
96
96
  - request:
97
97
  method: post
98
98
  uri: http://localhost:8088/api/admin
@@ -123,7 +123,7 @@ http_interactions:
123
123
  encoding: US-ASCII
124
124
  string: "{\"ok\":\"\"}"
125
125
  http_version: "1.1"
126
- recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
126
+ recorded_at: Fri, 06 Jul 2012 15:25:32 GMT
127
127
  - request:
128
128
  method: get
129
129
  uri: http://localhost:8088/api/register/is_registered?username=test&host=localhost&key=secret
@@ -154,5 +154,5 @@ http_interactions:
154
154
  encoding: US-ASCII
155
155
  string: "false"
156
156
  http_version: "1.1"
157
- recorded_at: Wed, 04 Jul 2012 17:45:07 GMT
157
+ recorded_at: Fri, 06 Jul 2012 15:25:32 GMT
158
158
  recorded_with: VCR 2.1.1
@@ -30,7 +30,7 @@ http_interactions:
30
30
  encoding: US-ASCII
31
31
  string: "{\"ok\":\"User test@localhost successfully registered\"}"
32
32
  http_version: "1.1"
33
- recorded_at: Wed, 04 Jul 2012 17:21:37 GMT
33
+ recorded_at: Fri, 06 Jul 2012 15:01:50 GMT
34
34
  - request:
35
35
  method: post
36
36
  uri: http://localhost:8088/api/admin
@@ -61,5 +61,5 @@ http_interactions:
61
61
  encoding: US-ASCII
62
62
  string: "{\"ok\":\"\"}"
63
63
  http_version: "1.1"
64
- recorded_at: Wed, 04 Jul 2012 17:21:37 GMT
64
+ recorded_at: Fri, 06 Jul 2012 15:01:50 GMT
65
65
  recorded_with: VCR 2.1.1
@@ -3,10 +3,16 @@ require 'test_helper'
3
3
  describe RestEjabberd do
4
4
  before do
5
5
  @client = RestEjabberd.new secret: 'secret'
6
- @username = 'test'
6
+ @username = 'test@localhost'
7
7
  @password = 'password'
8
8
  end
9
9
 
10
+ it 'should raise an IndexError if the username is malformed' do
11
+ assert_raises IndexError do
12
+ @client.is_registered?('wrong')
13
+ end
14
+ end
15
+
10
16
  it 'should register and unregister a user' do
11
17
  VCR.use_cassette("create and remove a user") do
12
18
  response = @client.register @username, @password
@@ -20,10 +26,10 @@ describe RestEjabberd do
20
26
  VCR.use_cassette("change user password") do
21
27
  @client.register @username, @password
22
28
 
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)
29
+ assert @client.change_password(@username, @password, 'newpassword')
30
+ assert @client.is_registered?(@username)
31
+ assert @client.unregister(@username, @password)
32
+ assert !@client.is_registered?(@username)
27
33
  end
28
34
  end
29
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_ejabberd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-05 00:00:00.000000000 Z
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest