Class: EjabberdRest::Client
- Inherits:
-
Object
- Object
- EjabberdRest::Client
- Defined in:
- lib/ejabberd_rest/client.rb
Constant Summary collapse
- DEFAULT_MOD_REST_URL =
"http://localhost:5285"
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#max_concurrency ⇒ Object
Returns the value of attribute max_concurrency.
-
#mod_rest_url ⇒ Object
Returns the value of attribute mod_rest_url.
Instance Method Summary collapse
- #add_user(username, domain, password) ⇒ Object
- #delete_user(username, domain) ⇒ Object
-
#initialize(attributes = {}) ⇒ Client
constructor
A new instance of Client.
- #modify_affiliations(from_jid, host, node, affiliations = {}) ⇒ Object
- #post_all_stanzas(stanzas) ⇒ Object
- #post_stanza(stanza) ⇒ Object
- #pubsub_delete_node(from_jid, host, node) ⇒ Object
- #pubsub_item_stanza(from_jid, host, node, message) ⇒ Object
- #pubsub_publish(from_jid, host, node, message) ⇒ Object
- #pubsub_publish_all_items(from_jid, host, node, items) ⇒ Object
- #pubsub_subscribe(jid, pubsub_service, node, resource) ⇒ Object
- #pubsub_subscribe_all_resources(jid, pubsub_service, node, resources) ⇒ Object
- #pubsub_unsubscribe(jid, pubsub_service, node, resource) ⇒ Object
- #pubsub_unsubscribe_all_resources(jid, pubsub_service, node, resources) ⇒ Object
- #subscribe_stanza(jid, pubsub_service, node, resource) ⇒ Object
- #unsubscribe_stanza(jid, pubsub_service, node, resource) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ejabberd_rest/client.rb', line 10 def initialize(attributes={}) mod_rest_url = attributes[:url] || DEFAULT_MOD_REST_URL debug = attributes[:debug] || false max_concurrency = attributes[:max_concurrency] || 100 manager = Typhoeus::Hydra.new(max_concurrency: 100) @connection = Faraday.new(mod_rest_url, parallel_manager: manager) do |builder| builder.request :url_encoded builder.response :logger if debug builder.adapter :typhoeus end end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
6 7 8 |
# File 'lib/ejabberd_rest/client.rb', line 6 def debug @debug end |
#max_concurrency ⇒ Object
Returns the value of attribute max_concurrency.
6 7 8 |
# File 'lib/ejabberd_rest/client.rb', line 6 def max_concurrency @max_concurrency end |
#mod_rest_url ⇒ Object
Returns the value of attribute mod_rest_url.
6 7 8 |
# File 'lib/ejabberd_rest/client.rb', line 6 def mod_rest_url @mod_rest_url end |
Instance Method Details
#add_user(username, domain, password) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ejabberd_rest/client.rb', line 23 def add_user(username, domain, password) response = post("/rest", body: "register #{username} #{domain} #{password}") if response.body.include?("successfully registered") true else if response.body.include?("already registered") raise Error::UserAlreadyRegistered else false end end end |
#delete_user(username, domain) ⇒ Object
37 38 39 |
# File 'lib/ejabberd_rest/client.rb', line 37 def delete_user(username, domain) post("/rest", body: "unregister #{username} #{domain}") end |
#modify_affiliations(from_jid, host, node, affiliations = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ejabberd_rest/client.rb', line 53 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) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/ejabberd_rest/client.rb', line 45 def post_all_stanzas(stanzas) @connection.in_parallel do stanzas.each do |stanza| post_stanza(stanza) end end end |
#post_stanza(stanza) ⇒ Object
41 42 43 |
# File 'lib/ejabberd_rest/client.rb', line 41 def post_stanza(stanza) post("/rest", body: stanza) end |
#pubsub_delete_node(from_jid, host, node) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/ejabberd_rest/client.rb', line 67 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) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ejabberd_rest/client.rb', line 77 def pubsub_item_stanza(from_jid, host, node, ) 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 << "#{}" stanza << "</item>" stanza << "</publish>" stanza << "</pubsub>" stanza << "</iq>" stanza end |
#pubsub_publish(from_jid, host, node, message) ⇒ Object
91 92 93 |
# File 'lib/ejabberd_rest/client.rb', line 91 def pubsub_publish(from_jid, host, node, ) post_stanza(pubsub_item_stanza(from_jid, host, node, )) end |
#pubsub_publish_all_items(from_jid, host, node, items) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/ejabberd_rest/client.rb', line 95 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) ⇒ Object
113 114 115 |
# File 'lib/ejabberd_rest/client.rb', line 113 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) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/ejabberd_rest/client.rb', line 117 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) ⇒ Object
135 136 137 |
# File 'lib/ejabberd_rest/client.rb', line 135 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) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/ejabberd_rest/client.rb', line 139 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) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/ejabberd_rest/client.rb', line 103 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) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/ejabberd_rest/client.rb', line 125 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 |