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

Rename Stripe.configuration to Stripe.config #972

Merged
merged 1 commit into from
Apr 2, 2021
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
44 changes: 22 additions & 22 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,36 @@ module Stripe

@app_info = nil

@configuration = Stripe::StripeConfiguration.setup
@config = Stripe::StripeConfiguration.setup

class << self
extend Forwardable

attr_reader :configuration
attr_reader :config

# User configurable options
def_delegators :@configuration, :api_key, :api_key=
def_delegators :@configuration, :api_version, :api_version=
def_delegators :@configuration, :stripe_account, :stripe_account=
def_delegators :@configuration, :api_base, :api_base=
def_delegators :@configuration, :uploads_base, :uploads_base=
def_delegators :@configuration, :connect_base, :connect_base=
def_delegators :@configuration, :open_timeout, :open_timeout=
def_delegators :@configuration, :read_timeout, :read_timeout=
def_delegators :@configuration, :write_timeout, :write_timeout=
def_delegators :@configuration, :proxy, :proxy=
def_delegators :@configuration, :verify_ssl_certs, :verify_ssl_certs=
def_delegators :@configuration, :ca_bundle_path, :ca_bundle_path=
def_delegators :@configuration, :log_level, :log_level=
def_delegators :@configuration, :logger, :logger=
def_delegators :@configuration, :max_network_retries, :max_network_retries=
def_delegators :@configuration, :enable_telemetry=, :enable_telemetry?
def_delegators :@configuration, :client_id=, :client_id
def_delegators :@config, :api_key, :api_key=
def_delegators :@config, :api_version, :api_version=
def_delegators :@config, :stripe_account, :stripe_account=
def_delegators :@config, :api_base, :api_base=
def_delegators :@config, :uploads_base, :uploads_base=
def_delegators :@config, :connect_base, :connect_base=
def_delegators :@config, :open_timeout, :open_timeout=
def_delegators :@config, :read_timeout, :read_timeout=
def_delegators :@config, :write_timeout, :write_timeout=
def_delegators :@config, :proxy, :proxy=
def_delegators :@config, :verify_ssl_certs, :verify_ssl_certs=
def_delegators :@config, :ca_bundle_path, :ca_bundle_path=
def_delegators :@config, :log_level, :log_level=
def_delegators :@config, :logger, :logger=
def_delegators :@config, :max_network_retries, :max_network_retries=
def_delegators :@config, :enable_telemetry=, :enable_telemetry?
def_delegators :@config, :client_id=, :client_id

# Internal configurations
def_delegators :@configuration, :max_network_retry_delay
def_delegators :@configuration, :initial_network_retry_delay
def_delegators :@configuration, :ca_store
def_delegators :@config, :max_network_retry_delay
def_delegators :@config, :initial_network_retry_delay
def_delegators :@config, :ca_store
end

# Gets the application for a plugin that's identified some. See
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/connection_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ConnectionManager
attr_reader :last_used
attr_reader :config

def initialize(config = Stripe.configuration)
def initialize(config = Stripe.config)
@config = config
@active_connections = {}
@last_used = Util.monotonic_time
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/resources/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.create(params = {}, opts = {})
end
end

