class Archipel::Api::Internal::Api

Public Class Methods

new(config) click to toggle source
# 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

Public Instance Methods

call(xml, to = @config[:hypervisor]) click to toggle source
# 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() { |client| ... } click to toggle source
# 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) click to toggle source
# 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

Protected Instance Methods

debug(text) click to toggle source
# File lib/archipel/api/internal/api.rb, line 67
def debug text
  return unless debug?
  $stderr.puts
  $stderr.puts text
  $stderr.puts
end

Private Instance Methods

debug?() click to toggle source
# File lib/archipel/api/internal/api.rb, line 75
def debug?
  ['true', true].include? @config[:debug]
end