From 3ed090e5d16286cb2d7cd83e8451739e41dada5d Mon Sep 17 00:00:00 2001 From: Gabe Berke-Williams Date: Sun, 20 Nov 2011 11:04:29 -0500 Subject: [PATCH] Refactor Subscription. --- lib/fake_braintree/sinatra_app.rb | 7 ++--- lib/fake_braintree/subscription.rb | 37 +++++++++++++++--------- spec/fake_braintree/subscription_spec.rb | 18 +++++------- spec/spec_helper.rb | 1 + spec/support/subscription_helpers.rb | 6 ++++ 5 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 spec/support/subscription_helpers.rb diff --git a/lib/fake_braintree/sinatra_app.rb b/lib/fake_braintree/sinatra_app.rb index 6b7f18b..f98d26a 100644 --- a/lib/fake_braintree/sinatra_app.rb +++ b/lib/fake_braintree/sinatra_app.rb @@ -35,10 +35,9 @@ class SinatraApp < Sinatra::Base # Braintree::Subscription.create post "/merchants/:merchant_id/subscriptions" do - response_hash = Subscription.new(request).response_hash - - FakeBraintree.subscriptions[response_hash["id"]] = response_hash - gzipped_response(201, response_hash.to_xml(:root => 'subscription')) + subscription_hash = Hash.from_xml(request.body).delete("subscription") + options = {:merchant_id => params[:merchant_id]} + Subscription.new(subscription_hash, options).create end # Braintree::Subscription.find diff --git a/lib/fake_braintree/subscription.rb b/lib/fake_braintree/subscription.rb index e8a7f6b..8eaaf82 100644 --- a/lib/fake_braintree/subscription.rb +++ b/lib/fake_braintree/subscription.rb @@ -2,22 +2,29 @@ module FakeBraintree class Subscription include Helpers - def initialize(request) - @subscription_hash = Hash.from_xml(request.body).delete("subscription") + def initialize(subscription_hash, options) + @subscription_hash = subscription_hash.merge("merchant_id" => options[:merchant_id], + "id" => options[:id]) end - def response_hash - response_hash = {} - response_hash["id"] = md5("#{@subscription_hash["payment_method_token"]}#{Time.now.to_f}")[0,6] - response_hash["transactions"] = [] - response_hash["add_ons"] = [] - response_hash["discounts"] = [] - response_hash["plan_id"] = @subscription_hash["plan_id"] - response_hash["next_billing_date"] = braintree_formatted_date(1.month.from_now) - response_hash["payment_method_token"] = @subscription_hash["payment_method_token"] - response_hash["status"] = Braintree::Subscription::Status::Active + def create + hash = subscription_hash + FakeBraintree.subscriptions[hash["id"]] = hash + gzipped_response(201, hash.to_xml(:root => 'subscription')) + end + + def subscription_hash + subscription_hash = @subscription_hash.dup + subscription_hash["id"] ||= subscription_id + subscription_hash["transactions"] = [] + subscription_hash["add_ons"] = [] + subscription_hash["discounts"] = [] + subscription_hash["plan_id"] = @subscription_hash["plan_id"] + subscription_hash["next_billing_date"] = braintree_formatted_date(1.month.from_now) + subscription_hash["payment_method_token"] = @subscription_hash["payment_method_token"] + subscription_hash["status"] = Braintree::Subscription::Status::Active - response_hash + subscription_hash end private @@ -25,5 +32,9 @@ def response_hash def braintree_formatted_date(date) date.strftime('%Y-%m-%d') end + + def subscription_id + md5("#{@subscription_hash["payment_method_token"]}#{Time.now.to_f}")[0,6] + end end end diff --git a/spec/fake_braintree/subscription_spec.rb b/spec/fake_braintree/subscription_spec.rb index 4ae60c6..0987265 100644 --- a/spec/fake_braintree/subscription_spec.rb +++ b/spec/fake_braintree/subscription_spec.rb @@ -24,19 +24,15 @@ first_result.subscription.id.should_not == second_result.subscription.id end + it "stores created subscriptions in FakeBraintree.subscriptions" do + FakeBraintree.subscriptions[create_subscription.subscription.id].should_not be_nil + end + it "sets the next billing date to a string of 1.month.from_now in UTC" do Timecop.freeze do - result = Braintree::Subscription.create(:payment_method_token => cc_token, - :plan_id => plan_id) - - result.subscription.next_billing_date.should == 1.month.from_now.utc.strftime('%Y-%m-%d') + create_subscription.subscription.next_billing_date.should == 1.month.from_now.utc.strftime('%Y-%m-%d') end end - - def create_subscription - Braintree::Subscription.create(:payment_method_token => cc_token, - :plan_id => plan_id) - end end describe "Braintree::Subscription.find" do @@ -53,6 +49,6 @@ def create_subscription let(:payment_method_token) { cc_token } let(:plan_id) { 'my-plan-id' } - let(:subscription_id) { Braintree::Subscription.create(:payment_method_token => payment_method_token, - :plan_id => plan_id).subscription.id } + let(:subscription_id) { Braintree::Subscription.create(:payment_method_token => payment_method_token, + :plan_id => plan_id).subscription.id } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6841091..f7a268b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,7 @@ config.include BraintreeHelpers config.include CustomerHelpers + config.include SubscriptionHelpers config.before do FakeBraintree.clear! diff --git a/spec/support/subscription_helpers.rb b/spec/support/subscription_helpers.rb new file mode 100644 index 0000000..b5b7899 --- /dev/null +++ b/spec/support/subscription_helpers.rb @@ -0,0 +1,6 @@ +module SubscriptionHelpers + def create_subscription + Braintree::Subscription.create(:payment_method_token => cc_token, + :plan_id => 'my_plan_id') + end +end