From b4e57ea98181f71cf42b95447c96efa230d07c34 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Thu, 19 Jul 2018 14:23:07 +0200 Subject: [PATCH 1/2] Better resource_url method --- lib/stripe/api_resource.rb | 4 +++- lib/stripe/application_fee.rb | 4 ---- lib/stripe/country_spec.rb | 4 ---- lib/stripe/ephemeral_key.rb | 4 ---- lib/stripe/exchange_rate.rb | 4 ---- lib/stripe/issuer_fraud_record.rb | 4 ---- lib/stripe/order_return.rb | 4 ---- lib/stripe/payment_intent.rb | 4 ---- lib/stripe/singleton_api_resource.rb | 4 +++- lib/stripe/subscription_item.rb | 4 ---- test/stripe/api_operations_test.rb | 3 +++ 11 files changed, 9 insertions(+), 34 deletions(-) diff --git a/lib/stripe/api_resource.rb b/lib/stripe/api_resource.rb index d31d0c463..c6ad6e9b3 100644 --- a/lib/stripe/api_resource.rb +++ b/lib/stripe/api_resource.rb @@ -19,7 +19,9 @@ def self.resource_url if self == APIResource raise NotImplementedError, "APIResource is an abstract class. You should perform actions on its subclasses (Charge, Customer, etc.)" end - "/v1/#{CGI.escape(class_name.downcase)}s" + # Namespaces are separated in object names with periods (.) and in URLs + # with forward slashes (/), so replace the former with the latter. + "/v1/#{self::OBJECT_NAME.downcase.tr('.', '/')}s" end # A metaprogramming call that specifies that a field of a resource can be diff --git a/lib/stripe/application_fee.rb b/lib/stripe/application_fee.rb index 0691a70a4..9b3b08cf7 100644 --- a/lib/stripe/application_fee.rb +++ b/lib/stripe/application_fee.rb @@ -9,10 +9,6 @@ class ApplicationFee < APIResource nested_resource_class_methods :refund, operations: %i[create retrieve update list] - def self.resource_url - "/v1/application_fees" - end - # If you don't need access to an updated fee object after the refund, it's # more performant to just call `fee.refunds.create` directly. def refund(params = {}, opts = {}) diff --git a/lib/stripe/country_spec.rb b/lib/stripe/country_spec.rb index 4bae8ffd4..cddd9b8f5 100644 --- a/lib/stripe/country_spec.rb +++ b/lib/stripe/country_spec.rb @@ -5,9 +5,5 @@ class CountrySpec < APIResource extend Stripe::APIOperations::List OBJECT_NAME = "country_spec".freeze - - def self.resource_url - "/v1/country_specs" - end end end diff --git a/lib/stripe/ephemeral_key.rb b/lib/stripe/ephemeral_key.rb index 96c18493c..c5cac7034 100644 --- a/lib/stripe/ephemeral_key.rb +++ b/lib/stripe/ephemeral_key.rb @@ -7,10 +7,6 @@ class EphemeralKey < APIResource OBJECT_NAME = "ephemeral_key".freeze - def self.resource_url - "/v1/ephemeral_keys" - end - def self.create(params = {}, opts = {}) opts = Util.normalize_opts(opts) raise ArgumentError, "stripe_version must be specified to create an ephemeral key" unless opts[:stripe_version] diff --git a/lib/stripe/exchange_rate.rb b/lib/stripe/exchange_rate.rb index 2db05dfac..427d5e0aa 100644 --- a/lib/stripe/exchange_rate.rb +++ b/lib/stripe/exchange_rate.rb @@ -5,9 +5,5 @@ class ExchangeRate < APIResource extend Stripe::APIOperations::List OBJECT_NAME = "exchange_rate".freeze - - def self.resource_url - "/v1/exchange_rates" - end end end diff --git a/lib/stripe/issuer_fraud_record.rb b/lib/stripe/issuer_fraud_record.rb index 835b5a8c1..76b12488b 100644 --- a/lib/stripe/issuer_fraud_record.rb +++ b/lib/stripe/issuer_fraud_record.rb @@ -5,9 +5,5 @@ class IssuerFraudRecord < APIResource extend Stripe::APIOperations::List OBJECT_NAME = "issuer_fraud_record".freeze - - def self.resource_url - "/v1/issuer_fraud_records" - end end end diff --git a/lib/stripe/order_return.rb b/lib/stripe/order_return.rb index 6940a6f57..d33df79f1 100644 --- a/lib/stripe/order_return.rb +++ b/lib/stripe/order_return.rb @@ -5,9 +5,5 @@ class OrderReturn < APIResource extend Stripe::APIOperations::List OBJECT_NAME = "order_return".freeze - - def self.resource_url - "/v1/order_returns" - end end end diff --git a/lib/stripe/payment_intent.rb b/lib/stripe/payment_intent.rb index f0fb48b21..b9c8b30a6 100644 --- a/lib/stripe/payment_intent.rb +++ b/lib/stripe/payment_intent.rb @@ -9,10 +9,6 @@ class PaymentIntent < APIResource OBJECT_NAME = "payment_intent".freeze - def self.resource_url - "/v1/payment_intents" - end - def cancel resp, api_key = request(:post, resource_url + "/cancel") initialize_from(resp.data, api_key) diff --git a/lib/stripe/singleton_api_resource.rb b/lib/stripe/singleton_api_resource.rb index 00abb51ff..7f735a82c 100644 --- a/lib/stripe/singleton_api_resource.rb +++ b/lib/stripe/singleton_api_resource.rb @@ -6,7 +6,9 @@ def self.resource_url if self == SingletonAPIResource raise NotImplementedError, "SingletonAPIResource is an abstract class. You should perform actions on its subclasses (Account, etc.)" end - "/v1/#{CGI.escape(class_name.downcase)}" + # Namespaces are separated in object names with periods (.) and in URLs + # with forward slashes (/), so replace the former with the latter. + "/v1/#{self::OBJECT_NAME.downcase.tr('.', '/')}" end def resource_url diff --git a/lib/stripe/subscription_item.rb b/lib/stripe/subscription_item.rb index 91689feb1..2e8704fca 100644 --- a/lib/stripe/subscription_item.rb +++ b/lib/stripe/subscription_item.rb @@ -8,9 +8,5 @@ class SubscriptionItem < APIResource include Stripe::APIOperations::Save OBJECT_NAME = "subscription_item".freeze - - def self.resource_url - "/v1/subscription_items" - end end end diff --git a/test/stripe/api_operations_test.rb b/test/stripe/api_operations_test.rb index 962eaaea3..bad71667e 100644 --- a/test/stripe/api_operations_test.rb +++ b/test/stripe/api_operations_test.rb @@ -8,6 +8,8 @@ class ApiOperationsTest < Test::Unit::TestCase class UpdateableResource < APIResource include Stripe::APIOperations::Save + OBJECT_NAME = "updateableresource".freeze + def self.protected_fields [:protected] end @@ -33,6 +35,7 @@ def self.protected_fields context ".nested_resource_class_methods" do class MainResource < APIResource extend Stripe::APIOperations::NestedResource + OBJECT_NAME = "mainresource".freeze nested_resource_class_methods :nested, operations: %i[create retrieve update delete list] end From 73eb3fec7780abae53ce8fa66091853735505150 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Thu, 19 Jul 2018 14:23:18 +0200 Subject: [PATCH 2/2] Regenerate .rubocop_todo.yml --- .rubocop_todo.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 70857e8f6..2b9b5d4a5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2018-06-28 10:59:56 -0400 using RuboCop version 0.50.0. +# on 2018-07-19 14:22:24 +0200 using RuboCop version 0.50.0. # 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 @@ -24,7 +24,7 @@ Metrics/ClassLength: Metrics/CyclomaticComplexity: Max: 15 -# Offense count: 269 +# Offense count: 278 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Metrics/LineLength: @@ -38,7 +38,7 @@ Metrics/MethodLength: # Offense count: 1 # Configuration parameters: CountComments. Metrics/ModuleLength: - Max: 350 + Max: 308 # Offense count: 6 # Configuration parameters: CountKeywordArgs. @@ -55,6 +55,6 @@ Style/ClassVars: - 'lib/stripe/stripe_object.rb' - 'test/stripe/api_resource_test.rb' -# Offense count: 55 +# Offense count: 56 Style/Documentation: Enabled: false