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 Method Summary collapse
-
#[](name_or_rel) ⇒ Object
Returns the value of the specified property or representations of resources related via the specified link rel or nil.
-
#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.
-
#href ⇒ Object
Returns the URL of the resource this representation represents.
-
#initialize(options) ⇒ Representation
constructor
Create a new Representation.
-
#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.
-
#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.
-
#to_json ⇒ Object
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 Method Details
#[](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
99 100 101 102 |
# File 'lib/hal_client/representation.rb', line 99 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.
86 87 88 89 90 91 92 93 |
# File 'lib/hal_client/representation.rb', line 86 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 |
#href ⇒ Object
Returns the URL of the resource this representation represents.
69 70 71 |
# File 'lib/hal_client/representation.rb', line 69 def href @href ||= links.hrefs('self').first end |
#post(data, options = {}) ⇒ Object
Posts a Representation or String to this resource.
data - a String or an object that responds to #to_hal options - set of options to pass to ‘HalClient#post`
37 38 39 |
# File 'lib/hal_client/representation.rb', line 37 def post(data, ={}) @hal_client.post(href, data, ) 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.
62 63 64 65 66 |
# File 'lib/hal_client/representation.rb', line 62 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
45 46 47 |
# File 'lib/hal_client/representation.rb', line 45 def property?(name) raw.key? name 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.
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/hal_client/representation.rb', line 172 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.
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/hal_client/representation.rb', line 129 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
108 109 110 111 112 113 114 |
# File 'lib/hal_client/representation.rb', line 108 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.
153 154 155 156 |
# File 'lib/hal_client/representation.rb', line 153 def (link_rel, ={}, &default_proc) (link_rel, , &default_proc). map(&:href) end |
#to_json ⇒ Object
Returns the raw json representation of this representation
191 192 193 |
# File 'lib/hal_client/representation.rb', line 191 def to_json raw.to_json end |
#to_s ⇒ Object
Returns a short human readable description of this representation.
186 187 188 |
# File 'lib/hal_client/representation.rb', line 186 def to_s "#<" + self.class.name + ": " + href + ">" end |