Class: ApiWrapper::ApiManager
- Inherits:
-
Object
- Object
- ApiWrapper::ApiManager
- 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
-
#base_url ⇒ String
readonly
The base URL for API requests.
-
#cache_policy ⇒ CachePolicy
readonly
The cache policy used for caching data.
-
#client ⇒ FaradayClient
readonly
The Faraday client used for making HTTP requests.
-
#endpoints ⇒ Hash
readonly
The endpoints configuration loaded from the API configuration file.
Instance Method Summary collapse
-
#fetch_data(endpoint_key, force_refresh: false) ⇒ Faraday::Response
Fetches data from the specified API endpoint.
-
#initialize(api_configuration_path, cache_store: nil) ⇒ ApiManager
constructor
Initializes a new instance of the ApiManager class.
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.
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_url ⇒ String (readonly)
The base URL for API requests.
27 28 29 |
# File 'lib/api_wrapper/api_manager.rb', line 27 def base_url @base_url end |
#cache_policy ⇒ CachePolicy (readonly)
The cache policy used for caching data.
27 28 29 |
# File 'lib/api_wrapper/api_manager.rb', line 27 def cache_policy @cache_policy end |
#client ⇒ FaradayClient (readonly)
The Faraday client used for making HTTP requests.
27 28 29 |
# File 'lib/api_wrapper/api_manager.rb', line 27 def client @client end |
#endpoints ⇒ Hash (readonly)
The endpoints configuration loaded from the API configuration file.
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.
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 |