Class: Async::REST::Resource
- Inherits:
-
HTTP::Middleware
- Object
- HTTP::Middleware
- Async::REST::Resource
- 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
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#reference ⇒ Object
readonly
Returns the value of attribute reference.
Class Method Summary collapse
- .connect(url) ⇒ Object
- .for(url, *args) ⇒ Object
- .with(parent, *args, headers: {}, parameters: nil, path: nil) ⇒ Object
Instance Method Summary collapse
- #get(klass = Representation, **parameters) ⇒ Object
-
#initialize(delegate, reference = HTTP::Reference.parse, headers = HTTP::Headers.new) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object
- #prepare_request(verb, payload) ⇒ Object
- #to_s ⇒ Object
- #with(*args, **options) ⇒ Object
Constructor Details
#initialize(delegate, reference = HTTP::Reference.parse, headers = HTTP::Headers.new) ⇒ Resource
Returns a new instance of Resource.
36 37 38 39 40 41 |
# File 'lib/async/rest/resource.rb', line 36 def initialize(delegate, reference = HTTP::Reference.parse, headers = HTTP::Headers.new) super(delegate) @reference = reference @headers = headers end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
69 70 71 |
# File 'lib/async/rest/resource.rb', line 69 def headers @headers end |
#reference ⇒ Object (readonly)
Returns the value of attribute reference.
68 69 70 |
# File 'lib/async/rest/resource.rb', line 68 def reference @reference 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 65 66 |
# 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? Async.run do begin yield resource ensure resource.close end end end |
.with(parent, *args, headers: {}, parameters: nil, path: nil) ⇒ Object
71 72 73 74 75 |
# File 'lib/async/rest/resource.rb', line 71 def self.with(parent, *args, headers: {}, parameters: nil, path: nil) reference = parent.reference.dup(path, parameters) self.new(*args, parent.delegate, reference, parent.headers.merge(headers)) end |
Instance Method Details
#get(klass = Representation, **parameters) ⇒ Object
81 82 83 |
# File 'lib/async/rest/resource.rb', line 81 def get(klass = Representation, **parameters) klass.new(self.with(parameters: parameters)).tap(&:value) end |
#inspect ⇒ Object
99 100 101 |
# File 'lib/async/rest/resource.rb', line 99 def inspect "\#<#{self.class} #{@reference.inspect} #{@headers.inspect}>" end |
#prepare_request(verb, payload) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/async/rest/resource.rb', line 87 def prepare_request(verb, payload) if payload headers = @headers.dup body = yield payload, headers else headers = @headers body = nil end return HTTP::Request[verb, @reference, headers, body] end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/async/rest/resource.rb', line 103 def to_s "\#<#{self.class} #{@reference.to_s}>" end |
#with(*args, **options) ⇒ Object
77 78 79 |
# File 'lib/async/rest/resource.rb', line 77 def with(*args, **) self.class.with(self, *args, **) end |