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_path(account) ⇒ Object



11
12
13
# File 'lib/fat_zebra/api_resource.rb', line 11

def base_path()
  "#{.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:



26
27
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 26

def request(method, path, payload = {}, options = {})
   =  options
  full_path = "/#{base_path }#{path}"

  payload[:test] = true if .test_mode
  payload        = Util.format_dates_in_hash(payload)
  url_params     = URI.encode_www_form(payload) if method == :get
  uri            = build_endpoint_url(.gateway, full_path, url_params, http_secure: .http_secure)

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

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