Class: ILORb

Inherits:
Object
  • Object
show all
Defined in:
lib/ilorb.rb,
lib/ilorb/ribcl.rb,
lib/ilorb/version.rb

Overview

just version

Defined Under Namespace

Classes: RIBCL

Constant Summary collapse

VERSION =
"0.0.4"

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ ILORb

Returns a new instance of ILORb.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ilorb.rb', line 11

def initialize(config = {})
  @hostname = config[:hostname]
  @password = config[:password]
  @login = config.fetch(:login, "Administrator")
  @port = config.fetch(:port, 443)
  @protocol = config.fetch(:protocol, :http)
  @verify_ssl = config.fetch(:verify_ssl, false)
  @definitions_path = config.fetch(:definitions_path, File.join(File.dirname(__FILE__), "ilorb/definitions"))
  @ribcl_path = config.fetch(:ribcl_path, "/ribcl")

  @log = Logger.new(STDOUT)
  @log.level = config[:debug] ? Logger::DEBUG : Logger::WARN

  @nori = Nori.new(convert_tags_to: ->(tag) { tag.downcase.to_sym })

  setup_commands
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

args should be empty or contain a hash anytime



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ilorb.rb', line 30

def method_missing(name, *args, &block)
  if @ribcl.command?(name)
    command = @ribcl.command(name)

    fail RIBCL::NotImplementedError, "#{name} is not supported" unless command.supported?
    @log.info("Calling method #{name}")

    params = args.first || {}
    attributes = {}

    command.get_attributes.each do |attr|
      # Attributes are mandatory
      fail "Attribute #{attr} missing in #{name} call" unless params.key?(attr)
      attributes.store(attr, @ribcl.encode(params.delete(attr)))
    end

    if !command.get_elements.empty?
      element_map = command.map_elements

      elements_array = [params].flatten.map do |params_hash|
        Hash[params_hash.map { |k, _| [k, @ribcl.encode(params_hash.delete(k))] if element_map.key?(k) }.compact]
      end

      # TODO: check for CDATA

      request = ribcl_request(command, attributes) do |xml|
        elements_array.each do |elements_hash|
          elements_hash.each do |key, value|
            elt = command.get_elements[element_map[key].first]
            if elt.is_a?(Array)
              attrs = Hash[elt.map { |x| [x, elements_hash.delete(element_map.invert[[element_map[key].first, x]])] }]
            else
              attrs = { element_map[key].last => value }
            end
            xml.send(element_map[key].first, attrs)
          end
        end
      end
    elsif !command.get_text.nil?
      if (text = params[command.get_text])
        request = ribcl_request(command, text, attributes)
      end
    else
      request = ribcl_request(command, attributes)
    end

    response = send_request(request)
    parse_response(response)
  else
    super
  end
end

Instance Method Details

#respond_to(name) ⇒ Object



83
84
85
# File 'lib/ilorb.rb', line 83

def respond_to(name)
  @ribcl.command?(name) ? true : super
end

#supported_commandsObject



87
88
89
# File 'lib/ilorb.rb', line 87

def supported_commands
  @ribcl.select { |_, command| command.supported? }.keys
end