Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backfill breaking change #89

Merged
merged 4 commits into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,3 @@
Style/AsciiComments:
Exclude:
- 'lib/iex/resources/quote.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, Autocorrect.
# SupportedStyles: module_function, extend_self
Style/ModuleFunction:
Exclude:
- 'lib/iex/api/config/client.rb'
- 'lib/iex/api/config/logger.rb'
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|
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't dig too deeply but going back to 1.2.0 before I started screwin' around there was no IEX::Api::Client.configure method. I believe this was a mistake in the docs between IEX::Api.configure and IEX::Api::Client.new(…)

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'
6 changes: 5 additions & 1 deletion 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 All @@ -22,7 +25,7 @@ class Client
include Cloud::Connection
include Cloud::Request

include Config::Client
attr_accessor(*Config::Client::ATTRIBUTES)

attr_reader :logger

Expand All @@ -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
46 changes: 24 additions & 22 deletions lib/iex/api/config/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module IEX
module Api
module Config
module Client
extend self

ATTRIBUTES = %i[
ca_file
ca_path
Expand All @@ -17,31 +15,35 @@ module Client
user_agent
].freeze

attr_accessor(*ATTRIBUTES)
class << self
include Config::Logger::Accessor

attr_accessor(*ATTRIBUTES)

def reset!
self.ca_file = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_FILE : nil
self.ca_path = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_DIR : nil
self.endpoint = 'https://cloud.iexapis.com/v1'
self.publishable_token = ENV['IEX_API_PUBLISHABLE_TOKEN']
self.secret_token = ENV['IEX_API_SECRET_TOKEN']
self.user_agent = "IEX Ruby Client/#{IEX::VERSION}"
def reset!
self.ca_file = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_FILE : nil
self.ca_path = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_DIR : nil
self.endpoint = 'https://cloud.iexapis.com/v1'
self.publishable_token = ENV['IEX_API_PUBLISHABLE_TOKEN']
self.secret_token = ENV['IEX_API_SECRET_TOKEN']
self.user_agent = "IEX Ruby Client/#{IEX::VERSION}"

self.open_timeout = nil
self.proxy = nil
self.referer = nil
self.timeout = nil
self.open_timeout = nil
self.proxy = nil
self.referer = nil
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
30 changes: 15 additions & 15 deletions lib/iex/api/config/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ module IEX
module Api
module Config
module Logger
extend self

ATTRIBUTES = %i[
instance
options
proc
].freeze

attr_accessor(*ATTRIBUTES)
class << self
attr_accessor(*ATTRIBUTES)

def reset!
self.instance = nil
self.options = {}
self.proc = nil
def reset!
self.instance = nil
self.options = {}
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
57 changes: 51 additions & 6 deletions spec/iex/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,44 @@
end
end
end

context 'logger option' do
let(:logger) { Logger.new(STDOUT) }

after { IEX::Api.logger.reset! }

context 'when assigning an instance' do
before { IEX::Api.logger = logger }

context '#initialize' do
it 'sets logger' do
expect(client.logger.instance).to eq(logger)
context 'when directly assigning `logger`' do
before { IEX::Api.logger = logger }

it 'sets logger' do
expect(client.logger.instance).to eq(logger)
end

it 'creates a connection with a logger' do
expect(client.send(:connection).builder.handlers).to include ::Faraday::Response::Logger
end
end
it 'creates a connection with a logger' do
expect(client.send(:connection).builder.handlers).to include ::Faraday::Response::Logger

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 Expand Up @@ -164,6 +188,27 @@
end
end
end

context 'when resetting/changing configuration' do
before do
IEX::Api.configure { |config| config.user_agent = 'custom/user-agent' }
end

it 'does not reset the client' do
expect { IEX::Api.config.reset! }.not_to change(client, :user_agent).from('custom/user-agent')
end

it 'effects the next client' do
pre_config_client = described_class.new
IEX::Api.configure { |config| config.user_agent = 'custom/user-agent-2' }
expect(described_class.new.user_agent).not_to eq(pre_config_client.user_agent)
end

it 'should not allow the client to reset' do
expect { client.reset! }.to raise_error(NoMethodError)
end
end

context 'without a token' do
let(:client) { described_class.new }
it 'results in an API key error', vcr: { cassette_name: 'client/access_denied' } do
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
1 change: 0 additions & 1 deletion spec/iex/config/logger_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'spec_helper'

describe IEX::Api::Config::Logger do
before { IEX::Api.logger.reset! }
after { IEX::Api.logger.reset! }

describe '#defaults' do
Expand Down