Skip to content

Commit

Permalink
Ensure backward compatibility w/ previous logger config
Browse files Browse the repository at this point in the history
Extract Config accessory to module for clearer inclusion/extension into IEX::Api
  • Loading branch information
agrberg committed Oct 31, 2020
1 parent 3948bb5 commit 46365db
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/iex/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 4 additions & 0 deletions lib/iex/api/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module IEX
module Api
extend Config::Client::Accessor
extend Config::Logger::Accessor

class Client
include Endpoints::AdvancedStats
include Endpoints::Chart
Expand Down Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions lib/iex/api/config/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module Client
].freeze

class << self
include Config::Logger::Accessor

attr_accessor(*ATTRIBUTES)

def reset!
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib/iex/api/config/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/iex/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions spec/iex/config/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 46365db

Please sign in to comment.