Class: Reviewed::Request
- Inherits:
-
Object
- Object
- Reviewed::Request
- Defined in:
- lib/reviewed/request.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
-
#all ⇒ Object
Convenience Method.
- #collection_from_response(method, url, params = {}) ⇒ Object
-
#find(id, params = {}) ⇒ Object
Get request on resource#show.
-
#initialize(opts = {}) ⇒ Request
constructor
A new instance of Request.
- #object_from_response(method, url, params = {}) ⇒ Object
- #path ⇒ Object
-
#where(params = {}) ⇒ Object
Get request on resource#index with query params.
Constructor Details
#initialize(opts = {}) ⇒ Request
Returns a new instance of Request.
5 6 7 8 9 |
# File 'lib/reviewed/request.rb', line 5 def initialize(opts={}) @resource = opts[:resource] @scope = opts[:scope] @client = opts[:client] || Reviewed::Client.new end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
3 4 5 |
# File 'lib/reviewed/request.rb', line 3 def client @client end |
#resource ⇒ Object
Returns the value of attribute resource.
3 4 5 |
# File 'lib/reviewed/request.rb', line 3 def resource @resource end |
Instance Method Details
#all ⇒ Object
Convenience Method
31 32 33 |
# File 'lib/reviewed/request.rb', line 31 def all where({}) end |
#collection_from_response(method, url, params = {}) ⇒ Object
40 41 42 43 |
# File 'lib/reviewed/request.rb', line 40 def collection_from_response(method, url, params={}) response = client.send(method, url, params) Reviewed::Collection.new(client, resource, response, params) end |
#find(id, params = {}) ⇒ Object
Get request on resource#show
20 21 22 23 |
# File 'lib/reviewed/request.rb', line 20 def find(id, params={}) url_path = [path, CGI::escape(id.to_s)] object_from_response(:get, url_path.join('/'), params) end |
#object_from_response(method, url, params = {}) ⇒ Object
35 36 37 38 |
# File 'lib/reviewed/request.rb', line 35 def object_from_response(method, url, params={}) response = client.send(method, url, params) resource.new(response.body) end |
#path ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/reviewed/request.rb', line 11 def path if @resource.respond_to? :to_path @resource.to_path(@scope) else @resource.to_s end end |
#where(params = {}) ⇒ Object
Get request on resource#index with query params
26 27 28 |
# File 'lib/reviewed/request.rb', line 26 def where(params={}) collection_from_response(:get, path, params) end |