module ApiClient::ClassMethods

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

Public Instance Methods

create(attributes, header = {})
Alias for: post
delete(id, header = {}) click to toggle source

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

@param [Integer] id id of the object. @param [Hash] header hash with the header options. @return [Base] the object initialized.

# 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
Also aliased as: destroy
destroy(id, header = {})
Alias for: delete
find(id, header = {})
Alias for: get
get(id, header = {}) click to toggle source

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

@param [Integer] id id of the object. @param [Hash] header hash with the header options. @return [Base] the object initialized.

# 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
Also aliased as: find
patch(id, attributes, header = {}) click to toggle source

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

@param [Hash] attributes hash with the attributes to send. @param [Hash] header hash with the header options. @return [Base] the object initialized.

# 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 = {}) click to toggle source

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

@param [Hash] attributes hash with the attributes to send. @param [Hash] header hash with the header options. @return [Base] the object initialized.

# 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
Also aliased as: create
put(id, attributes, header = {}) click to toggle source

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

@param [Hash] attributes hash with the attributes to send. @param [Hash] header hash with the header options. @return [Base] the object initialized.

# 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
Also aliased as: update_attributes
remove_root(attributes = {}) click to toggle source

Removes the root node attribute if found.

@param [Hash] attributes the hash with attributes. @return [Hash] the hash with attributes without the root node.

# 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
update_attributes(id, attributes, header = {})
Alias for: put

Protected Instance Methods

build(response, url) click to toggle source

Initialize an object based on a hash of attributes.

@param [Response] response requisition response. @param [String] url the url of the requisition. @return [Base] the object initialized.

# File lib/api-client/class_methods.rb, line 89
def build(response, url)
  return response if ApiClient.config.hydra
  attributes = ApiClient::Parser.response(response, url)
  hash = remove_root(attributes)
  hash = hash.merge({ 'response' => attributes })
  new(hash)
end