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



56
57
58
# File 'lib/shaf_client/resource.rb', line 56

def destroy!
  delete(:delete)
end

#get_doc(rel) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/shaf_client/resource.rb', line 38

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

#reload!Object



52
53
54
# File 'lib/shaf_client/resource.rb', line 52

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