Class: HalClient::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/hal_client/link.rb

Overview

HAL representation of a single link. Provides access to an embedded representation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options)
  @literal_rel = options[:rel]
  @target = options[:target]
  @template = options[:template]
  @curie_resolver = options[: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_resolverObject

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_relObject

Returns the value of attribute literal_rel.



40
41
42
# File 'lib/hal_client/link.rb', line 40

def literal_rel
  @literal_rel
end

#targetObject

Returns the value of attribute target.



40
41
42
# File 'lib/hal_client/link.rb', line 40

def target
  @target
end

#templateObject

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.new_from_embedded_entry(options)
  hash_entry = options[:hash_entry]
  hal_client = options[:hal_client]
  curie_resolver = options[:curie_resolver]
  base_url = options[:base_url]

  rel = hash_entry[:rel]
  hash_data = hash_entry[:data]

  absolute_href = (base_url + hash_data['_links']['self']['href']).to_s
  hash_data['_links']['self']['href'] = absolute_href

  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(options)
  hash_entry = options[:hash_entry]
  hal_client = options[:hal_client]
  curie_resolver = options[:curie_resolver]
  base_url = options[: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_relObject



105
106
107
# File 'lib/hal_client/link.rb', line 105

def fully_qualified_rel
  curie_resolver.resolve(literal_rel)
end

#hashObject

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_hrefObject

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

Returns:

  • (Boolean)


110
111
112
# File 'lib/hal_client/link.rb', line 110

def templated?
  !template.nil?
end