Class: Async::REST::Resource

Inherits:
Protocol::HTTP::Middleware
  • Object
show all
Defined in:
lib/async/rest/resource.rb

Overview

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.

Constant Summary collapse

ENDPOINT =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, reference = ::Protocol::URL::Reference.parse, headers = ::Protocol::HTTP::Headers.new) ⇒ Resource

Returns a new instance of Resource.



59
60
61
62
63
64
# File 'lib/async/rest/resource.rb', line 59

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



67
68
69
# File 'lib/async/rest/resource.rb', line 67

def headers
  @headers
end

#referenceObject (readonly)

Returns the value of attribute reference.



66
67
68
# File 'lib/async/rest/resource.rb', line 66

def reference
  @reference
end

Class Method Details

.connect(endpoint) ⇒ Object

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



24
25
26
27
28
# File 'lib/async/rest/resource.rb', line 24

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

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

Create a new resource for the given endpoint.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/async/rest/resource.rb', line 31

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

.with(parent, headers: {}, **options) ⇒ Object



49
50
51
52
53
54
# File 'lib/async/rest/resource.rb', line 49

def self.with(parent, headers: {}, **options)
	reference = parent.reference.with(**options)
	headers = parent.headers.merge(headers)
	
	self.new(parent.delegate, reference, headers)
end

Instance Method Details

#call(request) ⇒ Object



85
86
87
88
89
90
# File 'lib/async/rest/resource.rb', line 85

def call(request)
	request.path = @reference.with(path: request.path).to_s
	request.headers = @headers.merge(request.headers)
	
	super
end

#inspectObject



77
78
79
# File 'lib/async/rest/resource.rb', line 77

def inspect
	"\#<#{self.class} #{@reference.inspect} #{@headers.inspect}>"
end

#pathObject



73
74
75
# File 'lib/async/rest/resource.rb', line 73

def path
	@reference.path
end

#to_sObject



81
82
83
# File 'lib/async/rest/resource.rb', line 81

def to_s
	"\#<#{self.class} #{@reference.to_s}>"
end

#with(**options) ⇒ Object



69
70
71
# File 'lib/async/rest/resource.rb', line 69

def with(**options)
	self.class.with(self, **options)
end