Class: ManageIQClient::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/manage_iq_client/base.rb

Constant Summary collapse

DEFAULT_HEADERS =
{ accept: :json, content_type: :json }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



11
12
13
14
15
16
17
# File 'app/models/manage_iq_client/base.rb', line 11

def initialize
  setup_args = [ManageIQClient.host]
  setup_hash = { verify_ssl: ManageIQClient.verify_ssl }
  setup_hash.merge! user: self.class.user, password: self.class.password unless ManageIQClient.auth_token
  setup_args += [setup_hash]
  @client = RestClient::Resource.new(*setup_args)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'app/models/manage_iq_client/base.rb', line 6

def client
  @client
end

Instance Method Details

#destroy(resource) ⇒ Object



39
40
41
# File 'app/models/manage_iq_client/base.rb', line 39

def destroy(resource)
  client[resource.path].delete headers
end

#find(path, params = {}) ⇒ Object



43
44
45
# File 'app/models/manage_iq_client/base.rb', line 43

def find(path, params = {})
  ActiveSupport::JSON.decode(client[path_and_params path, params].get headers)
end

#headersObject



19
20
21
22
23
# File 'app/models/manage_iq_client/base.rb', line 19

def headers
  headers = DEFAULT_HEADERS
  headers.merge! 'X-Auth-Token' => ManageIQClient.auth_token if ManageIQClient.auth_token
  headers
end

#path_and_params(path, params) ⇒ Object



47
48
49
# File 'app/models/manage_iq_client/base.rb', line 47

def path_and_params(path, params)
  params.nil? ? path : [path, params.to_param].join('?')
end

#save(resource) ⇒ Object



25
26
27
28
29
30
# File 'app/models/manage_iq_client/base.rb', line 25

def save(resource)
  path = resource.class.collection_path
  payload = resource.to_payload
  payload.merge! files: resource.files if resource.respond_to?(:files) && resource.files
  send_payload path, 'add', payload
end

#update(resource) ⇒ Object



32
33
34
35
36
37
# File 'app/models/manage_iq_client/base.rb', line 32

def update(resource)
  path = resource.resource_pathh
  payload = resource.to_payload
  payload.merge! files: resource.files if resource.respond_to?(:files) && resource.files
  send_payload path, 'edit', payload
end