class HalClient::LinksSection
Encapsulates a “_links” section.
Constants
- NO_RELATED_RESOURCE
Attributes
base_url[R]
namespaces[R]
section[R]
Public Class Methods
new(section, opts={} )
click to toggle source
section - json hash for the links section base_url
- base URL with which to resolve relative URLs
# 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
Public Instance Methods
hrefs(link_rel, &default_proc)
click to toggle source
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.
# 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
Protected Instance Methods
fully_qualified(relations_section)
click to toggle source
# File lib/hal_client/links_section.rb, line 67 def fully_qualified(relations_section) Hash[relations_section.map {|rel, link_info| [(namespaces.resolve rel), link_info] }] end
resolve_to_url(link)
click to toggle source
# File lib/hal_client/links_section.rb, line 50 def resolve_to_url(link) (fail HalClient::InvalidRepresentationError) unless link.respond_to? :fetch return nil unless link.fetch("href") url = base_url + link.fetch("href") is_templated = link.fetch("templated", false) if is_templated Addressable::Template.new(url.to_s) else url.to_s end rescue KeyError fail HalClient::InvalidRepresentationError end