Class: Reviewed::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/reviewed/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Request

Returns a new instance of Request.



5
6
7
8
9
10
11
# File 'lib/reviewed/request.rb', line 5

def initialize(opts={})
  @resource = opts[:resource]
  @scope = opts[:scope]
  @client = opts[:client] || Reviewed::Client.new
  @skip_cache = false
  @reset_cache = false
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/reviewed/request.rb', line 3

def client
  @client
end

#resourceObject

Returns the value of attribute resource.



3
4
5
# File 'lib/reviewed/request.rb', line 3

def resource
  @resource
end

Instance Method Details

#allObject

Convenience Method



33
34
35
# File 'lib/reviewed/request.rb', line 33

def all
  where({})
end

#cache_control_paramsObject



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

Returns:

  • (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

#pathObject



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

Returns:

  • (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_cacheObject



50
51
52
53
# File 'lib/reviewed/request.rb', line 50

def with_new_cache
  @reset_cache = true
  self
end

#with_no_cacheObject



45
46
47
48
# File 'lib/reviewed/request.rb', line 45

def with_no_cache
  @skip_cache = true
  self
end