Class: ApiNavigator::Resources::MemberResource

Inherits:
ApiNavigator::Resource show all
Defined in:
lib/api_navigator/resources/member_resource.rb

Instance Attribute Summary collapse

Attributes inherited from ApiNavigator::Resource

#_links, #_response

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApiNavigator::Resource

#[], #_self_link, #_status, #_success?, #fetch, #inspect, #validate

Constructor Details

#initialize(representation, entry_point, response = nil) ⇒ MemberResource

Initializes a Resource.

Parameters:

  • representation

    The hash with the HAL representation of the Resource.

  • entry_point

    The EntryPoint object to inject the configutation.



22
23
24
25
26
# File 'lib/api_navigator/resources/member_resource.rb', line 22

def initialize(representation, entry_point, response = nil)
  super

  @_attributes = Attributes.new(representation.fetch('data'))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delegate the method to various elements of the resource.

This allows ‘api.posts` instead of `api.links.posts.resource` as well as api.posts(id: 1) assuming posts is a link.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/api_navigator/resources/member_resource.rb', line 32

def method_missing(method, *args, &block)
  if @_attributes.include?(method.to_s)
    result = @_attributes[method.to_s]

    if representation = resource_representation(result)
      Resource.from_representation(representation, @_entry_point)
    else
      result
    end
  else
    super
  end
end

Instance Attribute Details

#_attributesObject (readonly)

Returns the attributes of the Resource as Attributes.



6
7
8
# File 'lib/api_navigator/resources/member_resource.rb', line 6

def _attributes
  @_attributes
end

Class Method Details

.from_representation(representation, entry_point, response = nil) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/api_navigator/resources/member_resource.rb', line 9

def from_representation(representation, entry_point, response = nil)
  client_identifier = entry_point.client_identifier
  identifier        = representation.fetch('_meta',{})['type']
  resource_class    = ApiNavigator.resource_class(identifier, client_identifier: client_identifier)

  resource_class.new(representation, entry_point, response)
end

Instance Method Details

#resource_representation(data) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/api_navigator/resources/member_resource.rb', line 53

def resource_representation(data)
  return data if (data.kind_of?(Hash) && !data["data"].nil?)

  return { 'data' => data } if (data.kind_of?(Array) && resource_representation(data.first))

  nil
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Accessory method to allow the resource respond to methods that will hit method_missing.

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/api_navigator/resources/member_resource.rb', line 48

def respond_to_missing?(method, include_private = false)
  @_attributes.include?(method.to_s) ||
    super
end