Skip to content

Commit

Permalink
Make the flow clearer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Jan 13, 2012
1 parent 3385e12 commit ad82f0e
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions lib/fake_braintree/customer.rb
Expand Up @@ -11,7 +11,7 @@ def initialize(customer_hash, options)

def create
if invalid?
failure_response
response_for_invalid_card
else
hash = customer_hash
create_customer_with(hash)
Expand All @@ -21,16 +21,17 @@ def create
end

def update
if existing_customer_hash
hash = update_existing_customer!
if customer_exists_in_registry?
updates = customer_hash
hash = update_customer!(updates)
response_for_updated_customer(hash)
else
failure_response(404)
response_for_customer_not_found
end
end

def delete
delete_customer_with_id(existing_customer_id)
delete_customer_with_id(customer_id)
deletion_response
end

Expand Down Expand Up @@ -65,12 +66,16 @@ def invalid?
credit_card_is_failure? || invalid_credit_card?
end

def existing_customer_hash
existing_customer_id && FakeBraintree.registry.customers[existing_customer_id]
def update_customer!(hash)
customer_from_registry.merge!(hash)
end

def update_existing_customer!
existing_customer_hash.merge!(customer_hash)
def customer_exists_in_registry?
FakeBraintree.registry.customers.key?(customer_id)
end

def customer_from_registry
FakeBraintree.registry.customers[customer_id]
end

def credit_card_token(hash)
Expand All @@ -81,7 +86,7 @@ def last_four(hash)
hash["credit_card"].delete("number")[-4..-1]
end

def failure_response(code = 422)
def failure_response(code)
gzipped_response(code, FakeBraintree.failure_response(credit_card_number).to_xml(:root => 'api_error_response'))
end

Expand Down Expand Up @@ -119,10 +124,6 @@ def credit_card_number
has_credit_card_number? && @customer_hash["credit_card"]["number"]
end

def existing_customer_id
@customer_hash['id']
end

def response_for_created_customer(hash)
gzipped_response(201, hash.to_xml(:root => 'customer'))
end
Expand Down Expand Up @@ -171,5 +172,17 @@ def delete_customer_with_id(id)
def response_for_updated_customer(hash)
gzipped_response(200, hash.to_xml(:root => 'customer'))
end

def response_for_invalid_card
failure_response(422)
end

def response_for_customer_not_found
failure_response(404)
end

def customer_id
@customer_hash["id"]
end
end
end

0 comments on commit ad82f0e

Please sign in to comment.