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.
- #cache_control_params ⇒ Object
- #cached? ⇒ Boolean
- #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
- #uncached? ⇒ Boolean
-
#where(params = {}) ⇒ Object
Get request on resource#index with query params.
- #with_new_cache ⇒ Object
- #with_no_cache ⇒ Object
Constructor Details
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
33 34 35 |
# File 'lib/reviewed/request.rb', line 33 def all where({}) end |
#cache_control_params ⇒ Object
65 66 67 68 69 70 |
# File 'lib/reviewed/request.rb', line 65 def cache_control_params params = {} params.merge!({:"skip-cache" => true}) if skip_cache? params.merge!({:"reset-cache" => true}) if reset_cache? params end |
#cached? ⇒ Boolean
37 38 39 |
# File 'lib/reviewed/request.rb', line 37 def cached? !uncached? end |
#collection_from_response(method, url, params = {}) ⇒ Object
60 61 62 63 |
# File 'lib/reviewed/request.rb', line 60 def collection_from_response(method, url, params={}) response = client.send(method, url, params.merge(cache_control_params)) Reviewed::Collection.new(client, resource, response, params) end |
#find(id, params = {}) ⇒ Object
Get request on resource#show
22 23 24 25 |
# File 'lib/reviewed/request.rb', line 22 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
55 56 57 58 |
# File 'lib/reviewed/request.rb', line 55 def object_from_response(method, url, params={}) response = client.send(method, url, params.merge(cache_control_params)) resource.new(response.body, client) end |
#path ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/reviewed/request.rb', line 13 def path if @resource.respond_to? :to_path @resource.to_path(@scope) else @resource.to_s end end |
#uncached? ⇒ Boolean
41 42 43 |
# File 'lib/reviewed/request.rb', line 41 def uncached? skip_cache? || reset_cache? end |
#where(params = {}) ⇒ Object
Get request on resource#index with query params
28 29 30 |
# File 'lib/reviewed/request.rb', line 28 def where(params={}) collection_from_response(:get, path, params) end |
#with_new_cache ⇒ Object
50 51 52 53 |
# File 'lib/reviewed/request.rb', line 50 def with_new_cache @reset_cache = true self end |
#with_no_cache ⇒ Object
45 46 47 48 |
# File 'lib/reviewed/request.rb', line 45 def with_no_cache @skip_cache = true self end |