Skip to content

Commit

Permalink
Autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe committed Nov 28, 2023
1 parent 633ca27 commit 2d6249f
Show file tree
Hide file tree
Showing 111 changed files with 300 additions and 325 deletions.
76 changes: 65 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,87 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-07-30 09:56:31 +0800 using RuboCop version 0.73.0.
# on 2023-11-28 01:02:03 UTC using RuboCop version 1.57.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 23
# Offense count: 2
Lint/HashCompareByIdentity:
Exclude:
- 'lib/stripe/stripe_client.rb'

# Offense count: 26
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 51
Max: 50

# Offense count: 12
# Configuration parameters: CountComments.
# Offense count: 9
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 694
Max: 592

# Offense count: 12
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 15
Max: 12

# Offense count: 6
# Configuration parameters: CountKeywordArgs.
# Offense count: 9
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Metrics/ParameterLists:
Max: 7

# Offense count: 8
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 17
Max: 12

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: MinBranchesCount.
Style/CaseLikeIf:
Exclude:
- 'lib/stripe/stripe_configuration.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/CombinableLoops:
Exclude:
- 'lib/stripe/stripe_client.rb'

# Offense count: 86
# Offense count: 39
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: AllowSplatArgument.
Style/HashConversion:
Exclude:
- 'lib/stripe/stripe_client.rb'

# Offense count: 3
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength.
# AllowedMethods: present?, blank?, presence, try, try!
Style/SafeNavigation:
Exclude:
- 'lib/stripe/instrumentation.rb'
- 'lib/stripe/stripe_response.rb'

# Offense count: 15
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: Mode.
Style/StringConcatenation:
Exclude:
- 'lib/stripe.rb'
- 'lib/stripe/connection_manager.rb'
- 'lib/stripe/multipart_encoder.rb'
- 'lib/stripe/oauth.rb'
- 'lib/stripe/resources/bank_account.rb'
- 'lib/stripe/resources/source.rb'
- 'lib/stripe/stripe_client.rb'
- 'test/stripe/api_resource_test.rb'
- 'test/stripe/stripe_client_test.rb'
- 'test/stripe/webhook_test.rb'
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ task :update_certs do
require "uri"

fetch_file "https://curl.haxx.se/ca/cacert.pem",
::File.expand_path("../lib/data/ca-certificates.crt", __FILE__)
File.expand_path("lib/data/ca-certificates.crt", __dir__)
end

#
# helpers
#

def fetch_file(uri, dest)
::File.open(dest, "w") do |file|
File.open(dest, "w") do |file|
resp = Net::HTTP.get_response(URI.parse(uri))
unless resp.code.to_i == 200
abort("bad response when fetching: #{uri}\n" \
"Status #{resp.code}: #{resp.body}")
"Status #{resp.code}: #{resp.body}")
end
file.write(resp.body)
puts "Successfully fetched: #{uri}"
Expand Down
2 changes: 1 addition & 1 deletion bin/stripe-console
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require "irb"
require "irb/completion"

require "#{::File.dirname(__FILE__)}/../lib/stripe"
require "#{File.dirname(__FILE__)}/../lib/stripe"

