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, curies, entry_point) ⇒ LinkCollection

Initializes a LinkCollection.



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

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

  @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 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.



62
63
64
# File 'lib/hyperclient/link_collection.rb', line 62

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.



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

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
  elsif (curie_parts = /(?<ns>[^:]+):(?<short_name>.+)/.match(name))
    curie = curies[curie_parts[:ns]]
    link_or_links['href'] = curie.expand(link_or_links['href']) if curie
    Link.new(name, link_or_links, entry_point)
  else
    Link.new(name, link_or_links, entry_point)
  end
end