Class: ApiResponseCache::Actions::ResponseCache

Inherits:
Object
  • Object
show all
Defined in:
lib/api_response_cache/response_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache_path, expires_in) ⇒ ResponseCache

Returns a new instance of ResponseCache.



4
5
6
7
# File 'lib/api_response_cache/response_cache.rb', line 4

def initialize(cache_path, expires_in)
  @cache_path = cache_path
  @expires_in = expires_in || 1.hour
end

Instance Method Details

#bodyObject



13
14
15
# File 'lib/api_response_cache/response_cache.rb', line 13

def body
  cached_response['body']
end

#cached_responseObject



25
26
27
# File 'lib/api_response_cache/response_cache.rb', line 25

def cached_response
  @cached_response ||= Rails.cache.read(@cache_path)
end

#headersObject



21
22
23
# File 'lib/api_response_cache/response_cache.rb', line 21

def headers
  cached_response['headers']
end

#present?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/api_response_cache/response_cache.rb', line 9

def present?
  cached_response.present?
end

#statusObject



17
18
19
# File 'lib/api_response_cache/response_cache.rb', line 17

def status
  cached_response['status']
end

#write_cache(response) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/api_response_cache/response_cache.rb', line 29

def write_cache(response)
  cache_object = {
    body: response.body,
    status: response.status,
    headers: response.headers
  }.as_json
  Rails.cache.write(@cache_path, cache_object, expires_in: @expires_in)
end