From 46365db17830f63999a6dc474b343c6a4535fa9c Mon Sep 17 00:00:00 2001 From: Aaron Rosenberg Date: Sat, 31 Oct 2020 17:09:29 -0500 Subject: [PATCH] Ensure backward compatibility w/ previous logger config Extract Config accessory to module for clearer inclusion/extension into IEX::Api --- CHANGELOG.md | 1 + README.md | 18 +++++++++++++++--- lib/iex/api.rb | 2 +- lib/iex/api/client.rb | 4 ++++ lib/iex/api/config/client.rb | 18 ++++++++++-------- lib/iex/api/config/logger.rb | 16 ++++++++-------- spec/iex/client_spec.rb | 20 ++++++++++++++++++++ spec/iex/config/client_spec.rb | 27 +++++++++++++++++++++++++++ 8 files changed, 86 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b355072..31ceeee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * [#84](https://github.com/dblock/iex-ruby-client/pull/84): Added support for Advanced Stats API - [@FanaHOVA](https://github.com/fanahova). * [#86](https://github.com/dblock/iex-ruby-client/issues/86): Added advanced logger configuration to `config.logger`. Now a hash with keys `:instance, :options, and :proc` can be passed and used directly with Faraday - [@agrberg](https://github.com/agrberg). * [#88](https://github.com/dblock/iex-ruby-client/pull/88): Updated logger configuration to work like `Config` class and allow attribute and block based configuration - [@agrberg](https://github.com/agrberg). +* [#89](https://github.com/dblock/iex-ruby-client/pull/89): Backfill breaking `Config#logger` setting change from [#88](https://github.com/dblock/iex-ruby-client/pull/88) - [@agrberg](https://github.com/agrberg). * Your contribution here. ### 1.2.0 (2020/09/01) diff --git a/README.md b/README.md index 5fc0830..f0c004c 100644 --- a/README.md +++ b/README.md @@ -445,7 +445,7 @@ client.post('ref-data/isin', isin: ['US0378331005'], token: 'secret_token') # [{ You can configure client options globally or directly with a `IEX::Api::Client` instance. ```ruby -IEX::Api::Client.configure do |config| +IEX::Api.configure do |config| config.publishable_token = ENV['IEX_API_PUBLISHABLE_TOKEN'] config.endpoint = 'https://sandbox.iexapis.com/v1' # use sandbox environment end @@ -478,11 +478,23 @@ referer | Optional string for HTTP `Referer` header, enables token d Faraday will not log HTTP requests by default. In order to do this you can either provide a `logger` instance or configuration attributes to `IEX::Api::Client`. Configuration allows you to supply the `instance`, `options`, and `proc` to [Faraday](https://lostisland.github.io/faraday/middleware/logger#include-and-exclude-headersbodies). ```ruby -IEX::Api::Client.configure do |config| - config.logger.instance = Logger.new(STDOUT) +logger_instance = Logger.new(STDOUT) + +IEX::Api.configure do |config| + config.logger.instance = logger_instance config.logger.options = { bodies: true } config.logger.proc = proc { |logger| logger.filter(/T?[sp]k_\w+/i, '[REMOVED]') } end +# or +IEX::Api.logger do |logger| + logger.instance = logger_instance + logger.options = … + logger.proc = … +end +# or +IEX::Api.logger = logger_instance +# or +IEX::Api::Client.new(logger: logger_instance) ``` ## Sandbox Environment diff --git a/lib/iex/api.rb b/lib/iex/api.rb index 72c6829..9f8cab5 100644 --- a/lib/iex/api.rb +++ b/lib/iex/api.rb @@ -16,6 +16,6 @@ require_relative 'endpoints/ref_data' require_relative 'endpoints/stock_market' -require_relative 'api/config/client' require_relative 'api/config/logger' +require_relative 'api/config/client' require_relative 'api/client' diff --git a/lib/iex/api/client.rb b/lib/iex/api/client.rb index 8ebce6d..6181885 100644 --- a/lib/iex/api/client.rb +++ b/lib/iex/api/client.rb @@ -1,5 +1,8 @@ module IEX module Api + extend Config::Client::Accessor + extend Config::Logger::Accessor + class Client include Endpoints::AdvancedStats include Endpoints::Chart @@ -31,6 +34,7 @@ def initialize(options = {}) send("#{key}=", options[key] || IEX::Api.config.send(key)) end @logger = Config::Logger.dup + @logger.instance = options[:logger] if options.key?(:logger) end end end diff --git a/lib/iex/api/config/client.rb b/lib/iex/api/config/client.rb index 08ff640..9c520a4 100644 --- a/lib/iex/api/config/client.rb +++ b/lib/iex/api/config/client.rb @@ -16,6 +16,8 @@ module Client ].freeze class << self + include Config::Logger::Accessor + attr_accessor(*ATTRIBUTES) def reset! @@ -32,16 +34,16 @@ def reset! self.timeout = nil end end - end - end - class << self - def configure - block_given? ? yield(Config::Client) : Config::Client - end + module Accessor + def configure + block_given? ? yield(Config::Client) : Config::Client + end - def config - Config::Client + def config + Config::Client + end + end end end end diff --git a/lib/iex/api/config/logger.rb b/lib/iex/api/config/logger.rb index 83aec62..505b94d 100644 --- a/lib/iex/api/config/logger.rb +++ b/lib/iex/api/config/logger.rb @@ -17,16 +17,16 @@ def reset! self.proc = nil end end - end - end - class << self - def logger - block_given? ? yield(Config::Logger) : Config::Logger - end + module Accessor + def logger + block_given? ? yield(Config::Logger) : Config::Logger + end - def logger=(instance) - logger.instance = instance + def logger=(instance) + logger.instance = instance + end + end end end end diff --git a/spec/iex/client_spec.rb b/spec/iex/client_spec.rb index a6a1e8c..ef9453d 100644 --- a/spec/iex/client_spec.rb +++ b/spec/iex/client_spec.rb @@ -120,6 +120,26 @@ expect(client.send(:connection).builder.handlers).to include ::Faraday::Response::Logger end end + + context 'when assigning through `configure.logger`' do + it 'sets the logger' do + IEX::Api.configure.logger = logger + expect(client.logger.instance).to eq(logger) + end + end + + context 'when passing in at initialization' do + it 'sets the logger' do + client = described_class.new(logger: logger) + expect(client.logger.instance).to eq(logger) + end + + it 'can overwrite a set logger' do + IEX::Api.logger = logger + client = described_class.new(logger: nil) + expect(client.logger.instance).to be_nil + end + end end end diff --git a/spec/iex/config/client_spec.rb b/spec/iex/config/client_spec.rb index ad4974c..b358ce5 100644 --- a/spec/iex/config/client_spec.rb +++ b/spec/iex/config/client_spec.rb @@ -19,4 +19,31 @@ expect(IEX::Api.config.endpoint).to eq 'updated' end end + + context 'when configuring the logger' do + after { IEX::Api.configure.logger.reset! } + + let(:logger) { Logger.new(STDOUT) } + + describe '#logger=' do + it 'updates IEX::Api.config correctly' do + expect do + IEX::Api.configure { |config| config.logger = logger } + end.to change(IEX::Api.config.logger, :instance).from(nil).to(logger) + end + + it 'updates IEX::Api.logger correctly' do + expect do + IEX::Api.configure { |config| config.logger = logger } + end.to change(IEX::Api.logger, :instance).from(nil).to(logger) + end + end + + describe '#logger' do + it 'accesses the current logger' do + expect { IEX::Api.logger = logger } + .to change(IEX::Api.config.logger, :instance).from(nil).to(logger) + end + end + end end