diff --git a/lib/stripe.rb b/lib/stripe.rb index 60c655e93..cf414309a 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -207,6 +207,8 @@ def self._uname_ver # DEPRECATED. Use `Util#encode_parameters` instead. def self.uri_encode(params) + Stripe::Util.warn_deprecated("Stripe.uri_encode", + extra: "Use Stripe::Util#encode_parameters instead.") Util.encode_parameters(params) end diff --git a/lib/stripe/api_operations/delete.rb b/lib/stripe/api_operations/delete.rb index 87d1b2322..8c8979e6e 100644 --- a/lib/stripe/api_operations/delete.rb +++ b/lib/stripe/api_operations/delete.rb @@ -4,7 +4,7 @@ module Delete def delete(params={}, opts={}) opts = Util.normalize_opts(opts) response, opts = request(:delete, url, params, opts) - refresh_from(response, opts) + initialize_from(response, opts) end end end diff --git a/lib/stripe/api_operations/update.rb b/lib/stripe/api_operations/update.rb index 4e6155d6d..0fab6093f 100644 --- a/lib/stripe/api_operations/update.rb +++ b/lib/stripe/api_operations/update.rb @@ -33,7 +33,7 @@ def save(params={}) values.delete(:id) response, opts = request(:post, req_url, values) - refresh_from(response, opts) + initialize_from(response, opts) end self end diff --git a/lib/stripe/api_resource.rb b/lib/stripe/api_resource.rb index 79397c633..67d06e7a0 100644 --- a/lib/stripe/api_resource.rb +++ b/lib/stripe/api_resource.rb @@ -22,7 +22,7 @@ def url def refresh response, opts = request(:get, url, @retrieve_params) - refresh_from(response, opts) + initialize_from(response, opts) end def self.retrieve(id, opts={}) diff --git a/lib/stripe/application_fee.rb b/lib/stripe/application_fee.rb index beeab5e1f..fa6512bee 100644 --- a/lib/stripe/application_fee.rb +++ b/lib/stripe/application_fee.rb @@ -8,7 +8,7 @@ def self.url def refund(params={}, opts={}) response, opts = request(:post, refund_url, params, opts) - refresh_from(response, opts) + initialize_from(response, opts) end private diff --git a/lib/stripe/charge.rb b/lib/stripe/charge.rb index e4d8491f7..4b93599c5 100644 --- a/lib/stripe/charge.rb +++ b/lib/stripe/charge.rb @@ -6,23 +6,23 @@ class Charge < APIResource def refund(params={}, opts={}) response, opts = request(:post, refund_url, params, opts) - refresh_from(response, opts) + initialize_from(response, opts) end def capture(params={}, opts={}) response, opts = request(:post, capture_url, params, opts) - refresh_from(response, opts) + initialize_from(response, opts) end def update_dispute(params={}, opts={}) response, opts = request(:post, dispute_url, params, opts) - refresh_from({ :dispute => response }, opts, true) + initialize_from({ :dispute => response }, opts, true) dispute end def close_dispute(params={}, opts={}) response, opts = request(:post, close_dispute_url, params, opts) - refresh_from(response, opts) + initialize_from(response, opts) end def mark_as_fraudulent @@ -30,7 +30,7 @@ def mark_as_fraudulent :fraud_details => { :user_report => 'fraudulent' } } response, opts = request(:post, url, params) - refresh_from(response, opts) + initialize_from(response, opts) end def mark_as_safe @@ -38,7 +38,7 @@ def mark_as_safe :fraud_details => { :user_report => 'safe' } } response, opts = request(:post, url, params) - refresh_from(response, opts) + initialize_from(response, opts) end private diff --git a/lib/stripe/customer.rb b/lib/stripe/customer.rb index e926499e2..47ce81442 100644 --- a/lib/stripe/customer.rb +++ b/lib/stripe/customer.rb @@ -37,25 +37,25 @@ def create_upcoming_invoice(params={}, opts={}) def cancel_subscription(params={}, opts={}) response, opts = request(:delete, subscription_url, params, opts) - refresh_from({ :subscription => response }, opts, true) + initialize_from({ :subscription => response }, opts, true) subscription end def update_subscription(params={}, opts={}) response, opts = request(:post, subscription_url, params, opts) - refresh_from({ :subscription => response }, opts, true) + initialize_from({ :subscription => response }, opts, true) subscription end def create_subscription(params={}, opts={}) response, opts = request(:post, subscriptions_url, params, opts) - refresh_from({ :subscription => response }, opts, true) + initialize_from({ :subscription => response }, opts, true) subscription end def delete_discount _, opts = request(:delete, discount_url) - refresh_from({ :discount => nil }, opts, true) + initialize_from({ :discount => nil }, opts, true) end private diff --git a/lib/stripe/dispute.rb b/lib/stripe/dispute.rb index f840bb50e..9d27450a1 100644 --- a/lib/stripe/dispute.rb +++ b/lib/stripe/dispute.rb @@ -6,7 +6,7 @@ class Dispute < APIResource def close(params={}, opts={}) response, opts = request(:post, close_url, params, opts) - refresh_from(response, opts) + initialize_from(response, opts) end def close_url diff --git a/lib/stripe/invoice.rb b/lib/stripe/invoice.rb index 5bf29ab8c..b21e652e5 100644 --- a/lib/stripe/invoice.rb +++ b/lib/stripe/invoice.rb @@ -11,7 +11,7 @@ def self.upcoming(params, opts={}) def pay(opts={}) response, opts = request(:post, pay_url, {}, opts) - refresh_from(response, opts) + initialize_from(response, opts) end private diff --git a/lib/stripe/order.rb b/lib/stripe/order.rb index 1c4206a37..35fcc0869 100644 --- a/lib/stripe/order.rb +++ b/lib/stripe/order.rb @@ -6,7 +6,7 @@ class Order < APIResource def pay(params, opts={}) response, opts = request(:post, pay_url, params, opts) - refresh_from(response, opts) + initialize_from(response, opts) end private diff --git a/lib/stripe/stripe_object.rb b/lib/stripe/stripe_object.rb index 1bfa7a4c6..0779864a5 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -22,7 +22,9 @@ def initialize(id=nil, opts={}) def self.construct_from(values, opts={}) values = Stripe::Util.symbolize_names(values) - self.new(values[:id]).refresh_from(values, opts) + + # work around protected #initialize_from for now + self.new(values[:id]).send(:initialize_from, values, opts) end # Determines the equality of two Stripe objects. Stripe objects are @@ -41,35 +43,17 @@ def inspect "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values) end + # Re-initializes the object based on a hash of values (usually one that's + # come back from an API call). Adds or removes value accessors as necessary + # and updates the state of internal data. + # + # Please don't use this method. If you're trying to do mass assignment, try + # #initialize_from instead. def refresh_from(values, opts, partial=false) - @opts = Util.normalize_opts(opts) - @original_values = Marshal.load(Marshal.dump(values)) # deep copy - - removed = partial ? Set.new : Set.new(@values.keys - values.keys) - added = Set.new(values.keys - @values.keys) - - # Wipe old state before setting new. This is useful for e.g. updating a - # customer, where there is no persistent card parameter. Mark those values - # which don't persist as transient - - instance_eval do - remove_accessors(removed) - add_accessors(added, values) - end - - removed.each do |k| - @values.delete(k) - @transient_values.add(k) - @unsaved_values.delete(k) - end - - update_attributes_with_options(values, :opts => opts) - values.each do |k, _| - @transient_values.delete(k) - @unsaved_values.delete(k) - end - - return self + Stripe::Util.warn_deprecated("#refresh_from", + extra: "If you're trying to perform mass-assignment, please consider " + + "using #update_attributes instead.") + initialize_from(values, opts, partial) end # Mass assigns attributes on the model. @@ -294,6 +278,49 @@ def respond_to_missing?(symbol, include_private = false) @values && @values.has_key?(symbol) || super end + # Re-initializes the object based on a hash of values (usually one that's + # come back from an API call). Adds or removes value accessors as necessary + # and updates the state of internal data. + # + # Protected on purpose! Please do not expose. + # + # ==== Options + # + # * +:values:+ Hash used to update accessors and values. + # * +:opts:+ Options for StripeObject like an API key. + # * +:partial:+ Indicates that the re-initialization should not attempt to + # remove accessors. + def initialize_from(values, opts, partial=false) + @opts = Util.normalize_opts(opts) + @original_values = Marshal.load(Marshal.dump(values)) # deep copy + + removed = partial ? Set.new : Set.new(@values.keys - values.keys) + added = Set.new(values.keys - @values.keys) + + # Wipe old state before setting new. This is useful for e.g. updating a + # customer, where there is no persistent card parameter. Mark those values + # which don't persist as transient + + instance_eval do + remove_accessors(removed) + add_accessors(added, values) + end + + removed.each do |k| + @values.delete(k) + @transient_values.add(k) + @unsaved_values.delete(k) + end + + update_attributes_with_options(values, :opts => opts) + values.each do |k, _| + @transient_values.delete(k) + @unsaved_values.delete(k) + end + + self + end + # Mass assigns attributes on the model. # # This is a version of +update_attributes+ that takes some extra options diff --git a/lib/stripe/subscription.rb b/lib/stripe/subscription.rb index e234e9bca..b9aad2910 100644 --- a/lib/stripe/subscription.rb +++ b/lib/stripe/subscription.rb @@ -13,7 +13,7 @@ def self.retrieve(id, opts=nil) def delete_discount response, opts = request(:delete, discount_url) - refresh_from({ :discount => nil }, opts, true) + initialize_from({ :discount => nil }, opts, true) end private diff --git a/lib/stripe/transfer.rb b/lib/stripe/transfer.rb index 99483fd8f..f8074ba99 100644 --- a/lib/stripe/transfer.rb +++ b/lib/stripe/transfer.rb @@ -6,7 +6,7 @@ class Transfer < APIResource def cancel response, api_key = Stripe.request(:post, cancel_url, @api_key) - refresh_from(response, api_key) + initialize_from(response, api_key) end def cancel_url diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index c61dcadb8..54fb67a78 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -181,5 +181,16 @@ def self.check_api_key!(key) raise TypeError.new("api_key must be a string") unless key.is_a?(String) key end + + def self.warn_deprecated(name, options = {}) + # should not trigger on $VERBOSE = nil (false is "level 1", true is + # "level 2") + if $VERBOSE != nil + message = "Warning (stripe): #{name} is deprecated and will be " + + "removed in a future version." + message += " " + options[:extra] if options[:extra] + $stderr.puts(message) + end + end end end diff --git a/test/stripe/metadata_test.rb b/test/stripe/metadata_test.rb index 5a8fd1977..d1fcfd553 100644 --- a/test/stripe/metadata_test.rb +++ b/test/stripe/metadata_test.rb @@ -93,7 +93,7 @@ class MetadataTest < Test::Unit::TestCase obj.metadata['uuid'] = '6735' end params = {:metadata => {'type' => 'summer', 'uuid' => '6735'}} - curl_args = Stripe.uri_encode(params) + curl_args = Stripe::Util.encode_parameters(params) check_metadata({:metadata => {'type' => 'christmas'}}, curl_args, update_actions) diff --git a/test/stripe/util_test.rb b/test/stripe/util_test.rb index 2c404fd5f..2883e2da1 100644 --- a/test/stripe/util_test.rb +++ b/test/stripe/util_test.rb @@ -71,5 +71,32 @@ class UtilTest < Test::Unit::TestCase assert_raise { Stripe::Util.normalize_opts(nil) } assert_raise { Stripe::Util.normalize_opts(:api_key => nil) } end + + should "#warn_deprecated produces a deprecation warning" do + old_stderr = $stderr + $stderr = StringIO.new + begin + Stripe::Util.warn_deprecated("#refresh_from", extra: "Don't use it.") + message = "Warning (stripe): #refresh_from is deprecated and will be " + + "removed in a future version. Don't use it.\n" + assert_equal message, $stderr.string + ensure + $stderr = old_stderr + end + end + + should "#warn_deprecated is silent on $VERBOSE = nil" do + old_stderr = $stderr + old_verbose = $VERBOSE + $stderr = StringIO.new + $VERBOSE = nil + begin + Stripe::Util.warn_deprecated("#refresh_from", extra: "Don't use it.") + assert_equal "", $stderr.string + ensure + $stderr = old_stderr + $VERBOSE = old_verbose + end + end end end