class Async::REST::Representation
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.
Constants
- WRAPPER
Attributes
Public Class Methods
# File lib/async/rest/representation.rb, line 33 def self.[] wrapper klass = Class.new(self) klass.const_set(:WRAPPER, wrapper) return klass end
# File lib/async/rest/representation.rb, line 41 def self.for(*arguments, **options) representation = self.new(Resource.for(*arguments), **options) return representation unless block_given? Async do begin yield representation ensure representation.close end end end
@param resource [Resource] the RESTful resource that this representation is of. @param metadata [Hash | HTTP::Headers] the metadata associated wtih teh representation. @param value [Object] the value of the representation. @param wrapper [#prepare_request, process_response
] the wrapper for encoding/decoding the request/response body.
# File lib/async/rest/representation.rb, line 61 def initialize(resource, metadata: {}, value: nil, wrapper: self.class::WRAPPER.new) @resource = resource @wrapper = wrapper @metadata = metadata @value = value end
Public Instance Methods
# File lib/async/rest/representation.rb, line 77 def [] **parameters self.with(parameters: parameters) end
# File lib/async/rest/representation.rb, line 149 def assign(value) response = self.call(value) response.read return @value end
# File lib/async/rest/representation.rb, line 141 def call(value) if value self.post(value) else self.delete end end
# File lib/async/rest/representation.rb, line 81 def close @resource.close end
# File lib/async/rest/representation.rb, line 161 def inspect "\#<#{self.class} #{@resource.inspect}: value=#{@value.inspect}>" end
# File lib/async/rest/representation.rb, line 88 def prepare_request(verb, payload) @resource.prepare_request(verb, payload, &@wrapper.method(:prepare_request)) end
If an exception propagates out of this method, the response will be closed.
# File lib/async/rest/representation.rb, line 93 def process_response(request, response) @wrapper.process_response(request, response) end
# File lib/async/rest/representation.rb, line 157 def update @value = assign(@value) end
# File lib/async/rest/representation.rb, line 133 def value @value ||= value! end
# File lib/async/rest/representation.rb, line 118 def value! response = self.get if response.success? @metadata = response.headers @value = response.read else raise ResponseError, response end end
# File lib/async/rest/representation.rb, line 137 def value= value @value = self.assign(value) end
# File lib/async/rest/representation.rb, line 129 def value? !@value.nil? end
# File lib/async/rest/representation.rb, line 69 def with(klass = nil, **options) if klass klass.new(@resource.with(**options), wrapper: klass::WRAPPER.new) else self.class.new(@resource.with(**options), wrapper: @wrapper) end end