module Polyseerio::Resource::Helper

Helper methods for building and working with resources

Constants

EID_REGEX

Public Class Methods

forward_self(func) click to toggle source

Forward self as first argument. Used for method procs.

# File lib/resource/helper.rb, line 8
def self.forward_self(func)
  proc do |*args|
    func.call(self, *args)
  end
end
get_eid_from_resource_path(path) click to toggle source

Given a request path an eid is returned or nil

# File lib/resource/helper.rb, line 15
def self.get_eid_from_resource_path(path)
  # TODO: consolidate with regex?
  if path.include? '/environments/name/'
    return path.split('/environments/name/').last
  end

  result = EID_REGEX.match path

  return result if result.nil?

  result[1]
end
parse_resource_response(response, cid) click to toggle source

Parse a resource response TODO: hard to unit-test (due to Factory.make)

# File lib/resource/helper.rb, line 63
def self.parse_resource_response(response, cid)
  body = response.body
  meta = body[:meta] || {}
  included = body[:included] || {}
  data = body[:data]

  eid = get_eid_from_resource_path response.request.uri.path

  # attach eid if we have attributes and an id
  # would need to occur for each?????
  data[:attributes][:eid] = eid if !eid.nil? && data.key?(:attributes)

  # if resource collection if so map return
  if resource_collection? data
    return data.map do |item|
      to_instance(item, meta, included, cid)
    end
  end

  # convert to instance
  to_instance(data, meta, included, cid)
end
parse_response(response, cid) click to toggle source

Parse a response

# File lib/resource/helper.rb, line 87
def self.parse_response(response, cid)
  if resource_response? response.body
    return parse_resource_response(response, cid)
  end

  response
end
resource?(data) click to toggle source

True if response data is a resource collection (alias)

# File lib/resource/helper.rb, line 40
def self.resource?(data)
  data.is_a? Hash
end
resource_collection?(data) click to toggle source

True if response data is a resource collection (alias)

# File lib/resource/helper.rb, line 35
def self.resource_collection?(data)
  data.is_a? Array
end
resource_response?(body) click to toggle source

True if the response is a resource based response

# File lib/resource/helper.rb, line 29
def self.resource_response?(body)
  body.key?(:data) &&
    (resource?(body[:data]) || resource_collection?(body[:data]))
end
to_instance(data, _meta, _included, cid) click to toggle source

Convert parsed response data into an instance TODO: hard to unit-test (due to Factory.make)

# File lib/resource/helper.rb, line 46
def self.to_instance(data, _meta, _included, cid)
  type = data.fetch(:type, nil)
  id = data.fetch(:id, nil)
  attributes = data.fetch(:attributes, {})

  attributes[:id] = id

  # parse_response would only eve be called once the client has been
  # constructed meaning in order to get the class we simply need to
  # pass the type and cid. make is memoized on the type and cid args.
  resource = Factory.make(type, nil, cid)

  resource.new(attributes) # TODO: need to include meta
end