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



7
8
9
10
11
12
# File 'lib/hal_client/links_section.rb', line 7

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.



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

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.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hal_client/links_section.rb', line 29

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