class CorePro::Resource
Base endpoint resources class
Public Class Methods
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
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
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
Extracts the error message from the response
@param response [HTTP::Response] the server response @param parsed_response [Object] the parsed server response
@return [String]
# File lib/core_pro.rb, line 93 def self.extract_error(response, parsed_response) parsed_response&.fetch('errors', nil) || super(response, parsed_response) end
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
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
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
Helper to reload a resource
@return [CorePro::Resource]
# File lib/core_pro.rb, line 83 def reload self.class.find(send(resource_id)) end
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