Class: ShafClient::Resource

Inherits:
BaseResource show all
Includes:
MimeTypes
Defined in:
lib/shaf_client/resource.rb

Direct Known Subclasses

ApiError, EmptyResource, Form, UnknownResource

Constant Summary

Constants included from MimeTypes

MimeTypes::DEFAULT_ADAPTER, MimeTypes::MIME_TYPE_HAL, MimeTypes::MIME_TYPE_JSON

Instance Attribute Summary collapse

Attributes inherited from BaseResource

#attributes, #curies, #embedded_resources, #links

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseResource

#[], #actions, #attribute, #curie, #embedded, #link, #rel?, #to_h, #to_s

Constructor Details

#initialize(client, payload, status = nil, headers = {}) ⇒ Resource

Returns a new instance of Resource.



26
27
28
29
30
31
# File 'lib/shaf_client/resource.rb', line 26

def initialize(client, payload, status = nil, headers = {})
  @client = client
  @http_status = status
  @headers = headers
  super(payload)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ShafClient::BaseResource

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/shaf_client/resource.rb', line 9

def headers
  @headers
end

#http_statusObject (readonly)

Returns the value of attribute http_status.



9
10
11
# File 'lib/shaf_client/resource.rb', line 9

def http_status
  @http_status
end

Class Method Details

.build(client, payload, content_type = MIME_TYPE_HAL, status = nil, headers = {}) ⇒ Object



21
22
23
24
# File 'lib/shaf_client/resource.rb', line 21

def self.build(client, payload, content_type = MIME_TYPE_HAL, status = nil, headers = {})
  ResourceMapper.for(content_type)
    .new(client, payload, status, headers)
end

.content_type(type) ⇒ Object



13
14
15
# File 'lib/shaf_client/resource.rb', line 13

def self.content_type(type)
  ResourceMapper.register(type, self)
end

.profile(name) ⇒ Object



17
18
19
# File 'lib/shaf_client/resource.rb', line 17

def self.profile(name)
  content_type "#{MIME_TYPE_HAL};profile=#{name}"
end

Instance Method Details

#destroy!Object



88
89
90
# File 'lib/shaf_client/resource.rb', line 88

def destroy!
  delete(:delete)
end

#get(rel, **options) ⇒ Object



48
49
50
51
52
53
# File 'lib/shaf_client/resource.rb', line 48

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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/shaf_client/resource.rb', line 55

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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shaf_client/resource.rb', line 69

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

#inspectObject



33
34
35
36
37
38
39
# File 'lib/shaf_client/resource.rb', line 33

def inspect
  <<~RESOURCE
    Status: #{http_status}
    Headers: #{headers}
    #{to_s}
  RESOURCE
end

#reload!Object



84
85
86
# File 'lib/shaf_client/resource.rb', line 84

def reload!
  self << get(:self, headers: {'Cache-Control': 'no-cache'})
end