Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move some logic into methods.
Remove that weird __content__ line.
  • Loading branch information
Gabe Berke-Williams committed Nov 17, 2011
1 parent ebab395 commit 5970f68
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/fake_braintree/customer.rb
Expand Up @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit 5970f68

Please sign in to comment.