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

metadata[R]
resource[R]
wrapper[R]

Public Class Methods

[](wrapper) click to toggle source
# File lib/async/rest/representation.rb, line 33
def self.[] wrapper
        klass = Class.new(self)
        
        klass.const_set(:WRAPPER, wrapper)
        
        return klass
end
for(*arguments, **options) { |representation| ... } click to toggle source
# 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
new(resource, metadata: {}, value: nil, wrapper: self.class::WRAPPER.new) click to toggle source

@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

[](**parameters) click to toggle source
# File lib/async/rest/representation.rb, line 77
def [] **parameters
        self.with(parameters: parameters)
end
assign(value) click to toggle source
# File lib/async/rest/representation.rb, line 149
def assign(value)
        response = self.call(value)
        
        response.read
        
        return @value
end
call(value) click to toggle source
# File lib/async/rest/representation.rb, line 141
def call(value)
        if value
                self.post(value)
        else
                self.delete
        end
end
close() click to toggle source
# File lib/async/rest/representation.rb, line 81
def close
        @resource.close
end
inspect() click to toggle source
# File lib/async/rest/representation.rb, line 161
def inspect
        "\#<#{self.class} #{@resource.inspect}: value=#{@value.inspect}>"
end
prepare_request(verb, payload) click to toggle source
# File lib/async/rest/representation.rb, line 88
def prepare_request(verb, payload)
        @resource.prepare_request(verb, payload, &@wrapper.method(:prepare_request))
end
process_response(request, response) click to toggle source

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
update() click to toggle source
# File lib/async/rest/representation.rb, line 157
def update
        @value = assign(@value)
end
value() click to toggle source
# File lib/async/rest/representation.rb, line 133
def value
        @value ||= value!
end
value!() click to toggle source
# 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
value=(value) click to toggle source
# File lib/async/rest/representation.rb, line 137
def value= value
        @value = self.assign(value)
end
value?() click to toggle source
# File lib/async/rest/representation.rb, line 129
def value?
        !@value.nil?
end
with(klass = nil, **options) click to toggle source
# 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