class BookingSync::API::Resource

Attributes

_client[R]
_rels[R]
_resources_key[R]

Public Class Methods

new(client, data = {}, rels = {}, resources_key = nil) click to toggle source

Initialize a Resource with the given relations and data.

@param client [BookingSync::API::Client] The client that made the API request. @param data [Hash] Hash of key/value properties. @param rels [Hash] Hash of built relations for this resource. @param resources_key [Symbol|String] Key in response body under which

# File lib/bookingsync/api/resource.rb, line 14
def initialize(client, data = {}, rels = {}, resources_key = nil)
  @_client = client
  @_resources_key = resources_key
  data.each do |key, value|
    self[key.to_sym] = process_value(value)
  end
  @_rels = rels
end

Public Instance Methods

method_missing(method, *args) click to toggle source

Make associations accessible

@param method [Symbol] Name of association @param args [Array] Array of additional arguments

Calls superclass method
# File lib/bookingsync/api/resource.rb, line 40
def method_missing(method, *args)
  return self[method] if has_key?(method) # eager loaded with :include
  association_key = :"#{@_resources_key}.#{method}"
  if (polymorphic_association = find_polymorphic_association(self[:links], method))
    attributes = polymorphic_association.last
    ids, type = Array(attributes[:id]), attributes[:type]
    resolved_association_key = :"#{@_resources_key}.#{type.downcase}"
    uri_association_key = "#{association_key}.id"

    extract_resources(ids, resolved_association_key, uri_association_key, *args)
  elsif self[:links] && self[:links].has_key?(method)
    ids = Array(self[:links][method])
    extract_resources(ids, association_key, association_key, *args)
  else
    super
  end
end
process_value(value) click to toggle source

Process an individual value of this resource. Hashes get exploded into another Resource, and Arrays get their values processed too.

@param value [Object] An Object value of a Resource’s data. @return [Object] An Object to set as the value of a Resource key.

# File lib/bookingsync/api/resource.rb, line 28
def process_value(value)
  case value
  when Hash  then self.class.new(@_client, value)
  when Array then value.map { |v| process_value(v) }
  else value
  end
end
to_s() click to toggle source
# File lib/bookingsync/api/resource.rb, line 58
def to_s
  id.to_s
end

Private Instance Methods

extract_resources(ids, association_key, uri_association_key, *args) click to toggle source
# File lib/bookingsync/api/resource.rb, line 70
def extract_resources(ids, association_key, uri_association_key, *args)
  return [] if ids.empty?
  options = { uri: { uri_association_key => ids } }
  options.merge!(query: args.first) if args.first.is_a?(Hash)
  @_rels[association_key].get(options).resources
end
find_polymorphic_association(links, method) click to toggle source

links structure: {:taggable=>{:type=>“Article”, :id=>“15”}}

# File lib/bookingsync/api/resource.rb, line 65
def find_polymorphic_association(links, method)
  links.select { |_, data| data.is_a?(Hash) }
    .find { |assoc, _| assoc.to_s.downcase == method.to_s.downcase }
end