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.
-
#has_related?(link_rel) ⇒ Boolean
Returns true if this representation contains a link (including embedded links) whose rel is
link_rel. -
#href ⇒ Object
Returns the URL of the resource this representation represents.
-
#initialize(*args) ⇒ 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.
-
#to_s ⇒ Object
Returns a short human readable description of this representation.
Constructor Details
#initialize(*args) ⇒ 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.
Signature
initialize(hal_client, parsed_json)
initialize(parsed_json, hal_client)
Initialize this representation with a parsed json document and a hal_client with which to make requests.
initialize(href, hal_client)
Initialize this representation with an href and a hal_client with which to make requests. Any attempt to retrieve properties or related representations will result in the href being dereferenced.
initialize(href)
Initialize this representation with an href. The representation will not be able to make requests to dereference itself but this can still be useful in test situations to maintain a uniform interface.
initialize(parse_json)
Initializes representation that cannot request related representations.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/hal_client/representation.rb', line 48 def initialize(*args) (raise ArgumentError, "wrong number of arguments (#{args.size} for 1 or 2)") if args.size > 2 @raw = args.find {|it| (it.respond_to? :has_key?) && (it.respond_to? :fetch) } @hal_client = args.find {|it| HalClient === it } if @raw.nil? @href = args.find {|it| it.respond_to? :downcase } end 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
109 110 111 112 |
# File 'lib/hal_client/representation.rb', line 109 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.
96 97 98 99 100 101 102 103 |
# File 'lib/hal_client/representation.rb', line 96 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 |
#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
118 119 120 121 122 123 |
# File 'lib/hal_client/representation.rb', line 118 def (link_rel) _ = link_rel true rescue KeyError false end |
#href ⇒ Object
Returns the URL of the resource this representation represents.
79 80 81 |
# File 'lib/hal_client/representation.rb', line 79 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.
72 73 74 75 76 |
# File 'lib/hal_client/representation.rb', line 72 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.
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 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/hal_client/representation.rb', line 137 def (link_rel, = {}, &default_proc) default_proc ||= ->(link_rel){ raise KeyError, "No resources are related via `#{link_rel}`" } = (link_rel) linked = linked_or_nil(link_rel, ) 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.
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.
164 165 166 167 |
# File 'lib/hal_client/representation.rb', line 164 def (link_rel, ={}, &default_proc) (link_rel, , &default_proc). map(&:href) end |
#to_s ⇒ Object
Returns a short human readable description of this representation.
171 172 173 |
# File 'lib/hal_client/representation.rb', line 171 def to_s "#<" + self.class.name + ": " + href + ">" end |