class ShafClient::BaseResource
Attributes
attributes[R]
curies[R]
embedded_resources[R]
links[R]
Public Class Methods
new(payload)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 9 def initialize(payload) @payload = if payload&.is_a? String JSON.parse(payload) elsif payload.respond_to? :to_h payload.to_h else raise Error, <<~ERR Trying to create an instance of #{self.class} with a payload that cannot be coerced into a Hash ERR end parse end
Public Instance Methods
[](key)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 68 def [](key) attributes[key] end
actions()
click to toggle source
# File lib/shaf_client/base_resource.rb, line 72 def actions links.keys end
attribute(key, &block)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 42 def attribute(key, &block) block ||= proc { raise Error, "No attribute for key: #{key}" } _attribute(key, &block) end
curie(rel, &block)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 52 def curie(rel, &block) block ||= proc { raise Error, "No curie with rel: #{rel}" } _curie(rel, &block) end
embedded(rel, &block)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 57 def embedded(rel, &block) block ||= proc { raise Error, "No embedded resources with rel: #{rel}" } _embedded(rel, &block) end
inspect()
click to toggle source
# File lib/shaf_client/base_resource.rb, line 38 def inspect to_s end
link(rel, &block)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 47 def link(rel, &block) block ||= proc { raise Error, "No link with rel: #{rel}" } _link(rel, &block) end
rel?(rel)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 62 def rel?(rel) !link(rel).nil? || !embedded(rel).nil? rescue StandardError false end
to_h()
click to toggle source
# File lib/shaf_client/base_resource.rb, line 25 def to_h attributes.dup.tap do |hash| hash[:_links] = transform_values_to_h(links) hash[:_links].merge!(curies: curies.values.map(&:to_h)) unless curies.empty? embedded = transform_values_to_h(embedded_resources) hash[:_embedded] = embedded unless embedded.empty? end end
to_s()
click to toggle source
# File lib/shaf_client/base_resource.rb, line 34 def to_s JSON.pretty_generate(to_h) end
Protected Instance Methods
<<(other)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 116 def <<(other) @payload = other.payload.dup @attributes = other.attributes.dup @links = other.links.dup @curies = other.curies.dup @embedded_resources = other.embedded_resources.dup self end
_attribute(key, &block)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 82 def _attribute(key, &block) if block attributes.fetch(key.to_sym, &block) else attributes[key.to_sym] end end
_curie(rel, &block)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 99 def _curie(rel, &block) if block curies.fetch(rel.to_sym, &block) else curies[rel.to_sym] end end
_embedded(rel, &block)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 107 def _embedded(rel, &block) rewritten_rel = best_match(embedded_resources.keys, rel) if block embedded_resources.fetch(rewritten_rel, &block) else embedded_resources[rewritten_rel] end end
_link(rel, &block)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 90 def _link(rel, &block) rewritten_rel = best_match(links.keys, rel) if block links.fetch(rewritten_rel, &block) else links[rewritten_rel] end end
payload()
click to toggle source
# File lib/shaf_client/base_resource.rb, line 78 def payload @payload ||= {} end
Private Instance Methods
best_match(rels, rel)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 178 def best_match(rels, rel) rel = rel.to_sym return rel if rels.include? rel unless rel.to_s.include? ':' matches = rels.grep(/[^:]*:#{rel}/) return matches.first if matches.size == 1 raise AmbiguousRelError, "Ambiguous rel: #{rel}. (#{matches})" if matches.size > 1 end best_match(rels, rel.to_s.tr('_', '-')) if rel.to_s.include? '_' end
build_embedded_resource(payload)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 164 def build_embedded_resource(payload) BaseResource.new(payload) end
method_missing(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/shaf_client/base_resource.rb, line 168 def method_missing(method_name, *args, &block) return super unless attributes.key?(method_name) attribute(method_name) end
parse()
click to toggle source
# File lib/shaf_client/base_resource.rb, line 127 def parse @attributes = payload.transform_keys(&:to_sym) parse_links parse_embedded end
parse_curies(curies)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 143 def parse_curies(curies) curies.each do |value| curie = Curie.from(value) @curies[curie.name.to_sym] = curie end end
parse_embedded()
click to toggle source
# File lib/shaf_client/base_resource.rb, line 150 def parse_embedded embedded = @attributes.delete(:_embedded) || {} @embedded_resources ||= {} embedded.each do |key, value| @embedded_resources[key.to_sym] = if value.is_a? Array value.map { |d| build_embedded_resource(d) } else build_embedded_resource(value) end end end
parse_links()
click to toggle source
# File lib/shaf_client/base_resource.rb, line 133 def parse_links @links ||= {} @curies ||= {} (attributes.delete(:_links) || {}).each do |key, value| next parse_curies(value) if key == 'curies' @links[key.to_sym] = Link.from(value) end end
respond_to_missing?(method_name, include_private = false)
click to toggle source
Calls superclass method
# File lib/shaf_client/base_resource.rb, line 173 def respond_to_missing?(method_name, include_private = false) return true if attributes.key?(method_name) super end
transform_values_to_h(hash)
click to toggle source
# File lib/shaf_client/base_resource.rb, line 191 def transform_values_to_h(hash) hash.transform_values do |value| if value.is_a? Array value.map(&:to_h) else value.to_h end end end