Class: ApiNavigator::LinkCollection
- Inherits:
-
CollectionHash
- Object
- CollectionHash
- ApiNavigator::LinkCollection
- Defined in:
- lib/api_navigator/link_collection.rb
Overview
A wrapper class to easily acces the links in a Resource.
Instance Attribute Summary collapse
- #_curies ⇒ Object readonly
Instance Method Summary collapse
-
#build_curie(curie_hash, entry_point) ⇒ Object
private
private
Creates a curie from the response hash.
-
#build_link(name, link_or_links, curies, entry_point) ⇒ Object
private
private
Creates links from the response hash.
-
#initialize(collection, curies, entry_point) ⇒ LinkCollection
constructor
Initializes a LinkCollection.
Methods inherited from CollectionHash
#[], #each, #fetch, #include?, #method_missing, #respond_to_missing?, #to_h, #to_s
Constructor Details
#initialize(collection, curies, entry_point) ⇒ LinkCollection
Initializes a LinkCollection.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/api_navigator/link_collection.rb', line 17 def initialize(collection, curies, entry_point) raise "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 ApiNavigator::CollectionHash
Instance Attribute Details
#_curies ⇒ Object (readonly)
10 11 12 |
# File 'lib/api_navigator/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.
58 59 60 |
# File 'lib/api_navigator/link_collection.rb', line 58 def build_curie(curie_hash, entry_point) Curie.new(curie_hash, entry_point) end |
#build_link(name, link_or_links, curies, 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 links from the response hash.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/api_navigator/link_collection.rb', line 40 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 |