class ShafClient::Resource
Attributes
client[R]
headers[R]
http_status[R]
Public Class Methods
build(client, payload, content_type = MIME_TYPE_HAL, status = nil, headers = {})
click to toggle source
# File lib/shaf_client/resource.rb, line 21 def self.build(client, payload, content_type = MIME_TYPE_HAL, status = nil, headers = {}) resource_class, extensions = ResourceMapper.for( content_type: content_type, headers: headers, payload: payload, client: client, ) resrc = resource_class.new(client, payload, status, headers) extensions.compact.each { |extension| resrc.extend extension } resrc end
content_type(type, profile: nil)
click to toggle source
# File lib/shaf_client/resource.rb, line 13 def self.content_type(type, profile: nil) ResourceMapper.register(type, profile, self) end
default_resource_class!()
click to toggle source
# File lib/shaf_client/resource.rb, line 33 def self.default_resource_class! ResourceMapper.default = self end
new(client, payload, status = nil, headers = {})
click to toggle source
Calls superclass method
ShafClient::BaseResource::new
# File lib/shaf_client/resource.rb, line 37 def initialize(client, payload, status = nil, headers = {}) @client = client @http_status = status @headers = headers super(payload) end
profile(name)
click to toggle source
# File lib/shaf_client/resource.rb, line 17 def self.profile(name) content_type MIME_TYPE_HAL, profile: name end
Public Instance Methods
content_type()
click to toggle source
# File lib/shaf_client/resource.rb, line 103 def content_type headers['content-type'] end
destroy!()
click to toggle source
# File lib/shaf_client/resource.rb, line 99 def destroy! delete(:delete) end
get(rel, **options)
click to toggle source
# File lib/shaf_client/resource.rb, line 59 def get(rel, **options) href = link(rel).href embedded_resource = _embedded(rel) cached_resource = hypertext_cache_resource(href, embedded_resource, options) cached_resource || client.get(href, **options) end
get_doc(rel, **options)
click to toggle source
# File lib/shaf_client/resource.rb, line 66 def get_doc(rel, **options) rel = rel.to_s curie_name, rel = if rel.include? ':' rel.split(':') else [:doc, rel] end curie = curie(curie_name) uri = curie.resolve_templated(rel: rel) client.get(uri, **options) end
get_hal_form(rel)
click to toggle source
# File lib/shaf_client/resource.rb, line 80 def get_hal_form(rel) href = link(rel).href uri = rel.to_s if uri.match? %r{:[^/]} curie_name, rel = rel.split(':') curie = curie(curie_name) uri = curie.resolve_templated(rel: rel) end headers = {'Accept': 'application/prs.hal-forms+json'} client.get(uri, headers: headers).tap do |form| form.target = href if form.respond_to? :target= end end
inspect()
click to toggle source
# File lib/shaf_client/resource.rb, line 44 def inspect <<~RESOURCE Status: #{http_status} Headers: #{headers} #{to_s} RESOURCE end
reload!()
click to toggle source
# File lib/shaf_client/resource.rb, line 95 def reload! self << get(:self, headers: {'Cache-Control': 'no-cache'}) end
Protected Instance Methods
<<(other)
click to toggle source
Calls superclass method
ShafClient::BaseResource#<<
# File lib/shaf_client/resource.rb, line 109 def <<(other) @client = other.client @http_status = other.http_status.dup @headers = other.headers.dup super end
Private Instance Methods
build_embedded_resource(payload)
click to toggle source
# File lib/shaf_client/resource.rb, line 120 def build_embedded_resource(payload) self.class.build( client, payload, headers['content-type'], http_status, headers ) end
hypertext_cache?(options)
click to toggle source
# File lib/shaf_client/resource.rb, line 136 def hypertext_cache?(options) HypertextCacheStrategy.cacheable? hypertext_cache_strategy(options) end
hypertext_cache_resource(href, embedded_resource, options)
click to toggle source
# File lib/shaf_client/resource.rb, line 140 def hypertext_cache_resource(href, embedded_resource, options) return unless embedded_resource cache_strategy = hypertext_cache_strategy(options) return unless HypertextCacheStrategy.cacheable? cache_strategy if HypertextCacheStrategy.fetch_headers? cache_strategy resource = client.head(href, **options) status = resource.http_status headers = resource.headers embedded_resource = embedded_resource.payload else status = HypertextCacheStrategy.default_http_status headers = embedded_resource.headers end self.class.build(client, embedded_resource, headers['content-type'], status, headers) end
hypertext_cache_strategy(options)
click to toggle source
# File lib/shaf_client/resource.rb, line 130 def hypertext_cache_strategy(options) options.fetch(:hypertext_cache_strategy) do ShafClient.default_hypertext_cache_strategy end end