Async::RESTSourceAsyncRESTResource

class Resource

A resource is an abstract reference to some named entity, typically a URL with associated metatadata. Generally, your entry to any web service will be a resource, and that resource will create zero or more representations as you navigate through the service.

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.

Definitions

def self.connect(endpoint)

Connect to the given endpoint, returning the HTTP client and reference.

Signature

parameter endpoint Async::HTTP::Endpoint

used to connect to the remote system and specify the base path.

returns Tuple(Async::HTTP::Client, ::Protocol::HTTP::Reference)

the client and reference.

Implementation

def self.connect(endpoint)
	reference = ::Protocol::HTTP::Reference.parse(endpoint.path)
	
	return ::Protocol::HTTP::AcceptEncoding.new(HTTP::Client.new(endpoint)), reference
end

def self.open(endpoint = self::ENDPOINT, **options)

Create a new resource for the given endpoint.

Implementation

def self.open(endpoint = self::ENDPOINT, **options)
	if endpoint.is_a?(String)
		endpoint = Async::HTTP::Endpoint.parse(endpoint)
	end
	
	client, reference = connect(endpoint)
	
	resource = self.new(client, reference, **options)
	
	return resource unless block_given?
	
	Sync do
		yield resource
	ensure
		resource.close
	end
end

def initialize(delegate, reference = ::Protocol::HTTP::Reference.parse, headers = ::Protocol::HTTP::Headers.new)

Signature

parameter delegate Async::HTTP::Middleware

the delegate that will handle requests.

parameter reference ::Protocol::HTTP::Reference

the resource identifier (base request path/parameters).

parameter headers ::Protocol::HTTP::Headers

the default headers that will be supplied with the request.

Implementation

def initialize(delegate, reference = ::Protocol::HTTP::Reference.parse, headers = ::Protocol::HTTP::Headers.new)
	super(delegate)
	
	@reference = reference
	@headers = headers
end