Module: ApiWrapper

Defined in:
lib/api_wrapper.rb,
lib/api_wrapper/version.rb,
lib/api_wrapper/api_manager.rb,
lib/api_wrapper/cache/cache_store.rb,
lib/api_wrapper/cache/cache_policy.rb,
lib/api_wrapper/http_client/base_client.rb,
lib/api_wrapper/http_client/faraday_client.rb

Overview

ApiWrapper provides a unified interface for managing API interactions and caching.

It simplifies API management and caching with sensible defaults and customization options.

Usage:

# Fetch data using default configuration
response = ApiWrapper.fetch_data('endpoint_key')
puts response.body

# Configure with custom settings
ApiWrapper.configure do |config|
  config.api_configuration_path = 'custom/path/to/config.yml'
  config.cache_store = CustomCacheStore.new
end
response = ApiWrapper.fetch_data('endpoint_key')
puts response.body

Defined Under Namespace

Modules: Cache, HttpClient Classes: ApiManager, Configuration

Constant Summary collapse

VERSION =
'0.1.6'

Class Method Summary collapse

Class Method Details

.api_managerObject

Returns the singleton ApiManager instance



66
67
68
69
70
71
# File 'lib/api_wrapper.rb', line 66

def api_manager
  @api_manager ||= begin
    config = configuration
    ApiManager.new(config.api_configuration_path, cache_store: config.cache_store)
  end
end

.configurationObject

Accesses the configuration instance, initializing if needed



53
54
55
# File 'lib/api_wrapper.rb', line 53

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Configures ApiWrapper with a block

Yields:

Raises:

  • (ArgumentError)


58
59
60
61
62
63
# File 'lib/api_wrapper.rb', line 58

def configure
  raise ArgumentError, 'Configuration block required' unless block_given?

  yield(configuration)
  reset_api_manager!
end

.fetch_data(endpoint_key, force_refresh: false) ⇒ Object

Fetches data from the specified endpoint



74
75
76
# File 'lib/api_wrapper.rb', line 74

def fetch_data(endpoint_key, force_refresh: false)
  api_manager.fetch_data(endpoint_key, force_refresh:)
end

.reset_api_manager!Object

Resets the ApiManager instance



79
80
81
# File 'lib/api_wrapper.rb', line 79

def reset_api_manager!
  @api_manager = nil
end