Permalink
Browse files

Add our own JSON abstraction to deal with MultiJson backwards compati…

…bility

Thanks to Kevin Menard for the tip on how to approach it.
  • Loading branch information...
1 parent 32e619c commit f8073e132ac511c8b95bf2dfa34f35515ffd1e71 @evan-stripe evan-stripe committed Apr 26, 2012
Showing with 25 additions and 7 deletions.
  1. +24 −6 lib/stripe.rb
  2. +1 −1 stripe.gemspec
View
@@ -119,6 +119,24 @@ def self.flatten_params_array value, calculated_key
end
end
+ module JSON
+ if MultiJson.respond_to?(:dump)
+ def self.dump(*args)
+ MultiJson.dump(*args)
+ end
+ def self.load(*args)
+ MultiJson.load(*args)
+ end
+ else
+ def self.dump(*args)
+ MultiJson.encode(*args)
+ end
+ def self.load(*args)
+ MultiJson.decode(*args)
+ end
+ end
+ end
+
module APIOperations
module Create
module ClassMethods
@@ -193,11 +211,11 @@ def self.construct_from(values, api_key=nil)
obj
end
- def to_s(*args); MultiJson.dump(@values, :pretty => true); end
+ def to_s(*args); Stripe::JSON.dump(@values, :pretty => true); end
def inspect()
id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
- "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + MultiJson.dump(@values, :pretty => true)
+ "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + Stripe::JSON.dump(@values, :pretty => true)
end
def refresh_from(values, api_key, partial=false)
@@ -234,7 +252,7 @@ def []=(k, v)
end
def keys; @values.keys; end
def values; @values.values; end
- def to_json(*a); MultiJson.dump(@values); end
+ def to_json(*a); Stripe::JSON.dump(@values); end
def to_hash; @values; end
def each(&blk); @values.each(&blk); end
@@ -530,7 +548,7 @@ def self.request(method, url, api_key, params=nil, headers={})
end
begin
- headers = { :x_stripe_client_user_agent => MultiJson.dump(ua) }.merge(headers)
+ headers = { :x_stripe_client_user_agent => Stripe::JSON.dump(ua) }.merge(headers)
rescue => e
headers = {
:x_stripe_client_raw_user_agent => ua.inspect,
@@ -578,7 +596,7 @@ def self.request(method, url, api_key, params=nil, headers={})
begin
# Would use :symbolize_names => true, but apparently there is
# some library out there that makes symbolize_names not work.
- resp = MultiJson.load(rbody)
+ resp = Stripe::JSON.load(rbody)
rescue MultiJson::DecodeError
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
end
@@ -595,7 +613,7 @@ def self.execute_request(opts)
def self.handle_api_error(rcode, rbody)
begin
- error_obj = MultiJson.load(rbody)
+ error_obj = Stripe::JSON.load(rbody)
error_obj = Util.symbolize_names(error_obj)
error = error_obj[:error] or raise StripeError.new # escape from parsing
rescue MultiJson::DecodeError, StripeError
View
@@ -14,7 +14,7 @@ spec = Gem::Specification.new do |s|
s.require_paths = %w{lib}
s.add_dependency('rest-client', '~> 1.4')
- s.add_dependency('multi_json', '>= 1.3.0')
+ s.add_dependency('multi_json', '~> 1.0')
s.add_development_dependency('mocha')
s.add_development_dependency('shoulda')

0 comments on commit f8073e1

Please sign in to comment.