Permalink
Browse files

Make the Subscription class behave like the others.

  • Loading branch information...
1 parent 8f21376 commit 157b2a75e200e49f8f636793eab6886637c32a31 @gabebw gabebw committed Jan 13, 2012
Showing with 45 additions and 17 deletions.
  1. +45 −17 lib/fake_braintree/subscription.rb
View
62 lib/fake_braintree/subscription.rb
@@ -14,45 +14,45 @@ def create
end
def update
- if existing_subscription_hash
- hash = update_existing_subscription!
- gzipped_response(200, hash.to_xml(:root => 'subscription'))
+ if subscription_exists_in_registry?
+ updated_subscription = update_subscription!(subscription_hash)
+ response_for_created_subscription(updated_subscription)
else
- gzipped_response(404, {})
+ response_for_subscription_not_found
end
end
def subscription_hash
subscription_hash = @subscription_hash.dup
- subscription_hash["id"] ||= subscription_id
+ subscription_hash["id"] ||= generate_new_subscription_id
subscription_hash["transactions"] = []
subscription_hash["add_ons"] = added_add_ons
subscription_hash["discounts"] = added_discounts
- subscription_hash["plan_id"] = @subscription_hash["plan_id"]
+ subscription_hash["plan_id"] = 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
+ subscription_hash["payment_method_token"] = payment_method_token
+ subscription_hash["status"] ||= active_status
subscription_hash
end
private
- def existing_subscription_hash
- @subscription_hash['id'] && FakeBraintree.registry.subscriptions[@subscription_hash["id"]]
+ def update_subscription!(updates)
+ updated_subscription = subscription_from_registry.merge(updates)
+ FakeBraintree.registry.subscriptions[subscription_id] = updated_subscription
end
- def update_existing_subscription!
- new_hash = existing_subscription_hash.merge(subscription_hash)
- FakeBraintree.registry.subscriptions[@subscription_hash['id']] = new_hash
+ def subscription_from_registry
+ FakeBraintree.registry.subscriptions[subscription_id]
end
- def braintree_formatted_date(date)
- date.strftime('%Y-%m-%d')
+ def subscription_exists_in_registry?
+ FakeBraintree.registry.subscriptions.key?(subscription_id)
end
- def subscription_id
- md5("#{@subscription_hash["payment_method_token"]}#{Time.now.to_f}")[0,6]
+ def braintree_formatted_date(date)
+ date.strftime('%Y-%m-%d')
end
def added_add_ons
@@ -70,5 +70,33 @@ def added_discounts
[]
end
end
+
+ def subscription_id
+ @subscription_hash["id"]
+ end
+
+ def plan_id
+ @subscription_hash["plan_id"]
+ end
+
+ def payment_method_token
+ @subscription_hash["payment_method_token"]
+ end
+
+ def generate_new_subscription_id
+ md5("#{@subscription_hash["payment_method_token"]}#{Time.now.to_f}")[0,6]
+ end
+
+ def active_status
+ Braintree::Subscription::Status::Active
+ end
+
+ def response_for_subscription_not_found
+ gzipped_response(404, {})
+ end
+
+ def response_for_created_subscription(hash)
+ gzipped_response(201, hash.to_xml(:root => "subscription"))
+ end
end
end

0 comments on commit 157b2a7

Please sign in to comment.