Module: ApiClient::ClassMethods

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

Overview

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

Instance Method Summary collapse

Instance Method Details

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

Make a delete requisition and initialize an object with the response.

Parameters:

  • id (Integer)

    id of the object.

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

    hash with the header options.

Returns:

  • (Base)

    the object initialized.



63
64
65
66
67
68
# File 'lib/api-client/class_methods.rb', line 63

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

#get(id, header = {}) ⇒ Base Also known as: find

Make a get requisition and initialize an object with the response.

Parameters:

  • id (Integer)

    id of the object.

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

    hash with the header options.

Returns:

  • (Base)

    the object initialized.



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

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

#patch(id, attributes, header = {}) ⇒ Base

Make a patch requisition and initialize an object with the response.

Parameters:

  • attributes (Hash)

    hash with the attributes to send.

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

    hash with the header options.

Returns:

  • (Base)

    the object initialized.



51
52
53
54
55
56
# File 'lib/api-client/class_methods.rb', line 51

def patch(id, attributes, header = {})
  return new(attributes) if ApiClient.config.mock
  url = "#{ApiClient.config.path[path]}#{self.resource_path}/#{id}"
  response = ApiClient::Dispatcher.patch(url, { self.root_node.to_sym => attributes }, header)
  build(response, url)
end

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

Make a post requisition and initialize an object with the response.

Parameters:

  • attributes (Hash)

    hash with the attributes to send.

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

    hash with the header options.

Returns:

  • (Base)

    the object initialized.



23
24
25
26
27
28
# File 'lib/api-client/class_methods.rb', line 23

def post(attributes, header = {})
  return new(attributes) if ApiClient.config.mock
  url = "#{ApiClient.config.path[path]}#{self.resource_path}"
  response = ApiClient::Dispatcher.post(url, { self.root_node.to_sym => attributes }, header)
  build(response, url)
end

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

Make a put requisition and initialize an object with the response.

Parameters:

  • attributes (Hash)

    hash with the attributes to send.

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

    hash with the header options.

Returns:

  • (Base)

    the object initialized.



37
38
39
40
41
42
# File 'lib/api-client/class_methods.rb', line 37

def put(id, attributes, header = {})
  return new(attributes) if ApiClient.config.mock
  url = "#{ApiClient.config.path[path]}#{self.resource_path}/#{id}"
  response = ApiClient::Dispatcher.put(url, { self.root_node.to_sym => attributes }, header)
  build(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.



76
77
78
79
80
# File 'lib/api-client/class_methods.rb', line 76

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