Class: Hyperclient::LinkCollection

Inherits:
Collection show all
Defined in:
lib/hyperclient/link_collection.rb

Overview

A wrapper class to easily acces the links in a Resource.

Examples:

resource.links['author']
resource.links.author

Instance Method Summary collapse

Methods inherited from Collection

#[], #each, #fetch, #include?, #method_missing, #respond_to_missing?, #to_h, #to_s

Constructor Details

#initialize(collection, entry_point) ⇒ LinkCollection

Initializes a LinkCollection.



17
18
19
20
21
22
23
# File 'lib/hyperclient/link_collection.rb', line 17

def initialize(collection, entry_point)
  raise "Invalid response for LinkCollection. The response was: #{collection.inspect}" if collection && !collection.respond_to?(:collect)

  @collection = (collection || {}).inject({}) do |hash, (name, link)|
    hash.update(name => build_link(link, entry_point))
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hyperclient::Collection

Instance Method Details

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.

Creates links from the response hash.



32
33
34
35
36
37
38
39
# File 'lib/hyperclient/link_collection.rb', line 32

def build_link(link_or_links, entry_point)
  return unless link_or_links
  return Link.new(link_or_links, entry_point) unless link_or_links.respond_to?(:to_ary)

  link_or_links.collect do |link|
    build_link(link, entry_point)
  end
end