Class: FatZebra::APIResource

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

Overview

FatZebra API Resource

Define the API requests methods

Instance Attribute Summary

Attributes inherited from FatZebraObject

#data, #errors, #raw, #successful

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FatZebraObject

#[], #[]=, #add_data, #initialize, initialize_from, #keys, #load_response_api, #to_hash, #to_json, #update_attributes, #update_from

Methods included from Validation

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

Constructor Details

This class inherits a constructor from FatZebra::FatZebraObject

Dynamic Method Handling

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

Class Method Details

.class_nameString



50
51
52
# File 'lib/fat_zebra/api_resource.rb', line 50

def class_name
  name.split('::')[-1]
end

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

Send a request to the API

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fat_zebra/api_resource.rb', line 64

def request(method, path, payload = {}, options = {})
  payload[:test] = true if configurations.test_mode

  if method == :get
    url_params = !payload.empty? && Util.encode_parameters(payload)
    payload    = nil
  end

  uri = build_endpoint_url(path, url_params)

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

  request = Request.execute(request_options)

  request.body
end

.resource_nameString



44
45
46
# File 'lib/fat_zebra/api_resource.rb', line 44

def resource_name
  Util.underscore("#{class_name}s")
end

.resource_path(path = nil) ⇒ String



38
39
40
# File 'lib/fat_zebra/api_resource.rb', line 38

def resource_path(path = nil)
  "/#{configurations.api_version}/#{path || @resource_name || CGI.escape(resource_name)}"
end

Instance Method Details

#request(method, resource_path, payload = {}, options = {}) ⇒ Hash

Send a request to the API

Raises:



30
31
32
# File 'lib/fat_zebra/api_resource.rb', line 30

def request(method, resource_path, payload = {}, options = {})
  self.class.request(method, resource_path, payload, options)
end

#resource_nameString



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

def resource_name
  self.class.resource_name
end

#resource_path(path = nil) ⇒ String



16
17
18
# File 'lib/fat_zebra/api_resource.rb', line 16

def resource_path(path = nil)
  self.class.resource_path(path)
end