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:



8
9
10
11
# File 'lib/sevendigital/api_operator_cached.rb', line 8

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

Instance Method Details

#call_api(api_request) ⇒ Object



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

def call_api(api_request)
  api_response = retrieve_from_cache(api_request)
  api_response = cache_response(api_request) if response_out_of_date?(api_response)

  api_response.tap do |api_response|
    @client.log(:very_verbose) { "ApiOperatorCached: API Response: #{api_response}" }
  end
end

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

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/sevendigital/api_operator_cached.rb', line 23

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