Class: ApiNavigator::Resources::CollectionResource

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

Instance Attribute Summary collapse

Attributes inherited from ApiNavigator::Resource

#_links, #_response

Instance Method Summary collapse

Methods inherited from ApiNavigator::Resource

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

Constructor Details

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

Initializes a Resource.

Parameters:

  • representation

    The hash with the HAL representation of the Resource.

  • entry_point

    The EntryPoint object to inject the configutation.



13
14
15
16
17
18
# File 'lib/api_navigator/resources/collection_resource.rb', line 13

def initialize(representation, entry_point, response = nil)
  super
  
  collection_data = representation.fetch('data')
  @_collection    = collection_data.map do |resource| Resources::MemberResource.from_representation(resource, entry_point) end
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.



24
25
26
27
28
29
30
# File 'lib/api_navigator/resources/collection_resource.rb', line 24

def method_missing(method, *args, &block)
  begin
    @_collection.send(method, *args, &block)
  rescue NoMethodError
    super
  end
end

Instance Attribute Details

#_collectionObject (readonly)

Returns the embedded resource of the Resource as a ResourceCollection.



7
8
9
# File 'lib/api_navigator/resources/collection_resource.rb', line 7

def _collection
  @_collection
end

Instance Method Details

#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)


34
35
36
37
# File 'lib/api_navigator/resources/collection_resource.rb', line 34

def respond_to_missing?(method, include_private = false)
  @_collection.respond_to?(method, include_private) ||
    super
end