Class: HalClient::Link
- Inherits:
-
Object
- Object
- HalClient::Link
- Defined in:
- lib/hal_client/link.rb
Overview
HAL representation of a single link. Provides access to an embedded representation.
Instance Attribute Summary collapse
-
#curie_resolver ⇒ Object
Returns the value of attribute curie_resolver.
-
#literal_rel ⇒ Object
Returns the value of attribute literal_rel.
-
#target ⇒ Object
Returns the value of attribute target.
-
#template ⇒ Object
Returns the value of attribute template.
Class Method Summary collapse
-
.new_from_embedded_entry(options) ⇒ Object
Create a new Link using an entry from the ‘_embedded’ section of a HAL document.
-
.new_from_link_entry(options) ⇒ Object
Create a new Link using an entry from the ‘_links’ section of a HAL document.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Links with the same href, same rel value, and the same ‘templated’ value are considered equal Otherwise, they are considered unequal.
- #fully_qualified_rel ⇒ Object
-
#hash ⇒ Object
Differing Representations or Addressable::Templates with matching hrefs will get matching hash values, since we are using raw_href and not the objects themselves when computing hash.
-
#initialize(options) ⇒ Link
constructor
Create a new Link.
-
#raw_href ⇒ Object
Returns the URL of the resource this link references.
-
#templated? ⇒ Boolean
Returns true for a templated link, false for an ordinary (non-templated) link.
Constructor Details
#initialize(options) ⇒ Link
Create a new Link
options - name parameters
:rel - This Link's rel property
:target - An instance of Representation
:template - A URI template ( https://www.rfc-editor.org/rfc/rfc6570.txt )
:curie_resolver - An instance of CurieResolver (used to resolve curied rels)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hal_client/link.rb', line 15 def initialize() @literal_rel = [:rel] @target = [:target] @template = [:template] @curie_resolver = [:curie_resolver] || CurieResolver.new([]) (fail ArgumentError, "A rel must be provided") if @literal_rel.nil? if @target.nil? && @template.nil? (fail ArgumentError, "A target or template must be provided") end if @target && @template (fail ArgumentError, "Cannot provide both a target and a template") end if @target && !@target.kind_of?(Representation) (fail ArgumentError, "Invalid HAL representation: #{target.inspect}") end if @template && !@template.kind_of?(Addressable::Template) (fail ArgumentError, "Invalid Addressable::Template: #{template.inspect}") end end |
Instance Attribute Details
#curie_resolver ⇒ Object
Returns the value of attribute curie_resolver.
40 41 42 |
# File 'lib/hal_client/link.rb', line 40 def curie_resolver @curie_resolver end |
#literal_rel ⇒ Object
Returns the value of attribute literal_rel.
40 41 42 |
# File 'lib/hal_client/link.rb', line 40 def literal_rel @literal_rel end |
#target ⇒ Object
Returns the value of attribute target.
40 41 42 |
# File 'lib/hal_client/link.rb', line 40 def target @target end |
#template ⇒ Object
Returns the value of attribute template.
40 41 42 |
# File 'lib/hal_client/link.rb', line 40 def template @template end |
Class Method Details
.new_from_embedded_entry(options) ⇒ Object
Create a new Link using an entry from the ‘_embedded’ section of a HAL document
options - name parameters
:hash_entry - a hash containing keys :rel (string) and :data (hash from a '_embedded' entry)
:hal_client - an instance of HalClient
:curie_resolver - An instance of CurieResolver (used to resolve curied rels)
:base_url - Base url for resolving relative links in hash_entry (probably the parent
document’s “self” link)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hal_client/link.rb', line 81 def self.() hash_entry = [:hash_entry] hal_client = [:hal_client] curie_resolver = [:curie_resolver] base_url = [:base_url] rel = hash_entry[:rel] hash_data = hash_entry[:data] explicit_url = self_href(hash_data) hash_data['_links']['self']['href'] = (base_url + explicit_url).to_s if explicit_url Link.new(rel: rel, target: Representation.new(hal_client: hal_client, parsed_json: hash_data), curie_resolver: curie_resolver) end |
.new_from_link_entry(options) ⇒ Object
Create a new Link using an entry from the ‘_links’ section of a HAL document
options - name parameters
:hash_entry - a hash containing keys :rel (string) and :data (hash from a '_links' entry)
:hal_client - an instance of HalClient
:curie_resolver - An instance of CurieResolver (used to resolve curied rels)
:base_url - Base url for resolving relative links in hash_entry (probably the parent
document’s “self” link)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/hal_client/link.rb', line 51 def self.new_from_link_entry() hash_entry = [:hash_entry] hal_client = [:hal_client] curie_resolver = [:curie_resolver] base_url = [:base_url] rel = hash_entry[:rel] hash_data = hash_entry[:data] href = (base_url + hash_data['href']).to_s if hash_data['templated'] Link.new(rel: rel, template: Addressable::Template.new(href), curie_resolver: curie_resolver) else Link.new(rel: rel, target: Representation.new(hal_client: hal_client, href: href), curie_resolver: curie_resolver) end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Links with the same href, same rel value, and the same ‘templated’ value are considered equal Otherwise, they are considered unequal
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/hal_client/link.rb', line 116 def ==(other) if other.respond_to?(:raw_href) && other.respond_to?(:fully_qualified_rel) && other.respond_to?(:templated?) (raw_href == other.raw_href) && (fully_qualified_rel == other.fully_qualified_rel) && (templated? == other.templated?) else false end end |
#fully_qualified_rel ⇒ Object
105 106 107 |
# File 'lib/hal_client/link.rb', line 105 def fully_qualified_rel curie_resolver.resolve(literal_rel) end |
#hash ⇒ Object
Differing Representations or Addressable::Templates with matching hrefs will get matching hash values, since we are using raw_href and not the objects themselves when computing hash
132 133 134 |
# File 'lib/hal_client/link.rb', line 132 def hash [fully_qualified_rel, raw_href, templated?].hash end |
#raw_href ⇒ Object
Returns the URL of the resource this link references. In the case of a templated link, this is the unresolved url template pattern.
101 102 103 |
# File 'lib/hal_client/link.rb', line 101 def raw_href templated? ? template.pattern : target.href end |
#templated? ⇒ Boolean
Returns true for a templated link, false for an ordinary (non-templated) link
110 111 112 |
# File 'lib/hal_client/link.rb', line 110 def templated? !template.nil? end |