class Halo::APIResponse

Public Class Methods

new(options = {}) click to toggle source
# File lib/halo-api/api_response.rb, line 13
def initialize(options = {})
  client       = options.delete(:client)
  @api_key     = client.api_key
  @region      = client.region || Halo::Configuration::DEFAULT_REGION
  @cache_store = client.cache_store || {}

  raise InvalidCacheStore if cached? && !redis_store.is_a?(Redis)

  self.class.base_uri 'https://www.haloapi.com'
end

Public Instance Methods

cached?() click to toggle source

@return [Boolean] true if the request should be cached

# File lib/halo-api/api_response.rb, line 56
def cached?
  @cache_store[:cached]
end
get_data(path, options = {}) click to toggle source
# File lib/halo-api/api_response.rb, line 24
def get_data(path, options = {})
  store_key = "#{path}#{options}#{@region}"
  if cached?
    result = redis_store.get(store_key)
    return JSON.parse(result) if result
  end
  response = perform_uncached_request(:get, path, options)
  redis_store.setex(store_key, ttl, response.to_json) if cached?
  response
end
perform_uncached_request(verb, path, params = {}) click to toggle source
# File lib/halo-api/api_response.rb, line 35
def perform_uncached_request(verb, path, params = {})
  options = {}
  headers = {
    'Ocp-Apim-Subscription-Key' => @api_key,
    'Accept-Language' => @region
  }

  options[:headers] = headers unless headers.empty?
  options[:query]   = params unless params.empty?

  response = self.class.send(verb, Addressable::URI.encode(path), options)

  response.respond_to?(:parsed_response) ? response.parsed_response : response
end
redis_store() click to toggle source

@return [Redis] returns the cache store

# File lib/halo-api/api_response.rb, line 51
def redis_store
  @cache_store[:redis]
end
ttl() click to toggle source

@return [Integer] the ttl to apply to cached keys

# File lib/halo-api/api_response.rb, line 61
def ttl
  @cache_store[:ttl]
end