config = opts[:client]&.config || Stripe.configuration
config = opts[:client]&.config || Stripe.config
opts = {
api_base: config.uploads_base,
content_type: MultipartEncoder::MULTIPART_FORM_DATA,
Expand Down
15 changes: 7 additions & 8 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def initialize(config_arg = {})

@config = case config_arg
when Hash
Stripe.configuration.reverse_duplicate_merge(config_arg)
Stripe.config.reverse_duplicate_merge(config_arg)
when Stripe::ConnectionManager
# Supports accepting a connection manager object for backwards
# compatibility only, and that use is DEPRECATED.
Stripe.configuration.dup
Stripe.config.dup
when Stripe::StripeConfiguration
config_arg
when String
Stripe.configuration.reverse_duplicate_merge(
Stripe.config.reverse_duplicate_merge(
{ api_key: config_arg }
)
else
Expand Down Expand Up @@ -91,13 +91,12 @@ def self.clear_all_connection_managers(config: nil)

# A default client for the current thread.
def self.default_client
current_thread_context.default_client ||=
StripeClient.new(Stripe.configuration)
current_thread_context.default_client ||= StripeClient.new(Stripe.config)
end

# A default connection manager for the current thread scoped to the
# configuration object that may be provided.
def self.default_connection_manager(config = Stripe.configuration)
def self.default_connection_manager(config = Stripe.config)
current_thread_context.default_connection_managers[config.key] ||= begin
connection_manager = ConnectionManager.new(config)

Expand All @@ -114,7 +113,7 @@ def self.default_connection_manager(config = Stripe.configuration)
# both socket errors that may represent an intermittent problem and some
# special HTTP statuses.
def self.should_retry?(error,
method:, num_retries:, config: Stripe.configuration)
method:, num_retries:, config: Stripe.config)
return false if num_retries >= config.max_network_retries

case error
Expand Down Expand Up @@ -161,7 +160,7 @@ def self.should_retry?(error,
end
end

def self.sleep_time(num_retries, config: Stripe.configuration)
def self.sleep_time(num_retries, config: Stripe.config)
# Apply exponential backoff with initial_network_retry_delay on the
# number of num_retries so far as inputs. Do not allow the number to
# exceed max_network_retry_delay.
Expand Down
6 changes: 3 additions & 3 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def self.convert_to_stripe_object(data, opts = {})
end

def self.log_error(message, data = {})
config = data.delete(:config) || Stripe.configuration
config = data.delete(:config) || Stripe.config
logger = config.logger || Stripe.logger
if !logger.nil? ||
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_ERROR
Expand All @@ -86,7 +86,7 @@ def self.log_error(message, data = {})
end

def self.log_info(message, data = {})
config = data.delete(:config) || Stripe.configuration
config = data.delete(:config) || Stripe.config
logger = config.logger || Stripe.logger
if !logger.nil? ||
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_INFO
Expand All @@ -96,7 +96,7 @@ def self.log_info(message, data = {})
end

def self.log_debug(message, data = {})
config = data.delete(:config) || Stripe.configuration
config = data.delete(:config) || Stripe.config
logger = config.logger || Stripe.logger
if !logger.nil? ||
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_DEBUG
Expand Down
42 changes: 21 additions & 21 deletions test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StripeClientTest < Test::Unit::TestCase
end

should "support passing a full configuration object" do
config = Stripe.configuration.reverse_duplicate_merge({ api_key: "test_123", open_timeout: 100 })
config = Stripe.config.reverse_duplicate_merge({ api_key: "test_123", open_timeout: 100 })
client = StripeClient.new(config)
assert_equal config, client.config
end
Expand Down Expand Up @@ -393,7 +393,7 @@ class StripeClientTest < Test::Unit::TestCase
context ".sleep_time" do
should "should grow exponentially" do
StripeClient.stubs(:rand).returns(1)
Stripe.configuration.stubs(:max_network_retry_delay).returns(999)
Stripe.config.stubs(:max_network_retry_delay).returns(999)
assert_equal(Stripe.initial_network_retry_delay, StripeClient.sleep_time(1))
assert_equal(Stripe.initial_network_retry_delay * 2, StripeClient.sleep_time(2))
assert_equal(Stripe.initial_network_retry_delay * 4, StripeClient.sleep_time(3))
Expand All @@ -402,8 +402,8 @@ class StripeClientTest < Test::Unit::TestCase

should "enforce the max_network_retry_delay" do
StripeClient.stubs(:rand).returns(1)
Stripe.configuration.stubs(:initial_network_retry_delay).returns(1)
Stripe.configuration.stubs(:max_network_retry_delay).returns(2)
Stripe.config.stubs(:initial_network_retry_delay).returns(1)
Stripe.config.stubs(:max_network_retry_delay).returns(2)
assert_equal(1, StripeClient.sleep_time(1))
assert_equal(2, StripeClient.sleep_time(2))
assert_equal(2, StripeClient.sleep_time(3))
Expand All @@ -413,8 +413,8 @@ class StripeClientTest < Test::Unit::TestCase
should "add some randomness" do
random_value = 0.8
StripeClient.stubs(:rand).returns(random_value)
Stripe.configuration.stubs(:initial_network_retry_delay).returns(1)
Stripe.configuration.stubs(:max_network_retry_delay).returns(8)
Stripe.config.stubs(:initial_network_retry_delay).returns(1)
Stripe.config.stubs(:max_network_retry_delay).returns(8)

base_value = Stripe.initial_network_retry_delay * (0.5 * (1 + random_value))

Expand All @@ -436,8 +436,8 @@ class StripeClientTest < Test::Unit::TestCase
end

# Set the global configuration to be different than the client
Stripe.configuration.stubs(:initial_network_retry_delay).returns(100)
Stripe.configuration.stubs(:max_network_retry_delay).returns(200)
Stripe.config.stubs(:initial_network_retry_delay).returns(100)
Stripe.config.stubs(:max_network_retry_delay).returns(200)

assert_equal(1, StripeClient.sleep_time(1, config: config))
assert_equal(2, StripeClient.sleep_time(2, config: config))
Expand Down Expand Up @@ -478,9 +478,9 @@ class StripeClientTest < Test::Unit::TestCase
# this.
Util.stubs(:monotonic_time).returns(0.0)

# Stub the Stripe configuration so that mocha matchers will succeed. Currently,
# Stub the Stripe.config so that mocha matchers will succeed. Currently,
# mocha does not support using param matchers within hashes.
StripeClient.any_instance.stubs(:config).returns(Stripe.configuration)
StripeClient.any_instance.stubs(:config).returns(Stripe.config)
end

should "produce appropriate logging" do
Expand All @@ -493,12 +493,12 @@ class StripeClientTest < Test::Unit::TestCase
method: :post,
num_retries: 0,
path: "/v1/account",
config: Stripe.configuration)
config: Stripe.config)
Util.expects(:log_debug).with("Request details",
body: "",
idempotency_key: "abc",
query: nil,
config: Stripe.configuration)
config: Stripe.config)

