Class: Async::REST::Resource
- Inherits:
-
HTTP::Middleware
- Object
- HTTP::Middleware
- Async::REST::Resource
- Defined in:
- lib/async/rest/resource.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#reference ⇒ Object
readonly
Returns the value of attribute reference.
-
#wrapper ⇒ Object
readonly
Returns the value of attribute wrapper.
Class Method Summary collapse
- .connect(url) ⇒ Object
- .for(url, *args) ⇒ Object
- .with(parent, *args, headers: {}, parameters: nil, path: nil, wrapper: parent.wrapper) ⇒ Object
Instance Method Summary collapse
-
#initialize(delegate, reference = HTTP::Reference.parse, headers = HTTP::Headers.new, wrapper = Wrapper::JSON.new) ⇒ Resource
constructor
A new instance of Resource.
- #prepare_request(verb, payload = nil, **parameters) ⇒ Object
- #process_response(response) ⇒ Object
- #with(*args, **options) ⇒ Object
Constructor Details
#initialize(delegate, reference = HTTP::Reference.parse, headers = HTTP::Headers.new, wrapper = Wrapper::JSON.new) ⇒ Resource
Returns a new instance of Resource.
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
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
67 68 69 |
# File 'lib/async/rest/resource.rb', line 67 def headers @headers end |
#reference ⇒ Object (readonly)
Returns the value of attribute reference.
66 67 68 |
# File 'lib/async/rest/resource.rb', line 66 def reference @reference end |
#wrapper ⇒ Object (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, **) self.class.with(self, *args, **) end |