Class: ShafClient::Resource
Instance Attribute Summary collapse
Attributes inherited from BaseResource
#attributes, #curies, #embedded_resources, #links
Class Method Summary
collapse
Instance Method Summary
collapse
#[], #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.
24
25
26
27
28
29
|
# File 'lib/shaf_client/resource.rb', line 24
def initialize(client, payload, status = nil, = {})
@client = client
@http_status = status
@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
Returns the value of attribute headers.
7
8
9
|
# File 'lib/shaf_client/resource.rb', line 7
def
@headers
end
|
#http_status ⇒ Object
Returns the value of attribute http_status.
7
8
9
|
# File 'lib/shaf_client/resource.rb', line 7
def http_status
@http_status
end
|
Class Method Details
.build(client, payload, status = nil, headers = {}) ⇒ Object
19
20
21
22
|
# File 'lib/shaf_client/resource.rb', line 19
def self.build(client, payload, status = nil, = {})
content_type = .fetch('content-type', '')
ResourceMapper.for(content_type).new(client, payload, status, )
end
|
.content_type(type) ⇒ Object
11
12
13
|
# File 'lib/shaf_client/resource.rb', line 11
def self.content_type(type)
ResourceMapper.register(type, self)
end
|
.profile(name) ⇒ Object
15
16
17
|
# File 'lib/shaf_client/resource.rb', line 15
def self.profile(name)
content_type "application/hal+json;profile=#{name}"
end
|
Instance Method Details
#destroy! ⇒ Object
79
80
81
|
# File 'lib/shaf_client/resource.rb', line 79
def destroy!
delete(:delete)
end
|
#get_doc(rel) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/shaf_client/resource.rb', line 46
def get_doc(rel)
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_doc(uri)
end
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/shaf_client/resource.rb', line 60
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
= {'Accept': 'application/prs.hal-forms+json'}
client.get(uri, headers: ).tap do |form|
form.target = href if form.respond_to? :target=
end
end
|
#inspect ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/shaf_client/resource.rb', line 31
def inspect
<<~RESOURCE
Status: #{http_status}
Headers: #{}
#{to_s}
RESOURCE
end
|
#reload! ⇒ Object
75
76
77
|
# File 'lib/shaf_client/resource.rb', line 75
def reload!
self << get(:self, headers: {'Cache-Control': 'no-cache'})
end
|