Class: ShafClient::Resource

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

Direct Known Subclasses

ApiError, EmptyResource, Form, UnknownResource

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.



24
25
26
27
28
29
# File 'lib/shaf_client/resource.rb', line 24

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.



7
8
9
# File 'lib/shaf_client/resource.rb', line 7

def headers
  @headers
end

#http_statusObject (readonly)

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, headers = {})
  content_type = headers.fetch('content-type', '')
  ResourceMapper.for(content_type).new(client, payload, status, headers)
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

#get_hal_form(rel) ⇒ Object



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

  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



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

def inspect
  <<~RESOURCE
    Status: #{http_status}
    Headers: #{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