Class: ShafClient::Resource

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

Constant Summary

Constants included from MimeTypes

MimeTypes::MIME_TYPE_ALPS_JSON, MimeTypes::MIME_TYPE_HAL, MimeTypes::MIME_TYPE_JSON, MimeTypes::MIME_TYPE_PROBLEM_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.



37
38
39
40
41
42
# 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

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
25
26
27
28
29
30
31
# 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) ⇒ Object



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

def self.content_type(type, profile: nil)
  ResourceMapper.register(type, profile, self)
end

.default_resource_class!Object



33
34
35
# File 'lib/shaf_client/resource.rb', line 33

def self.default_resource_class!
  ResourceMapper.default = 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

#content_typeObject



103
104
105
# File 'lib/shaf_client/resource.rb', line 103

def content_type
  headers['content-type']
end

#destroy!Object



99
100
101
# File 'lib/shaf_client/resource.rb', line 99

def destroy!
  delete(:delete)
end

#get(rel, **options) ⇒ Object



59
60
61
62
63
64
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

#inspectObject



44
45
46
47
48
49
50
# File 'lib/shaf_client/resource.rb', line 44

def inspect
  "    Status: \#{http_status}\n    Headers: \#{headers}\n    \#{to_s}\n  RESOURCE\nend\n"

#reload!Object



95
96
97
# File 'lib/shaf_client/resource.rb', line 95

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