Class: Sevendigital::ApiOperatorCached

Inherits:
ApiOperator show all
Defined in:
lib/sevendigital/api_operator_cached.rb

Overview

Cached version of ApiOperator If response for an API request is already in cache and and it hasn’t expired returns the cached response is returned instead of making an API call otherwise uses methods inherited from ApiOperator to make an HTTP call to the API

Constant Summary

Constants inherited from ApiOperator

Sevendigital::ApiOperator::RESERVED_CHARACTERS

Instance Method Summary collapse

Methods inherited from ApiOperator

#add_form_parameters, #create_http_request, #create_request_uri, #create_standard_http_request, #digest_http_response, #ensure_secure_connection, #escape, #get_request_uri, #make_http_request, #make_http_request_and_digest, #new_http_request, #oauth_sign_request

Constructor Details

#initialize(client, cache) ⇒ ApiOperatorCached

:nodoc:



10
11
12
13
# File 'lib/sevendigital/api_operator_cached.rb', line 10

def initialize(client, cache)
  @cache = cache
  super(client)
end

Instance Method Details

#call_api(api_request) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/sevendigital/api_operator_cached.rb', line 15

def call_api(api_request)
  @client.log(:very_verbose) { "ApiOperatorCached: API Request: #{api_request.inspect}" }
  api_response = retrieve_from_cache(api_request)
  api_response = cache_response(api_request) if response_out_of_date?(api_response, default_cache_max_age(api_request))
  api_response.tap do |api_response|
    @client.log(:very_verbose) { "ApiOperatorCached: API Response: #{api_response}" }
  end
end

#default_cache_max_age(api_request) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/sevendigital/api_operator_cached.rb', line 47

def default_cache_max_age(api_request)
  return api_request.options[:cache_max_age] if api_request.options[:cache_max_age]
  if @client.configuration.cache_max_age
    return @client.configuration.cache_max_age[api_request.api_method.to_sym] || @client.configuration.cache_max_age[:"*"]
  end
  nil
end

#response_out_of_date?(api_response, cache_max_age, current_time = nil) ⇒ Boolean

def response_out_of_date?(api_response, current_time=nil)

  (api_response.nil? || header_invalid?(api_response.headers) || cache_expired?(api_response.headers, current_time)).tap do |expired|
    @client.log(:verbose) { "ApiOperatorCached: Cache response out of date" if expired }
  end
end

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sevendigital/api_operator_cached.rb', line 33

def response_out_of_date?(api_response, cache_max_age, current_time=nil)
  if api_response.nil?
    @client.log(:verbose) { "ApiOperatorCached: Response not found in cache" }
    return true
  end
  if header_invalid?(api_response.headers)
    @client.log(:verbose) { "ApiOperatorCached: Ignoring cached response - invalid cache-control header" }
    return true
  end
  (cache_expired?(api_response.headers, cache_max_age, current_time)).tap do |expired|
    @client.log(:verbose) { "ApiOperatorCached: Cache response out of date (max age #{cache_max_age})" } if expired
  end
end