class EjabberdRest::Client

Constants

DEFAULT_MOD_REST_URL

Attributes

debug[RW]
max_concurrency[RW]
mod_rest_url[RW]

Public Class Methods

new(url=DEFAULT_MOD_REST_URL, options={}) click to toggle source
# File lib/ejabberd_rest/client.rb, line 10
def initialize(url=DEFAULT_MOD_REST_URL, options={})
  manager = Typhoeus::Hydra.new(max_concurrency: options[:max_concurrency] || 25)

  @connection = Faraday.new(url, parallel_manager: manager) do |builder|
    builder.request  :url_encoded
    builder.response :logger if options[:debug]
    builder.adapter  :typhoeus
    builder.options.timeout = options[:timeout] || 3
    builder.options.open_timeout = options[:open_timeout] || 1
  end
end

Public Instance Methods

add_user(username, domain, password) click to toggle source
# File lib/ejabberd_rest/client.rb, line 22
def add_user(username, domain, password)
  response = post("/rest", body: "register #{username} #{domain} #{password}")

  if response.status >= 400
    raise Error::RequestFailed, "HTTP Status: #{response.status}"
  else
    if response.body.include?("successfully registered")
      true
    else
      if response.body.include?("already registered")
        raise Error::UserAlreadyRegistered
      else
        raise Error::UnknownError, response.body
      end
    end
  end
end
delete_user(username, domain) click to toggle source
# File lib/ejabberd_rest/client.rb, line 40
def delete_user(username, domain)
  post("/rest", body: "unregister #{username} #{domain}")
end
modify_affiliations(from_jid, host, node, affiliations = {}) click to toggle source
# File lib/ejabberd_rest/client.rb, line 56
def modify_affiliations(from_jid, host, node, affiliations = {})
  stanza =  "<iq type='set' from='#{from_jid}' to='#{host}' id='#{SecureRandom.uuid}'>"
  stanza <<   "<pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>"
  stanza <<     "<affiliations node='#{node}'>"
  affiliations.each do |k,v|
    stanza <<     "<affiliation jid='#{k}' affiliation='#{v}' />"
  end
  stanza <<     "</affiliations>"
  stanza <<   "</pubsub>"
  stanza << "</iq>"

  post_stanza(stanza)
end
post_all_stanzas(stanzas) click to toggle source
# File lib/ejabberd_rest/client.rb, line 48
def post_all_stanzas(stanzas)
  @connection.in_parallel do
    stanzas.each do |stanza|
      post_stanza(stanza)
    end
  end
end
post_stanza(stanza) click to toggle source
# File lib/ejabberd_rest/client.rb, line 44
def post_stanza(stanza)
  post("/rest", body: stanza)
end
pubsub_delete_node(from_jid, host, node) click to toggle source
# File lib/ejabberd_rest/client.rb, line 70
def pubsub_delete_node(from_jid, host, node)
  stanza =  "<iq type='set' from='#{from_jid}' to='#{host}' id='#{SecureRandom.uuid}'>"
  stanza <<   "<pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>"
  stanza <<     "<delete node='#{node}'/>"
  stanza <<   "</pubsub>"
  stanza << "</iq>"

  post_stanza(stanza)
end
pubsub_item_stanza(from_jid, host, node, message) click to toggle source
# File lib/ejabberd_rest/client.rb, line 80
def pubsub_item_stanza(from_jid, host, node, message)
  stanza =  "<iq type='set' from='#{from_jid}' to='#{host}' id='#{SecureRandom.uuid}'>"
  stanza <<   "<pubsub xmlns='http://jabber.org/protocol/pubsub'>"
  stanza <<     "<publish node='#{node}'>"
  stanza <<       "<item id='#{SecureRandom.uuid}'>"
  stanza <<         "#{message}"
  stanza <<       "</item>"
  stanza <<     "</publish>"
  stanza <<   "</pubsub>"
  stanza << "</iq>"

  stanza
end
pubsub_publish(from_jid, host, node, message) click to toggle source
# File lib/ejabberd_rest/client.rb, line 94
def pubsub_publish(from_jid, host, node, message)
  post_stanza(pubsub_item_stanza(from_jid, host, node, message))
end
pubsub_publish_all_items(from_jid, host, node, items) click to toggle source
# File lib/ejabberd_rest/client.rb, line 98
def pubsub_publish_all_items(from_jid, host, node, items)
  @connection.in_parallel do
    items.each do |item|
      pubsub_publish(from_jid, host, node, item)
    end
  end
end
pubsub_subscribe(jid, pubsub_service, node, resource) click to toggle source
# File lib/ejabberd_rest/client.rb, line 116
def pubsub_subscribe(jid, pubsub_service, node, resource)
  post_stanza(subscribe_stanza(jid, pubsub_service, node, resource))
end
pubsub_subscribe_all_resources(jid, pubsub_service, node, resources) click to toggle source
# File lib/ejabberd_rest/client.rb, line 120
def pubsub_subscribe_all_resources(jid, pubsub_service, node, resources)
  @connection.in_parallel do
    resources.each do |r|
      pubsub_subscribe(jid, pubsub_service, node, r)
    end
  end
end
pubsub_unsubscribe(jid, pubsub_service, node, resource) click to toggle source
# File lib/ejabberd_rest/client.rb, line 138
def pubsub_unsubscribe(jid, pubsub_service, node, resource)
  post_stanza(unsubscribe_stanza(jid, pubsub_service, node, resource))
end
pubsub_unsubscribe_all_resources(jid, pubsub_service, node, resources) click to toggle source
# File lib/ejabberd_rest/client.rb, line 142
def pubsub_unsubscribe_all_resources(jid, pubsub_service, node, resources)
  @connection.in_parallel do
    resources.each do |r|
      pubsub_unsubscribe(jid, pubsub_service, node, r)
    end
  end
end
subscribe_stanza(jid, pubsub_service, node, resource) click to toggle source
# File lib/ejabberd_rest/client.rb, line 106
def subscribe_stanza(jid, pubsub_service, node, resource)
  stanza =  "<iq type='set' from='#{jid}' to='#{pubsub_service}' id='#{SecureRandom.uuid}'>"
  stanza <<   "<pubsub xmlns='http://jabber.org/protocol/pubsub'>"
  stanza <<     "<subscribe node='#{node}' jid='#{jid}/#{resource}' />"
  stanza <<   "</pubsub>"
  stanza << "</iq>"

  stanza
end
unsubscribe_stanza(jid, pubsub_service, node, resource) click to toggle source
# File lib/ejabberd_rest/client.rb, line 128
def unsubscribe_stanza(jid, pubsub_service, node, resource)
  stanza =  "<iq type='set' from='#{jid}' to='#{pubsub_service}' id='#{SecureRandom.uuid}'>"
  stanza <<   "<pubsub xmlns='http://jabber.org/protocol/pubsub'>"
  stanza <<     "<unsubscribe node='#{node}' jid='#{jid}/#{resource}' />"
  stanza <<   "</pubsub>"
  stanza << "</iq>"

  stanza
end

Private Instance Methods

post(path, options={}) click to toggle source
# File lib/ejabberd_rest/client.rb, line 153
def post(path, options={})
  @connection.post do |req|
    req.url path
    req.body = options[:body] if options[:body]
  end
end