Class: ForemanApi::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_api/base.rb

Constant Summary collapse

API_VERSION =
"2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ Base

Returns a new instance of Base.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/foreman_api/base.rb', line 33

def initialize(config, options = {})
  config = config.dup
  self.logger = config.delete(:logger)

  headers = {
    :content_type => 'application/json',
    :accept       => "application/json;version=#{API_VERSION}"
  }
  headers.merge!(config[:headers]) unless config[:headers].nil?
  headers.merge!(options.delete(:headers)) unless options[:headers].nil?

  resource_config = {
    :user     => config[:username],
    :password => config[:password],
    :oauth    => config[:oauth],
    :headers  => headers
  }.merge(options)

  @client = RestClient::Resource.new(config[:base_url], resource_config)
  @config = config
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



31
32
33
# File 'lib/foreman_api/base.rb', line 31

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



31
32
33
# File 'lib/foreman_api/base.rb', line 31

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



31
32
33
# File 'lib/foreman_api/base.rb', line 31

def logger
  @logger
end

Class Method Details

.docObject

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/foreman_api/base.rb', line 81

def self.doc
  raise NotImplementedError
end

.method_doc(method) ⇒ Object



89
90
91
# File 'lib/foreman_api/base.rb', line 89

def self.method_doc(method)
  method_docs[method.to_s]
end

.validation_hash(method) ⇒ Object



85
86
87
# File 'lib/foreman_api/base.rb', line 85

def self.validation_hash(method)
  validation_hashes[method.to_s]
end

Instance Method Details

#http_call(http_method, path, params = { }, headers = { }) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/foreman_api/base.rb', line 64

def http_call(http_method, path, params = { }, headers = { })
  headers ||= { }

  args = [http_method]
  if %w[post put].include?(http_method.to_s)
    args << params.to_json
  else
    headers[:params] = params if params
  end

  logger.info "#{http_method.upcase} #{path}"
  logger.debug "Params: #{params.inspect}"
  logger.debug "Headers: #{headers.inspect}"
  args << headers if headers
  process_data client[path].send(*args)
end

#perform_call(method_name, params, headers) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/foreman_api/base.rb', line 55

def perform_call(method_name, params, headers)
  method_doc = self.class.method_doc(method_name)
  check_params params, :allowed => method_doc['params'].any?, :method => method_name
  method_apis = method_doc['apis']
  api = find_suitable_api_call(method_apis, params)
  url, params = fill_params_in_url api['api_url'], params
  return http_call(api['http_method'].downcase, url, params, headers)
end

#validate_params!(params, rules) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/foreman_api/base.rb', line 93

def validate_params!(params, rules)
  return unless params.is_a?(Hash)

  invalid_keys = params.keys.map(&:to_s) - (rules.is_a?(Hash) ? rules.keys : rules)
  raise ArgumentError, "Invalid keys: #{invalid_keys.join(", ")}" unless invalid_keys.empty?

  if rules.is_a? Hash
    rules.each do |key, sub_keys|
      validate_params!(params[key], sub_keys) if params[key]
    end
  end
end