Class: HalClient::Representation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hal_client/representation.rb

Overview

HAL representation of a single resource. Provides access to properties, links and embedded representations.

Instance Method Summary collapse

Constructor Details

#initialize(parsed_json: nil, hal_client: nil, href: nil) ⇒ Representation

Create a new Representation

parsed_json - A hash structure representing a single HAL

document.

href - The href of this representation. hal_client - The HalClient instance to use when navigating.



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

def initialize(parsed_json: nil, hal_client: nil, href: nil)
  @raw = parsed_json
  @hal_client = hal_client
  @href = href

  (fail ArgumentError, "Either parsed_json or href must be provided") if
    @raw.nil? && href.nil?

  (fail InvalidRepresentationError, "Invalid HAL representation: #{raw.inspect}") if
    raw && ! hashish?(raw)
end

Instance Method Details

#[](name_or_rel) ⇒ Object

Returns the value of the specified property or representations

of resources related via the specified link rel or nil

name_or_rel - The name of property or link rel of interest



81
82
83
84
# File 'lib/hal_client/representation.rb', line 81

def [](name_or_rel)
  item_key = name_or_rel
  fetch(item_key, nil)
end

#fetch(name_or_rel, default = MISSING, &default_proc) ⇒ Object

Returns the value of the specified property or representations

of resources related via the specified link rel or the
specified default value.

name_or_rel - The name of property or link rel of interest default - an optional object that should be return if the

specified property or link does not exist

default_proc - an option proc that will be called with name

to produce default value if the specified property or link does not
exist

Raises KeyError if the specified property or link does not exist

and no default nor default_proc is provided.


68
69
70
71
72
73
74
75
# File 'lib/hal_client/representation.rb', line 68

def fetch(name_or_rel, default=MISSING, &default_proc)
  item_key = name_or_rel
  default_proc ||= ->(_){default} if default != MISSING

  property(item_key) {
    related(item_key, &default_proc)
  }
end

#has_related?(link_rel) ⇒ Boolean

Returns true if this representation contains a link (including embedded links) whose rel is link_rel.

link_rel - The link rel of interest

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/hal_client/representation.rb', line 90

def has_related?(link_rel)
  _ = related link_rel
  true

rescue KeyError
  false
end

#hrefObject

Returns the URL of the resource this representation represents.



51
52
53
# File 'lib/hal_client/representation.rb', line 51

def href
  @href ||= link_section.fetch("self").fetch("href")
end

#property(name, default = MISSING, &default_proc) ⇒ Object

Returns The value of the specified property or the specified

default value.

name - The name of property of interest default - an optional object that should be return if the

specified property does not exist

default_proc - an option proc that will be called with name

to produce default value if the specified property does not
exist

Raises KeyError if the specified property does not exist

and no default nor default_proc is provided.


44
45
46
47
48
# File 'lib/hal_client/representation.rb', line 44

def property(name, default=MISSING, &default_proc)
  default_proc ||= ->(_){ default} if default != MISSING

  raw.fetch(name.to_s, &default_proc)
end

Returns representations of resources related via the specified

link rel or the specified default value.

link_rel - The link rel of interest options - optional keys and values with which to expand any

templated links that are encountered

default_proc - an option proc that will be called with name

to produce default value if the specified property or link does not
exist

Raises KeyError if the specified link does not exist

and no default_proc is provided.


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/hal_client/representation.rb', line 110

def related(link_rel, options = {}, &default_proc)
  default_proc ||= ->(link_rel){
    raise KeyError, "No resources are related via `#{link_rel}`"
  }

  embedded = embedded_or_nil(link_rel)
  linked = linked_or_nil(link_rel, options)

  if !embedded.nil? or !linked.nil?
    RepresentationSet.new (Array(embedded) + Array(linked))
  else
    default_proc.call link_rel
  end
end

Returns urls of resources related via the specified

link rel or the specified default value.

link_rel - The link rel of interest options - optional keys and values with which to expand any

templated links that are encountered

default_proc - an option proc that will be called with name

to produce default value if the specified property or link does not
exist

Raises KeyError if the specified link does not exist

and no default_proc is provided.


137
138
139
140
# File 'lib/hal_client/representation.rb', line 137

def related_hrefs(link_rel, options={}, &default_proc)
  related(link_rel, options, &default_proc).
    map(&:href)
end

#to_sObject

Returns a short human readable description of this representation.



144
145
146
# File 'lib/hal_client/representation.rb', line 144

def to_s
  "#<" + self.class.name + ": " + href + ">"
end