Module: ApiClient::InstanceMethods

Included in:
Base
Defined in:
lib/api-client/instance_methods.rb

Overview

This module handles the logic to make an api call and update_attributes the current object with the response.

Instance Method Summary collapse

Instance Method Details

#delete(header = {}) ⇒ Base Also known as: destroy

Make a delete requisition and update the object with the response.

Parameters:

  • header (Hash) (defaults to: {})

    hash with the header options.

Returns:

  • (Base)

    the object updated.


58
59
60
61
62
63
# File 'lib/api-client/instance_methods.rb', line 58

def delete(header = {})
  return self if ApiClient.config.mock
  url = "#{ApiClient.config.path[path]}#{self.class.resource_path}/#{id}"
  response = ApiClient::Dispatcher.delete(url, header)
  update(response, url)
end

#get(header = {}) ⇒ Base Also known as: reload

Make a get requisition and update the object with the response.

Parameters:

  • header (Hash) (defaults to: {})

    hash with the header options.

Returns:

  • (Base)

    the object updated.


8
9
10
11
12
13
# File 'lib/api-client/instance_methods.rb', line 8

def get(header = {})
  return self if ApiClient.config.mock
  url = "#{ApiClient.config.path[path]}#{self.class.resource_path}/#{id}"
  response = ApiClient::Dispatcher.get(url, header)
  update(response, url)
end

#patch(header = {}) ⇒ Base

Make a patch requisition and update the object with the response.

Parameters:

  • header (Hash) (defaults to: {})

    hash with the header options.

Returns:

  • (Base)

    the object updated.


47
48
49
50
51
52
# File 'lib/api-client/instance_methods.rb', line 47

def patch(header = {})
  return self if ApiClient.config.mock
  url = "#{ApiClient.config.path[path]}#{self.class.resource_path}/#{id}"
  response = ApiClient::Dispatcher.patch(url, self.to_hash, header)
  update(response, url)
end

#post(header = {}) ⇒ Base Also known as: create

Make a post requisition and update the object with the response.

Parameters:

  • header (Hash) (defaults to: {})

    hash with the header options.

Returns:

  • (Base)

    the object updated.


21
22
23
24
25
26
# File 'lib/api-client/instance_methods.rb', line 21

def post(header = {})
  return self if ApiClient.config.mock
  url = "#{ApiClient.config.path[path]}#{self.class.resource_path}"
  response = ApiClient::Dispatcher.post(url, self.to_hash, header)
  update(response, url)
end

#put(header = {}) ⇒ Base Also known as: update_attributes

Make a put requisition and update the object with the response.

Parameters:

  • header (Hash) (defaults to: {})

    hash with the header options.

Returns:

  • (Base)

    the object updated.


34
35
36
37
38
39
# File 'lib/api-client/instance_methods.rb', line 34

def put(header = {})
  return self if ApiClient.config.mock
  url = "#{ApiClient.config.path[path]}#{self.class.resource_path}/#{id}"
  response = ApiClient::Dispatcher.put(url, self.to_hash, header)
  update(response, url)
end

#remove_root(attributes = {}) ⇒ Hash

Removes the root node attribute if found.

Parameters:

  • attributes (Hash) (defaults to: {})

    the hash with attributes.

Returns:

  • (Hash)

    the hash with attributes without the root node.


71
72
73
74
75
# File 'lib/api-client/instance_methods.rb', line 71

def remove_root(attributes = {})
  attributes = attributes[self.class.root_node.to_sym] if attributes.key?(self.class.root_node.to_sym)
  attributes = attributes[self.class.root_node.to_s] if attributes.key?(self.class.root_node.to_s)
  attributes
end