Class: FatZebra::APIResource

Inherits:
FatZebraObject show all
Includes:
APIHelper
Defined in:
lib/fat_zebra/api_resource.rb

Overview

FatZebra API Resource

Define the API requests methods

Instance Attribute Summary

Attributes inherited from FatZebraObject

#accepted

Attributes included from ObjectHelper

#data, #errors, #raw

Class Method Summary collapse

Methods included from APIHelper

included, #request, #resource_name, #resource_path

Methods inherited from FatZebraObject

initialize_from, #load_response_api, #update_from

Methods included from Validation

#errors, #valid!, #valid?, #validates

Methods included from ObjectHelper

#[], #[]=, #add_accessor, #add_accessors, #add_data, #initialize, #inspect, #keys, #method_missing, #remove_accessor, #to_hash, #to_json, #update_attributes

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FatZebra::ObjectHelper

Class Method Details

.base_pathObject



13
14
15
# File 'lib/fat_zebra/api_resource.rb', line 13

def base_path
  "#{configurations.api_version}/"
end

.request(method, path, payload = {}, options = {}) ⇒ Hash

Send a request to the API

rubocop:disable Metrics/AbcSize

Parameters:

  • method (Symbol)

    for the request

  • endpoint (String)

    for the request

  • options (Hash) (defaults to: {})
  • Payload (Hash)

Returns:

  • (Hash)

    response

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fat_zebra/api_resource.rb', line 28

def request(method, path, payload = {}, options = {})
  payload[:test] = true if configurations.test_mode
  payload        = Util.format_dates_in_hash(payload)
  url_params     = Util.encode_parameters(payload) if method == :get
  uri            = build_endpoint_url(configurations.gateway, path, url_params, http_secure: configurations.http_secure)

  request_options = Util.compact(
    method: method,
    url: uri.to_s,
    payload: payload,
    proxy: configurations.proxy,
    use_ssl: configurations.http_secure
  ).merge(authentication).merge(default_headers).merge(configurations.global_options).merge(options)

  Request.execute(request_options).body
rescue FatZebra::RequestError => e
  return e.http_body if e.http_status == 422

  raise
end