Class: Async::REST::Resource

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Resource.

Parameters:

  • delegate (Async::HTTP::Middleware)

    the delegate that will handle requests.

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

    the base request path/parameters.

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

    the default headers that will be supplied with the request.

  • wrapper (#prepare_request, #process_response) (defaults to: Wrapper::JSON.new)

    the wrapper for encoding/decoding the request/response body.



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

def initialize(delegate, reference = HTTP::Reference.parse, headers = HTTP::Headers.new, wrapper = Wrapper::JSON.new)
	super(delegate)
	
	@reference = reference
	@headers = headers
	@wrapper = wrapper
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

#wrapperObject (readonly)

Returns the value of attribute wrapper.



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

def wrapper
  @wrapper
end

Class Method Details

.connect(url) ⇒ Object



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

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

.for(url, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/async/rest/resource.rb', line 52

def self.for(url, *args)
	client, reference = connect(url)
	
	resource = self.new(client, reference, *args)
	
	return resource unless block_given?
	
	begin
		yield resource
	ensure
		resource.close
	end
end

.with(parent, *args, headers: {}, parameters: nil, path: nil, wrapper: parent.wrapper) ⇒ Object



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

def self.with(parent, *args, headers: {}, parameters: nil, path: nil, wrapper: parent.wrapper)
	self.new(*args, parent.delegate, parent.reference.dup(path, parameters), parent.headers.merge(headers), wrapper)
end

Instance Method Details

#prepare_request(verb, payload = nil, **parameters) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/async/rest/resource.rb', line 78

def prepare_request(verb, payload = nil, **parameters)
	if parameters.empty?
		reference = @reference
	else
		reference = @reference.dup(nil, parameters)
	end
	
	headers = @headers.dup
	
	if payload
		body = @wrapper.prepare_request(payload, headers)
	else
		body = nil
	end
	
	return HTTP::Request[verb, reference, headers, body]
end

#process_response(response) ⇒ Object



96
97
98
# File 'lib/async/rest/resource.rb', line 96

def process_response(response)
	@wrapper.process_response(response)
end

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



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

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