Class: Hawkei::APIResource

Inherits:
HawkeiObject show all
Defined in:
lib/hawkei/api_resource.rb

Overview

Hawkei API Resource

Define the API requests methods

Direct Known Subclasses

Batch, Watcher

Instance Attribute Summary

Attributes inherited from HawkeiObject

#data, #errors, #raw, #successful

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HawkeiObject

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

Constructor Details

This class inherits a constructor from Hawkei::HawkeiObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hawkei::HawkeiObject

Class Method Details

.class_nameString

Returns the class name.

Returns:

  • (String)

    the class name



49
50
51
# File 'lib/hawkei/api_resource.rb', line 49

def class_name
  name.split('::')[-1]
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:



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

def request(method, path, payload = {}, options = {})
  payload[:sent_at] = Time.now.utc.iso8601(3)
  payload    = Util.deep_obfuscate_value(payload, configurations.obfuscated_fields)
  url_params = Util.encode_parameters(payload) if method == :get
  uri        = build_endpoint_url(path, url_params)

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

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

  raise
end

.resource_nameString

Returns resource name.

Returns:

  • (String)

    resource name



43
44
45
# File 'lib/hawkei/api_resource.rb', line 43

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

.resource_path(path = nil) ⇒ String

Returns resource path.

Returns:

  • (String)

    resource path



37
38
39
# File 'lib/hawkei/api_resource.rb', line 37

def resource_path(path = nil)
  "/api/#{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

Parameters:

  • method (Symbol)

    for the request

  • endpoint (String)

    for the request

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

Returns:

  • (Hash)

    response

Raises:



29
30
31
# File 'lib/hawkei/api_resource.rb', line 29

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

#resource_nameString

Returns resource name.

Returns:

  • (String)

    resource name



9
10
11
# File 'lib/hawkei/api_resource.rb', line 9

def resource_name
  self.class.resource_name
end

#resource_path(path = nil) ⇒ String

Returns resource path.

Returns:

  • (String)

    resource path



15
16
17
# File 'lib/hawkei/api_resource.rb', line 15

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