Class: Async::REST::Resource

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

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Resource.

Parameters:

  • delegate (Async::HTTP::Middleware)

    the delegate that will handle requests.

  • reference (::Protocol::HTTP::Reference) (defaults to: ::Protocol::HTTP::Reference.parse)

    the resource identifier (base request path/parameters).

  • headers (::Protocol::HTTP::Headers) (defaults to: ::Protocol::HTTP::Headers.new)

    the default headers that will be supplied with the request.



37
38
39
40
41
42
# File 'lib/async/rest/resource.rb', line 37

def initialize(delegate, reference = ::Protocol::HTTP::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.



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

def headers
  @headers
end

#referenceObject (readonly)

Returns the value of attribute reference.



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

def reference
  @reference
end

Class Method Details

.connect(endpoint) ⇒ Object

Parameters:

  • endpoint (Async::HTTP::Endpoint)

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



45
46
47
48
49
# File 'lib/async/rest/resource.rb', line 45

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

.for(endpoint, *args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/async/rest/resource.rb', line 51

def self.for(endpoint, *args)
	# 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, *args)
	
	return resource unless block_given?
	
	Async do
		begin
			yield resource
		ensure
			resource.close
		end
	end
end

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



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

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

Instance Method Details

#get(klass = Representation, **parameters) ⇒ Object



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

def get(klass = Representation, **parameters)
	klass.new(self.with(parameters: parameters)).tap(&:value)
end

#inspectObject



103
104
105
# File 'lib/async/rest/resource.rb', line 103

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

#prepare_request(verb, payload) ⇒ Object

Parameters:

  • verb (String)

    the HTTP verb to use.

  • payload (Object)

    the object which will used to generate the body of the request.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/async/rest/resource.rb', line 91

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_sObject



107
108
109
# File 'lib/async/rest/resource.rb', line 107

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

#with(*args, **options) ⇒ Object



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

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