Class: HalClient::LinksSection

Inherits:
Object
  • Object
show all
Defined in:
lib/hal_client/links_section.rb

Overview

Encapsulates a “_links” section.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(section, opts = {}) ⇒ LinksSection

section - json hash for the links section base_url - base URL with which to resolve relative URLs



14
15
16
17
18
19
# File 'lib/hal_client/links_section.rb', line 14

def initialize(section, opts={} )
  @namespaces = CurieResolver.new(section.fetch("curies"){[]})

  @section = section.merge(fully_qualified(section))
  @base_url = opts.fetch(:base_url) { raise ArgumentError, "base_url must be specified" }
end

Instance Attribute Details

#namespacesObject (readonly)

Returns the value of attribute namespaces.



21
22
23
# File 'lib/hal_client/links_section.rb', line 21

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.



36
37
38
39
40
41
42
43
44
# File 'lib/hal_client/links_section.rb', line 36

def hrefs(link_rel, &default_proc)
  default_proc ||= NO_RELATED_RESOURCE

  return default_proc.call(link_rel) unless section.key? link_rel

  [section.fetch(link_rel)]
    .flatten
    .map{|link| resolve_to_url(link)}
end