lib/reviewed/request.rb in reviewed-0.5.0 vs lib/reviewed/request.rb in reviewed-0.6.0

- old
+ new

@@ -4,10 +4,12 @@ def initialize(opts={}) @resource = opts[:resource] @scope = opts[:scope] @client = opts[:client] || Reviewed::Client.new + @skip_cache = false + @reset_cache = false end def path if @resource.respond_to? :to_path @resource.to_path(@scope) @@ -31,15 +33,50 @@ def all where({}) end def object_from_response(method, url, params={}) - response = client.send(method, url, params) + response = client.send(method, url, params.merge(cache_control_params)) resource.new(response.body) end + def cached? + !uncached? + end + + def uncached? + skip_cache? || reset_cache? + end + + def with_no_cache + @skip_cache = true + self + end + + def with_new_cache + @reset_cache = true + self + end + def collection_from_response(method, url, params={}) - response = client.send(method, url, params) + response = client.send(method, url, params.merge(cache_control_params)) Reviewed::Collection.new(client, resource, response, params) + end + + def cache_control_params + params = {} + params.merge!({:"skip-cache" => true}) if skip_cache? + params.merge!({:"reset-cache" => true}) if reset_cache? + params + end + + private + + def skip_cache? + @skip_cache + end + + def reset_cache? + @reset_cache end end end