Skip to content

Commit

Permalink
Merge pull request #43 from ahasunos/feat/use-api-wrapper
Browse files Browse the repository at this point in the history
Integrate `api_wrapper` for API Management and Refactor Code for Improved Maintainability
  • Loading branch information
ahasunos authored Sep 18, 2024
2 parents 299b784 + 0916fbf commit 41b540f
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 904 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jobs:
- name: Run tests
run: bundle exec rspec

- name: Upload coverage report
uses: actions/upload-artifact@v2
with:
name: coverage-report
path: coverage/
# - name: Upload coverage report
# uses: actions/upload-artifact@v4
# with:
# name: coverage-report
# path: coverage/
57 changes: 19 additions & 38 deletions lib/nse_data.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# 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
Expand All @@ -11,12 +10,16 @@ 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
Expand All @@ -25,41 +28,19 @@ def define_api_methods
#
# @return [Array] An array of endpoint names.
def list_all_endpoints
@list_all_endpoints ||= APIManager.new.load_endpoints
end

# Configure the logger for the NseData gem.
#
# 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?
nse_api_client.endpoints
end

# Access the configured logger.
# Fetches data from a specific API endpoint.
#
# 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.')
# @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
end

# Initialize configuration with default settings.
@logger_config = Config::Logger.new
# Define API methods at runtime.
define_api_methods
end

NseData.define_api_methods
83 changes: 0 additions & 83 deletions lib/nse_data/api_manager.rb

This file was deleted.

77 changes: 0 additions & 77 deletions lib/nse_data/cache/README.md

This file was deleted.

118 changes: 0 additions & 118 deletions lib/nse_data/cache/cache_policy.rb

This file was deleted.

Loading

0 comments on commit 41b540f

Please sign in to comment.