Class: ApiWrapper::ApiManager

Inherits:
Object
  • Object
show all
Defined in:
lib/api_wrapper/api_manager.rb

Overview

ApiManager is responsible for managing API interactions, including fetching data from endpoints and handling caching behavior.

It supports configuration of caching policies, including specifying endpoints that should bypass the cache and setting custom TTL (time-to-live) values for individual endpoints. The class uses a Faraday client for making HTTP requests and integrates with a cache store for storing and retrieving cached data.

Usage:

api_manager = ApiWrapper::ApiManager.new('path/to/api_configuration.yml')

response = api_manager.fetch_data('endpoint_key')
puts response.body

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_configuration_path, cache_store: nil) ⇒ ApiManager

Initializes a new instance of the ApiManager class.

Defaults to in-memory cache if nil.

Parameters:

  • api_configuration_path (String)

    Path to the API configuration file.

  • cache_store (CacheStore, RedisCacheStore, nil) (defaults to: nil)

    The cache store to use for caching.



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

def initialize(api_configuration_path, cache_store: nil)
  load_api_configuration(api_configuration_path)

  # Initialize cache policy
  @cache_policy = ApiWrapper::Cache::CachePolicy.new(cache_store || ApiWrapper::Cache::CacheStore.new)

  # Configure cache policy
  configure_cache_policy

  # Initialize Faraday client
  @client = ApiWrapper::HttpClient::FaradayClient.new(@base_url, @cache_policy)
end

Instance Attribute Details

#base_urlString (readonly)

The base URL for API requests.

Returns:

  • (String)

    the current value of base_url



27
28
29
# File 'lib/api_wrapper/api_manager.rb', line 27

def base_url
  @base_url
end

#cache_policyCachePolicy (readonly)

The cache policy used for caching data.

Returns:

  • (CachePolicy)

    the current value of cache_policy



27
28
29
# File 'lib/api_wrapper/api_manager.rb', line 27

def cache_policy
  @cache_policy
end

#clientFaradayClient (readonly)

The Faraday client used for making HTTP requests.

Returns:

  • (FaradayClient)

    the current value of client



27
28
29
# File 'lib/api_wrapper/api_manager.rb', line 27

def client
  @client
end

#endpointsHash (readonly)

The endpoints configuration loaded from the API configuration file.

Returns:

  • (Hash)

    the current value of endpoints



27
28
29
# File 'lib/api_wrapper/api_manager.rb', line 27

def endpoints
  @endpoints
end

Instance Method Details

#fetch_data(endpoint_key, force_refresh: false) ⇒ Faraday::Response

Fetches data from the specified API endpoint.

Parameters:

  • endpoint_key (String)

    The key of the API endpoint to fetch data from.

  • force_refresh (Boolean) (defaults to: false)

    Whether to force refresh the data, bypassing the cache.

Returns:

  • (Faraday::Response)

    The response object containing the fetched data.

Raises:

  • (ArgumentError)

    If the provided endpoint key is invalid.



52
53
54
55
56
57
58
59
# File 'lib/api_wrapper/api_manager.rb', line 52

def fetch_data(endpoint_key, force_refresh: false)
  endpoint = @endpoints[endpoint_key]
  raise ArgumentError, "Invalid endpoint key: #{endpoint_key}" unless endpoint

  @cache_policy.fetch(endpoint['path'], force_refresh:) do
    @client.get(endpoint['path'])
  end
end