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(hal_client, parsed_json) ⇒ Representation
constructor
Create a new Representation.
-
#property(name, default = MISSING, &default_proc) ⇒ Object
Returns The value of the specified property or the specified default value.
-
#related(link_rel, options = {}, &default_proc) ⇒ Object
Returns representations of resources related via the specified link rel or the specified default value.
-
#related_hrefs(link_rel, options = {}, &default_proc) ⇒ Object
Returns urls of resources related via the specified link rel or the specified default value.
Constructor Details
#initialize(hal_client, parsed_json) ⇒ Representation
Create a new Representation
hal_client - The HalClient instance to use when navigating. parsed_json - A hash structure representing a single HAL document.
19 20 21 22 |
# File 'lib/hal_client/representation.rb', line 19 def initialize(hal_client, parsed_json) @hal_client = hal_client @raw = parsed_json 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
73 74 75 76 |
# File 'lib/hal_client/representation.rb', line 73 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.
60 61 62 63 64 65 66 67 |
# File 'lib/hal_client/representation.rb', line 60 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.
43 44 45 |
# File 'lib/hal_client/representation.rb', line 43 def 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.
36 37 38 39 40 |
# File 'lib/hal_client/representation.rb', line 36 def property(name, default=MISSING, &default_proc) default_proc ||= ->(_){ default} if default != MISSING raw.fetch(name.to_s, &default_proc) end |
#related(link_rel, options = {}, &default_proc) ⇒ Object
Returns 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
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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hal_client/representation.rb', line 90 def (link_rel, = {}, &default_proc) default_proc ||= ->(link_rel){ raise KeyError, "No resources are related via `#{link_rel}`" } = (link_rel) rescue nil linked = linked(link_rel, ) rescue nil if !.nil? or !linked.nil? RepresentationSet.new (Array() + Array(linked)) else default_proc.call link_rel end end |
#related_hrefs(link_rel, options = {}, &default_proc) ⇒ Object
Returns urls 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
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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/hal_client/representation.rb', line 117 def (link_rel, ={}, &default_proc) default_proc ||= ->(link_rel){ raise KeyError, "No resources are related via `#{link_rel}`" } = boxed .fetch(link_rel, nil) linked = boxed link_section.fetch(link_rel, nil) if !.nil? or !linked.nil? Array().map{|it| it.fetch("_links").fetch("self").fetch("href") rescue nil} + Array(linked).map{|it| it.fetch("href", nil) }. compact else default_proc.call link_rel end end |