class Async::REST::Resource

The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g. “today's weather in Los Angeles”), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time.

Attributes

headers[R]
reference[R]

Public Class Methods

connect(endpoint) click to toggle source

@param endpoint [Async::HTTP::Endpoint] used to connect to the remote system and specify the base path.

# File lib/async/rest/resource.rb, line 43
def self.connect(endpoint)
        reference = ::Protocol::HTTP::Reference.parse(endpoint.path)
        
        return ::Protocol::HTTP::AcceptEncoding.new(HTTP::Client.new(endpoint)), reference
end
for(endpoint, *arguments) { |resource| ... } click to toggle source
# File lib/async/rest/resource.rb, line 49
def self.for(endpoint, *arguments)
        # TODO This behaviour is deprecated and will probably be removed.
        if endpoint.is_a? String
                endpoint = HTTP::Endpoint.parse(endpoint)
        end
        
        client, reference = connect(endpoint)
        
        resource = self.new(client, reference, *arguments)
        
        return resource unless block_given?
        
        Async do
                begin
                        yield resource
                ensure
                        resource.close
                end
        end
end
new(delegate, reference = ::Protocol::HTTP::Reference.parse, headers = ::Protocol::HTTP::Headers.new) click to toggle source

@param delegate [Async::HTTP::Middleware] the delegate that will handle requests. @param reference [::Protocol::HTTP::Reference] the resource identifier (base request path/parameters). @param headers [::Protocol::HTTP::Headers] the default headers that will be supplied with the request.

Calls superclass method
# File lib/async/rest/resource.rb, line 35
def initialize(delegate, reference = ::Protocol::HTTP::Reference.parse, headers = ::Protocol::HTTP::Headers.new)
        super(delegate)
        
        @reference = reference
        @headers = headers
end
with(parent, *arguments, headers: {}, **options) click to toggle source
# File lib/async/rest/resource.rb, line 73
def self.with(parent, *arguments, headers: {}, **options)
        reference = parent.reference.with(**options)
        
        self.new(*arguments, parent.delegate, reference, parent.headers.merge(headers))
end

Public Instance Methods

get(klass = Representation, **parameters) click to toggle source
# File lib/async/rest/resource.rb, line 83
def get(klass = Representation, **parameters)
        klass.new(self.with(parameters: parameters)).tap(&:value)
end
inspect() click to toggle source
# File lib/async/rest/resource.rb, line 101
def inspect
        "\#<#{self.class} #{@reference.inspect} #{@headers.inspect}>"
end
prepare_request(verb, payload) { |payload, headers| ... } click to toggle source

@param verb [String] the HTTP verb to use. @param payload [Object] the object which will used to generate the body of the request.

# File lib/async/rest/resource.rb, line 89
def prepare_request(verb, payload)
        if payload
                headers = @headers.dup
                body = yield payload, headers
        else
                headers = @headers
                body = nil
        end
        
        return ::Protocol::HTTP::Request[verb, @reference, headers, body]
end
to_s() click to toggle source
# File lib/async/rest/resource.rb, line 105
def to_s
        "\#<#{self.class} #{@reference.to_s}>"
end
with(*arguments, **options) click to toggle source
# File lib/async/rest/resource.rb, line 79
def with(*arguments, **options)
        self.class.with(self, *arguments, **options)
end