class CorePro::Resource

Base endpoint resources class

Public Class Methods

all(filters = [], params = {}) click to toggle source

Resource collection finder, uses the default limit

@param filters [Array] URI filters to pass to the endpoint. @param params [Hash] URI parameters to pass to the endpoint. @return [Array] of [Object] instances

# File lib/core_pro.rb, line 27
def self.all(filters = [], params = {})
  objectify(request(:get, uri(:list, *filters), params: params))
end
create(params = {}) click to toggle source

Resource creation helper

@param params [Hash] request parameters to pass to the endpoint as JSON. @return [Object] instance

# File lib/core_pro.rb, line 44
def self.create(params = {})
  objectify(request(:post, uri(:create), json: params))
end
error_response?(response, parsed_response) click to toggle source

Validate error response

Looks at the response code by default.

@param response [HTTP::Response] the server response @param parsed_response [Object] the parsed server response

@return [TrueClass] if status code is not a successful standard value

# 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) click to toggle source

Extracts the error message from the response

@param response [HTTP::Response] the server response @param parsed_response [Object] the parsed server response

@return [String]

Calls superclass method
# 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) click to toggle source

Resource finder

@param id [String] resource indentifier @param params [Hash] URI parameters to pass to the endpoint. @return [Object] instance

# File lib/core_pro.rb, line 36
def self.find(*id_or_ids)
  objectify(request(:get, uri(:get, *id_or_ids)))
end
objectify(payload) click to toggle source

Resource constructor wrapper

@param payload [Hash] response payload to build a resource. @return [Object] instance or a list of instances.

# 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 = {}) click to toggle source

Resource update helper

@param id [String] resource indentifier @param params [Hash] request parameters to pass to the endpoint as JSON. @return [Object] instance

# File lib/core_pro.rb, line 53
def self.update(params = {})
  objectify(request(:post, uri(:update), json: params))
end

Public Instance Methods

reload() click to toggle source

Helper to reload a resource

@return [CorePro::Resource]

# File lib/core_pro.rb, line 83
def reload
  self.class.find(send(resource_id))
end
resource_id() click to toggle source

Returns the ID name based on the resource type

@return [String]

# 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