Class: BookingSync::API::Resource

Inherits:
Hash
  • Object
show all
Includes:
Hashie::Extensions::MethodAccess
Defined in:
lib/bookingsync/api/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data = {}, rels = {}, resources_key = nil) ⇒ Resource

Initialize a Resource with the given relations and data.

Parameters:

  • client (BookingSync::API::Client)

    The client that made the API request.

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

    Hash of key/value properties.

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

    Hash of built relations for this resource.

  • resources_key (Symbol|String) (defaults to: nil)

    Key in response body under which



14
15
16
17
18
19
20
21
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Make associations accessible

Parameters:

  • method (Symbol)

    Name of association

  • args (Array)

    Array of additional arguments



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

Instance Attribute Details

#_clientObject (readonly)

Returns the value of attribute _client.



6
7
8
# File 'lib/bookingsync/api/resource.rb', line 6

def _client
  @_client
end

#_relsObject (readonly)

Returns the value of attribute _rels.



6
7
8
# File 'lib/bookingsync/api/resource.rb', line 6

def _rels
  @_rels
end

#_resources_keyObject (readonly)

Returns the value of attribute _resources_key.



6
7
8
# File 'lib/bookingsync/api/resource.rb', line 6

def _resources_key
  @_resources_key
end

Instance Method Details

#process_value(value) ⇒ Object

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

Parameters:

  • value (Object)

    An Object value of a Resource’s data.

Returns:

  • (Object)

    An Object to set as the value of a Resource key.



28
29
30
31
32
33
34
# 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_sObject



58
59
60
# File 'lib/bookingsync/api/resource.rb', line 58

def to_s
  id.to_s
end