Class: RestEjabberd

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_ejabberd.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ RestEjabberd

Returns a new instance of RestEjabberd.



6
7
8
9
10
11
# File 'lib/rest_ejabberd.rb', line 6

def initialize(attributes = {})
  @api = URI.parse attributes.fetch(:api_url, 'http://localhost:8088/api/')
  @secret = attributes.fetch(:secret)
  @client = Net::HTTP.new(@api.host, @api.port)
  @client.set_debug_output $stdout if ENV['DEBUG']
end

Instance Method Details

#change_password(username, old_password, new_password) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rest_ejabberd.rb', line 35

def change_password(username, old_password, new_password)
  check_for_host username
  url = @api + 'register/change_password'
  username, host = username.split('@')
  request = prepare_request(Net::HTTP::Post.new(url.request_uri))
  request.body = change_password_command(username, host, old_password, new_password)

  @client.request(request).body == '"ok"'
end

#is_registered?(username) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/rest_ejabberd.rb', line 45

def is_registered?(username)
  check_for_host username
  username, host = username.split('@')
  url = @api + "register/is_registered?username=#{username}&host=#{host}&key=#{@secret}"
  request = prepare_request(Net::HTTP::Get.new(url.request_uri))
  @client.request(request).body == 'true'
end

#register(username, password) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/rest_ejabberd.rb', line 13

def register(username, password)
  check_for_host username
  url = @api + "admin"
  username, host = username.split('@')

  request = prepare_request(Net::HTTP::Post.new(url.request_uri))
  request.body = command('register', username, host, password)

  JSON.parse @client.request(request).body
end

#unregister(username, password) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rest_ejabberd.rb', line 24

def unregister(username, password)
  check_for_host username
  url = @api + "admin"
  username, host = username.split('@')
  request = prepare_request(Net::HTTP::Post.new(url.request_uri))
  request.body = command('unregister', username, host)

  response = JSON.parse(@client.request(request).body)
  response.has_key?("ok")
end