Skip to content

Commit

Permalink
Merge pull request #663 from stripe/ob-better-resource-url
Browse files Browse the repository at this point in the history
Better `resource_url` method
  • Loading branch information
ob-stripe authored Jul 19, 2018
2 parents 99a1d57 + 73eb3fe commit 87b758e
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 38 deletions.
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -38,7 +38,7 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 350
Max: 308

# Offense count: 6
# Configuration parameters: CountKeywordArgs.
Expand All @@ -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
4 changes: 3 additions & 1 deletion lib/stripe/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions lib/stripe/application_fee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down
4 changes: 0 additions & 4 deletions lib/stripe/country_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions lib/stripe/ephemeral_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 0 additions & 4 deletions lib/stripe/exchange_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions lib/stripe/issuer_fraud_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions lib/stripe/order_return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions lib/stripe/payment_intent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/stripe/singleton_api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions lib/stripe/subscription_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions test/stripe/api_operations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 87b758e

Please sign in to comment.