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

Better resource_url method #663

Merged
merged 2 commits into from
Jul 19, 2018
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
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('.', '/')}"
Copy link
Contributor

Choose a reason for hiding this comment

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

A bit disappointing that we have the same code and comment twice. Is there a way to share the code? :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Haha, you mean from stripe-php?

Maybe once all these languages are running on the LLVM! ;)

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