Permalink
Browse files

Move some logic into methods.

Remove that weird __content__ line.
  • Loading branch information...
Gabe Berke-Williams
Gabe Berke-Williams committed Nov 17, 2011
1 parent ebab395 commit 5970f68cf83eb15a27422cafff05f8fd8f78014e
Showing with 19 additions and 7 deletions.
  1. +19 −7 lib/fake_braintree/customer.rb
@@ -25,15 +25,12 @@ def customer_hash
hash = @customer_hash.dup
hash["id"] ||= create_id
hash["merchant-id"] = @merchant_id
+
if hash["credit_card"] && hash["credit_card"].is_a?(Hash)
- hash["credit_card"].delete("__content__")
if !hash["credit_card"].empty?
- hash["credit_card"]["last_4"] = hash["credit_card"].delete("number")[-4..-1]
- hash["credit_card"]["token"] = md5("#{hash['merchant_id']}#{hash['id']}")
- if expiration_date = hash["credit_card"].delete("expiration_date")
- hash["credit_card"]["expiration_month"] = expiration_date.split('/')[0]
- hash["credit_card"]["expiration_year"] = expiration_date.split('/')[1]
- end
+ hash["credit_card"]["last_4"] = last_four(hash)
+ hash["credit_card"]["token"] = credit_card_token(hash)
+ split_expiration_date_into_month_and_year!(hash)
credit_card = hash.delete("credit_card")
hash["credit_cards"] = [credit_card]
@@ -45,6 +42,21 @@ def customer_hash
private
+ def split_expiration_date_into_month_and_year!(hash)
+ if expiration_date = hash["credit_card"].delete("expiration_date")
+ hash["credit_card"]["expiration_month"] = expiration_date.split('/')[0]
+ hash["credit_card"]["expiration_year"] = expiration_date.split('/')[1]
+ end
+ end
+
+ def credit_card_token(hash)
+ md5("#{hash['merchant_id']}#{hash['id']}")
+ end
+
+ def last_four(hash)
+ hash["credit_card"].delete("number")[-4..-1]
+ end
+
def failure_response
gzipped_response(422, FakeBraintree.failure_response(@customer_hash["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
end

0 comments on commit 5970f68

Please sign in to comment.