Class: Async::REST::Representation

Inherits:
Object
  • Object
show all
Defined in:
lib/async/rest/representation.rb

Overview

REST components perform actions on a resource by using a representation to capture the current or intended state of that resource and transferring that representation between components. A representation is a sequence of bytes, plus representation metadata to describe those bytes. Other commonly used but less precise names for a representation include: document, file, and HTTP message entity, instance, or variant.

A representation consists of data, metadata describing the data, and, on occasion, metadata to describe the metadata (usually for the purpose of verifying message integrity). Metadata is in the form of name-value pairs, where the name corresponds to a standard that defines the value’s structure and semantics. Response messages may include both representation metadata and resource metadata: information about the resource that is not specific to the supplied representation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, metadata: {}, value: nil, wrapper: Wrapper::JSON.new) ⇒ Representation

Returns a new instance of Representation.

Parameters:

  • resource (Resource)

    the RESTful resource that this representation is of.

  • metadata (Hash | HTTP::Headers) (defaults to: {})

    the metadata associated wtih teh representation.

  • value (Object) (defaults to: nil)

    the value of the representation.

  • wrapper (#prepare_request, #process_response) (defaults to: Wrapper::JSON.new)

    the wrapper for encoding/decoding the request/response body.



41
42
43
44
45
46
47
# File 'lib/async/rest/representation.rb', line 41

def initialize(resource, metadata: {}, value: nil, wrapper: Wrapper::JSON.new)
  @resource = resource
  @wrapper = wrapper
  
   = 
  @value = value
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



80
81
82
# File 'lib/async/rest/representation.rb', line 80

def 
  
end

#resourceObject (readonly)

Returns the value of attribute resource.



57
58
59
# File 'lib/async/rest/representation.rb', line 57

def resource
  @resource
end

#wrapperObject (readonly)

Returns the value of attribute wrapper.



58
59
60
# File 'lib/async/rest/representation.rb', line 58

def wrapper
  @wrapper
end

Class Method Details

.for(*args, **options) ⇒ Object



33
34
35
# File 'lib/async/rest/representation.rb', line 33

def self.for(*args, **options)
  self.new(Resource.for(*args), **options)
end

Instance Method Details

#closeObject



53
54
55
# File 'lib/async/rest/representation.rb', line 53

def close
  @resource.close
end

#inspectObject



105
106
107
# File 'lib/async/rest/representation.rb', line 105

def inspect
  "\#<#{self.class} #{@resource.inspect}: value=#{@value.inspect}>"
end

#prepare_request(verb, payload) ⇒ Object



60
61
62
# File 'lib/async/rest/representation.rb', line 60

def prepare_request(verb, payload)
  @resource.prepare_request(verb, payload, &@wrapper.method(:prepare_request))
end

#process_response(request, response) ⇒ Object



64
65
66
# File 'lib/async/rest/representation.rb', line 64

def process_response(request, response)
  @wrapper.process_response(request, response)
end

#valueObject



93
94
95
# File 'lib/async/rest/representation.rb', line 93

def value
  @value ||= value!
end

#value!Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/async/rest/representation.rb', line 82

def value!
  response = self.get
  
  if response.success?
     = response.headers
    @value = response.read
  else
    raise RequestFailure, "Could not fetch remote resource #{@resource}: #{response.status}!"
  end
end

#value=(value) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/async/rest/representation.rb', line 97

def value= value
  if @value = value
    self.post(value)
  else
    self.delete
  end
end

#with(**parameters) ⇒ Object



49
50
51
# File 'lib/async/rest/representation.rb', line 49

def with(**parameters)
  self.class.new(@resource.with(parameters: parameters), wrapper: @wrapper)
end