Class: HalClient::LinksSection
- Inherits:
-
Object
- Object
- HalClient::LinksSection
- Defined in:
- lib/hal_client/links_section.rb
Overview
Encapsulates a “_links” section.
Constant Summary collapse
- UNSET =
Object.new
Instance Attribute Summary collapse
-
#namespaces ⇒ Object
readonly
Returns the value of attribute namespaces.
Instance Method Summary collapse
-
#hrefs(link_rel, &default_proc) ⇒ Object
Returns the URLs or URL templates of each link with the specified rel in this section.
-
#initialize(section, namespaces = nil) ⇒ LinksSection
constructor
A new instance of LinksSection.
Constructor Details
#initialize(section, namespaces = nil) ⇒ LinksSection
Returns a new instance of LinksSection.
7 8 9 10 11 |
# File 'lib/hal_client/links_section.rb', line 7 def initialize(section, namespaces=nil) @namespaces = namespaces || CurieResolver.new(section.fetch("curies"){[]}) @section = fully_qualified section end |
Instance Attribute Details
#namespaces ⇒ Object (readonly)
Returns the value of attribute namespaces.
13 14 15 |
# File 'lib/hal_client/links_section.rb', line 13 def namespaces @namespaces end |
Instance Method Details
#hrefs(link_rel, &default_proc) ⇒ Object
Returns the URLs or URL templates of each link with the specified rel in this section.
link_rel - The fully qualified link relation default_proc - (optional) A proc to execute to create a
default value if the specified link_rel does not exist
Yields the link_rel to the default_proc if the specified link_rel is not present and returns the return value of the default_proc.
Raises KeyError if the specified link_rel is not present and no default_value or default_proc are provided.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hal_client/links_section.rb', line 28 def hrefs(link_rel, &default_proc) default_proc ||= ->(link_rel){ raise KeyError, "No resources are related via `#{link_rel}`" } return default_proc.call(link_rel) unless section.key? link_rel [section.fetch(link_rel)] .flatten .map{|link| resolve_to_url(link)} end |