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 Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Collection

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

Constructor Details

#initialize(collection, curies, entry_point) ⇒ LinkCollection

Initializes a LinkCollection.

Parameters:

  • collection

    The Hash with the links.

  • curies

    The Hash with link curies.

  • entry_point

    The EntryPoint object to inject the configuration.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hyperclient/link_collection.rb', line 17

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

  @_curies = (curies || {}).reduce({}) do |hash, curie_hash|
    curie = build_curie(curie_hash, entry_point)
    hash.update(curie.name => curie)
  end

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

Dynamic Method Handling

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

Instance Attribute Details

#_curiesObject (readonly)


10
11
12
# File 'lib/hyperclient/link_collection.rb', line 10

def _curies
  @_curies
end

Instance Method Details

#build_curie(curie_hash, entry_point) ⇒ Object (private)

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 a curie from the response hash.

Parameters:

  • curie_hash

    A Hash with the curie.

  • entry_point

    The EntryPoint object to inject the configuration.

Returns:

  • a Link or an array of Links when given an Array.


60
61
62
# File 'lib/hyperclient/link_collection.rb', line 60

def build_curie(curie_hash, entry_point)
  Curie.new(curie_hash, entry_point)
end

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.

Parameters:

  • name

    A String to identify the link's name.

  • link_or_links

    A Hash or an Array of hashes with the links to build.

  • curies

    An Array of Curies for templated links.

  • entry_point

    The EntryPoint object to inject the configuration.

Returns:

  • a Link or an Array of Links when given an Array.


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hyperclient/link_collection.rb', line 42

def build_link(name, link_or_links, curies, entry_point)
  return unless link_or_links

  if link_or_links.respond_to?(:to_ary)
    link_or_links.map do |link|
      build_link(name, link, curies, entry_point)
    end
  else
    Link.new(name, link_or_links, entry_point)
  end
end