lib/nse_data.rb in nse_data-0.1.1 vs lib/nse_data.rb in nse_data-0.2.0

- old
+ new

@@ -1,65 +1,46 @@ # frozen_string_literal: true require_relative 'nse_data/version' -require_relative 'nse_data/api_manager' -require_relative 'nse_data/config/logger' +require_relative 'nse_data/nse_api_client' # The NseData module serves as the namespace for the NSE Data gem, # which provides an interface to interact with and retrieve stock market data # from the National Stock Exchange of India. module NseData class Error < StandardError; end class << self - # This module provides functionality for accessing NSE data. + # Caches the instance of NseApiClient. + def nse_api_client + @nse_api_client ||= NseApiClient.new + end + + # Dynamically define fetch methods for each API endpoint. def define_api_methods - api_manager = APIManager.new - api_manager.endpoints.each_key do |method_name| - define_singleton_method("fetch_#{method_name}") do - api_manager.fetch_data(method_name).body + nse_api_client.endpoints.each_key do |method_name| + define_singleton_method("fetch_#{method_name}") do |force_refresh: false| + nse_api_client.fetch_data(method_name, force_refresh:) end end end # Returns a list of all available endpoints. # # @return [Array] An array of endpoint names. def list_all_endpoints - @list_all_endpoints ||= APIManager.new.load_endpoints + nse_api_client.endpoints end - # Configure the logger for the NseData gem. + # Fetches data from a specific API endpoint. # - # This method allows users to customize the logger used throughout the library. - # To use it, call `NseData.configure` and provide a block to set up the logger. - # - # Example: - # - # NseData.configure do |config| - # custom_logger = Logger.new('nse_data.log') - # custom_logger.level = Logger::DEBUG - # config.logger = custom_logger - # end - # - # @yieldparam [NseData::Config::Logger] config The configuration object to be customized. - def configure - @logger_config ||= Config::Logger.new - yield(@logger_config) if block_given? + # @param endpoint [String] The endpoint key. + # @param force_refresh [Boolean] Skip cache if true. + # @return [Hash, String] The API response. + def fetch_data(endpoint, force_refresh: false) + nse_api_client.fetch_data(endpoint, force_refresh:) end - - # Access the configured logger. - # - # This method returns the Logger instance configured through `NseData.configure`. - # - # @return [Logger] The configured Logger instance. - # @raise [RuntimeError] If the logger has not been configured. - def logger - @logger_config&.logger || (raise 'Logger not configured. Please call NseData.configure first.') - end end - # Initialize configuration with default settings. - @logger_config = Config::Logger.new + # Define API methods at runtime. + define_api_methods end - -NseData.define_api_methods