class ManageIQ::API::Client::Resource

Constants

CUSTOM_INSPECT_EXCLUSIONS

Attributes

actions[R]
attributes[R]
collection[R]

Public Class Methods

new(collection, resource_hash) click to toggle source
# File lib/manageiq/api/client/resource.rb, line 26
def initialize(collection, resource_hash)
  raise "Cannot instantiate a Resource directly" if instance_of?(Resource)
  @collection = collection
  @attributes = resource_hash.except("actions")
  add_href
  fetch_actions(resource_hash)
end
subclass(name) click to toggle source
# File lib/manageiq/api/client/resource.rb, line 10
def self.subclass(name)
  name = name.classify

  if const_defined?(name, false)
    const_get(name, false)
  else
    const_set(name, Class.new(self))
  end
end

Public Instance Methods

[](attr) click to toggle source
# File lib/manageiq/api/client/resource.rb, line 34
def [](attr)
  attr_str = attr.to_s
  attributes[attr_str] if attributes.key?(attr_str)
end

Private Instance Methods

add_href() click to toggle source

Let's add href's here if not yet defined by the server

# File lib/manageiq/api/client/resource.rb, line 70
def add_href
  return if attributes.key?("href")
  return unless attributes.key?("id")
  attributes["href"] = client.connection.api_path("#{collection.name}/#{attributes['id']}")
end
exec_action(name, args = nil, &block) click to toggle source
# File lib/manageiq/api/client/resource.rb, line 56
def exec_action(name, args = nil, &block)
  args ||= {}
  raise "Action #{name} parameters must be a hash" if !args.kind_of?(Hash)
  action = find_action(name)
  res = client.send(action.method, URI(action.href)) do
    body = { "action" => action.name }
    resource = args.dup
    resource.merge!(block.call) if block
    resource.present? ? body.merge("resource" => resource) : body
  end
  action_result(res)
end
method_missing(sym, *args, &block) click to toggle source
Calls superclass method
# File lib/manageiq/api/client/resource.rb, line 41
def method_missing(sym, *args, &block)
  reload_actions unless actions_present?
  if attributes.key?(sym.to_s)
    attributes[sym.to_s]
  elsif action_defined?(sym)
    exec_action(sym, *args, &block)
  else
    super
  end
end
reload_actions() click to toggle source
# File lib/manageiq/api/client/resource.rb, line 76
def reload_actions
  return unless attributes.key?("href")
  resource_hash = client.get(attributes["href"])
  @attributes = resource_hash.except("actions")
  fetch_actions(resource_hash)
end
respond_to_missing?(sym, *_) click to toggle source
Calls superclass method
# File lib/manageiq/api/client/resource.rb, line 52
def respond_to_missing?(sym, *_)
  attributes.key?(sym.to_s) || action_defined?(sym) || super
end