Class: HalClient::Representation
- Inherits:
-
Object
- Object
- HalClient::Representation
- 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 Attribute Summary collapse
-
#hal_client ⇒ Object
readonly
Internal: Returns the HalClient used to retrieve this representation.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#[](name_or_rel) ⇒ Object
Returns the value of the specified property or representations of resources related via the specified link rel or nil.
-
#as_enum ⇒ Object
Returns an Enumerable of the items in this collection resource if this is an rfc 6573 collection.
-
#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.
- #hash ⇒ Object
-
#href ⇒ Object
Returns the URL of the resource this representation represents.
-
#initialize(options) ⇒ Representation
constructor
Create a new Representation.
-
#patch(data, options = {}) ⇒ Object
Patchs a
RepresentationorStringto this resource. -
#post(data, options = {}) ⇒ Object
Posts a
RepresentationorStringto this resource. -
#property(name, default = MISSING, &default_proc) ⇒ Object
Returns The value of the specified property or the specified default value.
-
#property?(name) ⇒ Boolean
(also: #has_property?)
Returns true if this representation contains the specified property.
-
#put(data, options = {}) ⇒ Object
Puts a
RepresentationorStringto this resource. -
#raw ⇒ Object
Internal: Returns parsed json document.
-
#raw_related_hrefs(link_rel, &default_proc) ⇒ Object
Returns values of the
hrefmember of links and the URL of embedded representations related via the specified link rel. -
#related(link_rel, options = {}, &default_proc) ⇒ Object
Returns representations of resources related via the specified link rel or the specified default value.
-
#related?(link_rel) ⇒ Boolean
(also: #has_related?)
Returns true if this representation contains a link (including embedded links) whose rel is
link_rel. -
#related_hrefs(link_rel, options = {}, &default_proc) ⇒ Object
Returns urls of resources related via the specified link rel or the specified default value.
-
#reset ⇒ Object
Resets this representation such that it will be requested from the upstream on it’s next use.
-
#to_json ⇒ Object
(also: #to_hal)
Returns the raw json representation of this representation.
-
#to_s ⇒ Object
Returns a short human readable description of this representation.
Constructor Details
#initialize(options) ⇒ Representation
Create a new Representation
options - name parameters
: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.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hal_client/representation.rb', line 21 def initialize() @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 Attribute Details
#hal_client ⇒ Object (readonly)
Internal: Returns the HalClient used to retrieve this representation
272 273 274 |
# File 'lib/hal_client/representation.rb', line 272 def hal_client @hal_client end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
249 250 251 252 253 254 255 256 257 |
# File 'lib/hal_client/representation.rb', line 249 def ==(other) if href && other.respond_to?(:href) href == other.href elsif other.respond_to?(:raw) @raw == other.raw else false end end |
#[](name_or_rel) ⇒ Object
Returns the value of the specified property or representations
of resources via the specified link rel or nil
name_or_rel - The name of property or link rel of interest
128 129 130 131 |
# File 'lib/hal_client/representation.rb', line 128 def [](name_or_rel) item_key = name_or_rel fetch(item_key, nil) end |
#as_enum ⇒ Object
Returns an Enumerable of the items in this collection resource if this is an rfc 6573 collection.
Raises HalClient::NotACollectionError if this is not a collection resource.
218 219 220 |
# File 'lib/hal_client/representation.rb', line 218 def as_enum Collection.new(self) end |
#fetch(name_or_rel, default = MISSING, &default_proc) ⇒ Object
Returns the value of the specified property or representations
of resources 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.
115 116 117 118 119 120 121 122 |
# File 'lib/hal_client/representation.rb', line 115 def fetch(name_or_rel, default=MISSING, &default_proc) item_key = name_or_rel default_proc ||= ->(_){default} if default != MISSING property(item_key) { (item_key, &default_proc) } end |
#hash ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/hal_client/representation.rb', line 241 def hash if href href.hash else @raw.hash end end |
#href ⇒ Object
Returns the URL of the resource this representation represents.
94 95 96 97 98 99 100 |
# File 'lib/hal_client/representation.rb', line 94 def href @href ||= if "self" links.hrefs('self').first else nil end end |
#patch(data, options = {}) ⇒ Object
Patchs a Representation or String to this resource. Causes this representation to be reloaded the next time it is used.
data - a String or an object that responds to #to_hal options - set of options to pass to ‘HalClient#patch`
60 61 62 63 64 |
# File 'lib/hal_client/representation.rb', line 60 def patch(data, ={}) @hal_client.patch(href, data, ).tap do reset end end |
#post(data, options = {}) ⇒ Object
Posts a Representation or String to this resource. Causes this representation to be reloaded the next time it is used.
data - a String or an object that responds to #to_hal options - set of options to pass to ‘HalClient#post`
38 39 40 41 42 |
# File 'lib/hal_client/representation.rb', line 38 def post(data, ={}) @hal_client.post(href, data, ).tap do reset end 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.
87 88 89 90 91 |
# File 'lib/hal_client/representation.rb', line 87 def property(name, default=MISSING, &default_proc) default_proc ||= ->(_){ default} if default != MISSING raw.fetch(name.to_s, &default_proc) end |
#property?(name) ⇒ Boolean Also known as: has_property?
Returns true if this representation contains the specified property.
name - the name of the property to check
70 71 72 |
# File 'lib/hal_client/representation.rb', line 70 def property?(name) raw.key? name end |
#put(data, options = {}) ⇒ Object
Puts a Representation or String to this resource. Causes this representation to be reloaded the next time it is used.
data - a String or an object that responds to #to_hal options - set of options to pass to ‘HalClient#put`
49 50 51 52 53 |
# File 'lib/hal_client/representation.rb', line 49 def put(data, ={}) @hal_client.put(href, data, ).tap do reset end end |
#raw ⇒ Object
Internal: Returns parsed json document
261 262 263 264 265 266 267 268 |
# File 'lib/hal_client/representation.rb', line 261 def raw if @raw.nil? && @href (fail "unable to make requests due to missing hal client") unless hal_client @raw ||= hal_client.get(@href).raw end @raw end |
#raw_related_hrefs(link_rel, &default_proc) ⇒ Object
Returns values of the href member of links and the URL of embedded representations related via the specified link rel. The only difference between this and #related_hrefs is that this method makes no attempt to expand templated links. For templated links the returned collection will include the template pattern as encoded in the HAL document.
link_rel - The link rel of interest 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.
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/hal_client/representation.rb', line 201 def (link_rel, &default_proc) default_proc ||= ->(link_rel){ raise KeyError, "No resources are related via `#{link_rel}`" } = (link_rel) { nil } linked = links.hrefs(link_rel) { nil } return default_proc.call(link_rel) if .nil? and linked.nil? Array(linked) + Array().map(&:href) end |
#related(link_rel, options = {}, &default_proc) ⇒ Object
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.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/hal_client/representation.rb', line 158 def (link_rel, = {}, &default_proc) default_proc ||= ->(link_rel){ raise KeyError, "No resources are related via `#{link_rel}`" } = (link_rel) { nil } linked = linked(link_rel, ) { nil } return default_proc.call(link_rel) if .nil? and linked.nil? RepresentationSet.new (Array() + Array(linked)) end |
#related?(link_rel) ⇒ Boolean Also known as:
Returns true if this representation contains a link (including embedded links) whose rel is link_rel.
link_rel - The link rel of interest
137 138 139 140 141 142 143 |
# File 'lib/hal_client/representation.rb', line 137 def (link_rel) _ = link_rel true rescue KeyError false end |
#related_hrefs(link_rel, options = {}, &default_proc) ⇒ Object
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.
182 183 184 185 |
# File 'lib/hal_client/representation.rb', line 182 def (link_rel, ={}, &default_proc) (link_rel, , &default_proc). map(&:href) end |
#reset ⇒ Object
Resets this representation such that it will be requested from the upstream on it’s next use.
224 225 226 227 |
# File 'lib/hal_client/representation.rb', line 224 def reset @href = href # make sure we have the href @raw = nil end |
#to_json ⇒ Object Also known as: to_hal
Returns the raw json representation of this representation
236 237 238 |
# File 'lib/hal_client/representation.rb', line 236 def to_json MultiJson.dump(raw) end |
#to_s ⇒ Object
Returns a short human readable description of this representation.
231 232 233 |
# File 'lib/hal_client/representation.rb', line 231 def to_s "#<" + self.class.name + ": " + (href || "ANONYMOUS") + ">" end |