Class: CorePro::Resource

Inherits:
OpenStruct
  • Object
show all
Extended by:
HTTP::RestClient::DSL
Defined in:
lib/core_pro.rb

Overview

Base endpoint resources class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(filters = [], params = {}) ⇒ Array

Resource collection finder, uses the default limit

Parameters:

  • filters (Array) (defaults to: [])

    URI filters to pass to the endpoint.

  • params (Hash) (defaults to: {})

    URI parameters to pass to the endpoint.

Returns:

  • (Array)

    of [Object] instances



27
28
29
# File 'lib/core_pro.rb', line 27

def self.all(filters = [], params = {})
  objectify(request(:get, uri(:list, *filters), params: params))
end

.create(params = {}) ⇒ Object

Resource creation helper

Parameters:

  • params (Hash) (defaults to: {})

    request parameters to pass to the endpoint as JSON.

Returns:

  • (Object)

    instance



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

def self.create(params = {})
  objectify(request(:post, uri(:create), json: params))
end

.error_response?(response, parsed_response) ⇒ TrueClass

Validate error response

Looks at the response code by default.

Parameters:

  • response (HTTP::Response)

    the server response

  • parsed_response (Object)

    the parsed server response

Returns:

  • (TrueClass)

    if status code is not a successful standard value



105
106
107
108
# File 'lib/core_pro.rb', line 105

def self.error_response?(response, parsed_response)
  errors = parsed_response&.fetch('errors', nil) || []
  !(200..299).cover?(response.code) || errors.any?
end

.extract_error(response, parsed_response) ⇒ String

Extracts the error message from the response

Parameters:

  • response (HTTP::Response)

    the server response

  • parsed_response (Object)

    the parsed server response

Returns:

  • (String)


93
94
95
# File 'lib/core_pro.rb', line 93

def self.extract_error(response, parsed_response)
  parsed_response&.fetch('errors', nil) || super(response, parsed_response)
end

.find(*id_or_ids) ⇒ Object

Resource finder

Parameters:

  • id (String)

    resource indentifier

  • params (Hash)

    URI parameters to pass to the endpoint.

Returns:

  • (Object)

    instance



36
37
38
# File 'lib/core_pro.rb', line 36

def self.find(*id_or_ids)
  objectify(request(:get, uri(:get, *id_or_ids)))
end

.objectify(payload) ⇒ Object

Resource constructor wrapper

Parameters:

  • payload (Hash)

    response payload to build a resource.

Returns:

  • (Object)

    instance or a list of instances.



61
62
63
64
65
66
67
68
69
# File 'lib/core_pro.rb', line 61

def self.objectify(payload)
  if payload.is_a?(Hash) && payload['data'].is_a?(Array)
    return payload['data'].map { |data| new(data) }
  end

  return new(payload['data']) if payload&.fetch('data', nil).is_a?(Hash)

  payload
end

.update(params = {}) ⇒ Object

Resource update helper

Parameters:

  • id (String)

    resource indentifier

  • params (Hash) (defaults to: {})

    request parameters to pass to the endpoint as JSON.

Returns:

  • (Object)

    instance



53
54
55
# File 'lib/core_pro.rb', line 53

def self.update(params = {})
  objectify(request(:post, uri(:update), json: params))
end

Instance Method Details

#reloadCorePro::Resource

Helper to reload a resource

Returns:



83
84
85
# File 'lib/core_pro.rb', line 83

def reload
  self.class.find(send(resource_id))
end

#resource_idString

Returns the ID name based on the resource type

Returns:

  • (String)


74
75
76
77
78
# File 'lib/core_pro.rb', line 74

def resource_id
  resource_name = self.class.name.to_s.split('::').last
  resource_name[0] = resource_name[0].downcase
  "#{resource_name}Id"
end