Util.expects(:log_info).with("Response from Stripe API",
account: "acct_123",
Expand All @@ -509,17 +509,17 @@ class StripeClientTest < Test::Unit::TestCase
path: "/v1/account",
request_id: "req_123",
status: 200,
config: Stripe.configuration)
config: Stripe.config)
Util.expects(:log_debug).with("Response details",
body: body,
idempotency_key: "abc",
request_id: "req_123",
config: Stripe.configuration)
config: Stripe.config)
Util.expects(:log_debug).with("Dashboard link for request",
idempotency_key: "abc",
request_id: "req_123",
url: Util.request_id_dashboard_url("req_123", Stripe.api_key),
config: Stripe.configuration)
config: Stripe.config)

stub_request(:post, "#{Stripe.api_base}/v1/account")
.to_return(
Expand Down Expand Up @@ -549,7 +549,7 @@ class StripeClientTest < Test::Unit::TestCase
method: :post,
num_retries: 0,
path: "/v1/account",
config: Stripe.configuration)
config: Stripe.config)
Util.expects(:log_info).with("Response from Stripe API",
account: nil,
api_version: nil,
Expand All @@ -559,7 +559,7 @@ class StripeClientTest < Test::Unit::TestCase
path: "/v1/account",
request_id: nil,
status: 500,
config: Stripe.configuration)
config: Stripe.config)

error = {
code: "code",
Expand All @@ -575,7 +575,7 @@ class StripeClientTest < Test::Unit::TestCase
error_type: error[:type],
idempotency_key: nil,
request_id: nil,
config: Stripe.configuration)
config: Stripe.config)

stub_request(:post, "#{Stripe.api_base}/v1/account")
.to_return(
Expand All @@ -597,7 +597,7 @@ class StripeClientTest < Test::Unit::TestCase
method: :post,
num_retries: 0,
path: "/oauth/token",
config: Stripe.configuration)
config: Stripe.config)
Util.expects(:log_info).with("Response from Stripe API",
account: nil,
api_version: nil,
Expand All @@ -607,15 +607,15 @@ class StripeClientTest < Test::Unit::TestCase
path: "/oauth/token",
request_id: nil,
status: 400,
config: Stripe.configuration)
config: Stripe.config)

Util.expects(:log_error).with("Stripe OAuth error",
status: 400,
error_code: "invalid_request",
error_description: "No grant type specified",
idempotency_key: nil,
request_id: nil,
config: Stripe.configuration)
config: Stripe.config)

stub_request(:post, "#{Stripe.connect_base}/oauth/token")
.to_return(body: JSON.generate(error: "invalid_request",
Expand Down