# Config IRB to enable --simple-prompt and auto indent
IRB.conf[:PROMPT_MODE] = :SIMPLE
Expand Down
8 changes: 3 additions & 5 deletions lib/stripe/api_operations/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def execute_resource_request_stream(method, url,

raise ArgumentError,
"request option '#{opt}' should be a string value " \
"(was a #{val.class})"
"(was a #{val.class})"
end
end

Expand All @@ -96,14 +96,12 @@ def execute_resource_request_stream(method, url,

raise ArgumentError,
"request params should be either a Hash or nil " \
"(was a #{params.class})"
"(was a #{params.class})"
end

private def warn_on_opts_in_params(params)
Util::OPTS_USER_SPECIFIED.each do |opt|
if params.key?(opt)
warn("WARNING: '#{opt}' should be in opts instead of params.")
end
warn("WARNING: '#{opt}' should be in opts instead of params.") if params.key?(opt)
end
end
end
Expand Down
12 changes: 5 additions & 7 deletions lib/stripe/api_operations/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ module ClassMethods
# {APIOperations::Request.execute_resource_request}.
def update(id, params = {}, opts = {})
params.each_key do |k|
if protected_fields.include?(k)
raise ArgumentError, "Cannot update protected field: #{k}"
end
raise ArgumentError, "Cannot update protected field: #{k}" if protected_fields.include?(k)
end

request_stripe_object(
Expand Down Expand Up @@ -64,17 +62,17 @@ def save(params = {}, opts = {})

values = serialize_params(self).merge(params)

# note that id gets removed here our call to #url above has already
# NOTE: that id gets removed here our call to #url above has already
# generated a uri for this object with an identifier baked in
values.delete(:id)

resp, opts = execute_resource_request(:post, save_url, values, opts)
initialize_from(resp.data, opts)
end
extend Gem::Deprecate
deprecate :save, "the `update` class method (for examples"\
" see https://github.com/stripe/stripe-ruby"\
"/wiki/Migration-guide-for-v8)", 2022, 11
deprecate :save, "the `update` class method (for examples " \
"see https://github.com/stripe/stripe-ruby" \
"/wiki/Migration-guide-for-v8)", 2022, 11

def self.included(base)
# Set `metadata` as additive so that when it's set directly we remember
Expand Down
4 changes: 2 additions & 2 deletions lib/stripe/api_resource_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def self.custom_method(name, http_verb:, http_path: nil)
end

def self.resource_url
"/v1/test_helpers/"\
"#{self::RESOURCE_CLASS::OBJECT_NAME.downcase.tr('.', '/')}s"
"/v1/test_helpers/" \
"#{self::RESOURCE_CLASS::OBJECT_NAME.downcase.tr('.', '/')}s"
end

def resource_url
Expand Down
10 changes: 4 additions & 6 deletions lib/stripe/connection_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ def execute_request(method, uri, body: nil, headers: nil, query: nil,

connection.open_timeout = config.open_timeout
connection.read_timeout = config.read_timeout
if connection.respond_to?(:write_timeout=)
connection.write_timeout = config.write_timeout
end
connection.write_timeout = config.write_timeout if connection.respond_to?(:write_timeout=)

connection.use_ssl = uri.scheme == "https"

Expand Down Expand Up @@ -192,9 +190,9 @@ def execute_request(method, uri, body: nil, headers: nil, query: nil,

@verify_ssl_warned = true
warn("WARNING: Running without SSL cert verification. " \
"You should never do this in production. " \
"Execute `Stripe.verify_ssl_certs = true` to enable " \
"verification.")
"You should never do this in production. " \
"Execute `Stripe.verify_ssl_certs = true` to enable " \
"verification.")
end
end
end
14 changes: 3 additions & 11 deletions lib/stripe/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@ module Stripe
# StripeError is the base error from which all other more specific Stripe
# errors derive.
class StripeError < StandardError
attr_reader :message
attr_reader :message, :code, :error, :http_body, :http_headers, :http_status, :json_body, :request_id

# Response contains a StripeResponse object that has some basic information
# about the response that conveyed the error.
attr_accessor :response

attr_reader :code
attr_reader :error
attr_reader :http_body
attr_reader :http_headers
attr_reader :http_status
attr_reader :json_body # equivalent to #data
attr_reader :request_id
attr_accessor :response # equivalent to #data

# Initializes a StripeError.
def initialize(message = nil, http_status: nil, http_body: nil,
def initialize(message = nil, http_status: nil, http_body: nil, # rubocop:todo Lint/MissingSuper
json_body: nil, http_headers: nil, code: nil)
@message = message
@http_status = http_status
Expand Down
26 changes: 5 additions & 21 deletions lib/stripe/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module Stripe
class Instrumentation
# Event emitted on `request_begin` callback.
class RequestBeginEvent
attr_reader :method
attr_reader :path
attr_reader :method, :path

# Arbitrary user-provided data in the form of a Ruby hash that's passed
# from subscribers on `request_begin` to subscribers on `request_end`.
Expand All @@ -27,16 +26,8 @@ def initialize(method:, path:, user_data:)

# Event emitted on `request_end` callback.
class RequestEndEvent
attr_reader :duration
attr_reader :http_status
attr_reader :method
attr_reader :num_retries
attr_reader :path
attr_reader :request_id
attr_reader :response_header
attr_reader :response_body
attr_reader :request_header
attr_reader :request_body
attr_reader :duration, :http_status, :method, :num_retries, :path, :request_id, :response_header, :response_body,
:request_header, :request_body

# Arbitrary user-provided data in the form of a Ruby hash that's passed
# from subscribers on `request_begin` to subscribers on `request_end`.
Expand All @@ -62,12 +53,7 @@ def initialize(request_context:, response_context:,
end

class RequestContext
attr_reader :duration
attr_reader :method
attr_reader :path
attr_reader :request_id
attr_reader :body
attr_reader :header
attr_reader :duration, :method, :path, :request_id, :body, :header

def initialize(duration:, context:, header:)
@duration = duration
Expand All @@ -80,9 +66,7 @@ def initialize(duration:, context:, header:)
end

class ResponseContext
attr_reader :http_status
attr_reader :body
attr_reader :header
attr_reader :http_status, :body, :header

def initialize(http_status:, response:)
@http_status = http_status
Expand Down
14 changes: 7 additions & 7 deletions lib/stripe/multipart_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ def encode(params)
end

private def write_field(name, data, filename:)
if !@first_field
@body << "\r\n"
else
if @first_field
@first_field = false
else
@body << "\r\n"
end

@body << "--#{@boundary}\r\n"

if filename
@body << %(Content-Disposition: form-data) +
@body << (%(Content-Disposition: form-data) +
%(; name="#{escape(name.to_s)}") +
%(; filename="#{escape(filename)}"\r\n)
%(; filename="#{escape(filename)}"\r\n))
@body << %(Content-Type: application/octet-stream\r\n)
else
@body << %(Content-Disposition: form-data) +
%(; name="#{escape(name.to_s)}"\r\n)
@body << (%(Content-Disposition: form-data) +
%(; name="#{escape(name.to_s)}"\r\n))
end

@body << "\r\n"
Expand Down
12 changes: 6 additions & 6 deletions lib/stripe/oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def self.get_client_id(params = {})
client_id = params[:client_id] || Stripe.client_id
unless client_id
raise AuthenticationError, "No client_id provided. " \
'Set your client_id using "Stripe.client_id = <CLIENT-ID>". ' \
"You can find your client_ids in your Stripe dashboard at " \
"https://dashboard.stripe.com/account/applications/settings, " \
"after registering your account as a platform. See " \
"https://stripe.com/docs/connect/standalone-accounts for details, " \
"or email [email protected] if you have any questions."
'Set your client_id using "Stripe.client_id = <CLIENT-ID>". ' \
"You can find your client_ids in your Stripe dashboard at " \
"https://dashboard.stripe.com/account/applications/settings, " \
"after registering your account as a platform. See " \
"https://stripe.com/docs/connect/standalone-accounts for details, " \
"or email [email protected] if you have any questions."
end
client_id
end
Expand Down
16 changes: 6 additions & 10 deletions lib/stripe/resources/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,13 @@ def serialize_params(options = {})
end

def serialize_params_account(_obj, update_hash, options = {})
if (entity = @values[:legal_entity])
if (owners = entity[:additional_owners])
entity_update = update_hash[:legal_entity] ||= {}
entity_update[:additional_owners] =
serialize_additional_owners(entity, owners)
end
if (entity = @values[:legal_entity]) && (owners = entity[:additional_owners])
entity_update = update_hash[:legal_entity] ||= {}
entity_update[:additional_owners] =
serialize_additional_owners(entity, owners)
end
if (individual = @values[:individual])
if individual.is_a?(Person) && !update_hash.key?(:individual)
update_hash[:individual] = individual.serialize_params(options)
end
if (individual = @values[:individual]) && (individual.is_a?(Person) && !update_hash.key?(:individual))
update_hash[:individual] = individual.serialize_params(options)
end
update_hash
end
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/resources/alipay_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def resource_url
end

"#{Customer.resource_url}/#{CGI.escape(customer)}/sources" \
"/#{CGI.escape(id)}"
"/#{CGI.escape(id)}"
end

def self.update(_id, _params = nil, _opts = nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/resources/application_fee_refund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ApplicationFeeRefund < APIResource

def resource_url
"#{ApplicationFee.resource_url}/#{CGI.escape(fee)}/refunds" \
"/#{CGI.escape(id)}"
"/#{CGI.escape(id)}"
end

def self.update(_id, _params = nil, _opts = nil)
Expand Down
Loading

0 comments on commit 2d6249f

Please sign in to comment.