Class: Archipel::Api::Internal::Api
- Inherits:
-
Object
- Object
- Archipel::Api::Internal::Api
- Defined in:
- lib/archipel/api/internal/api.rb
Direct Known Subclasses
Instance Method Summary collapse
- #call(xml, to = @config[:hypervisor]) ⇒ Object
- #in_connection ⇒ Object
-
#initialize(config) ⇒ Api
constructor
A new instance of Api.
- #wait_for_reply(timeout, client) ⇒ Object
Constructor Details
#initialize(config) ⇒ Api
Returns a new instance of Api.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/archipel/api/internal/api.rb', line 9 def initialize config config = config.merge Archipel::Api.get_defaults @config = config config_missing = %i(login password server hypervisor).any? do |property| config[property].nil? end if config_missing raise "Login, password, server or hypervisor not provided. " + "Use Archipel.defaults or provide constructor parameters to #{self.class.name}." end end |
Instance Method Details
#call(xml, to = @config[:hypervisor]) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/archipel/api/internal/api.rb', line 23 def call xml, to = @config[:hypervisor] debug xml output = in_connection do |client| iq = Jabber::Iq.new :set, to iq.add REXML::Document.new(xml).elements[1] client.send iq wait_for_reply 60.seconds, client end ret = XmlSimple.xml_in output raise Exception, ret['error'][0]['text'][0] if ret['error'] ret end |
#in_connection ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/archipel/api/internal/api.rb', line 38 def in_connection Jabber.debug = ['true', true].include? @config[:xmpp_debug] client = Jabber::Client.new Jabber::JID.new @config[:login] client.connect @config[:server] client.auth @config[:password] ret = yield client client.close ret end |
#wait_for_reply(timeout, client) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/archipel/api/internal/api.rb', line 48 def wait_for_reply timeout, client output = nil mutex = Mutex.new condition = ConditionVariable.new client.add_stanza_callback do |stanza| next unless %w(result error).include? stanza.attributes['type'] output = stanza.to_s debug output mutex.synchronize { condition.signal } end mutex.synchronize { condition.wait mutex, timeout } raise StandardError, "No response given in #{timeout} seconds" unless output output end |