Skip to content

Instantly share code, notes, and snippets.

@cailinanne
Created February 28, 2012 15:43
Show Gist options
  • Select an option

  • Save cailinanne/1933245 to your computer and use it in GitHub Desktop.

Select an option

Save cailinanne/1933245 to your computer and use it in GitHub Desktop.
Toy code for loan matching
def match_loans(basket)
match_basket = nil
total_spent = 0
match_user = # LOAD UP THE MATCHING ACCOUNT
basket.basket_items.each do |item|
loan = item.purchasable
# If this loan qualifies for matching and
# If there are enough shares left to sell and ...
# If the matching user has enough funds left
# TODO I'm sure there's a more elegant way to write this if clause, it looks yucky
if loan.qualifies_for_matching? && item.amount < loan.amount_left_to_purchase && item.amount < (match_user.balance - total_spent)
# Create the matching basket if we haven't already...
match_basket ||= Basket.create! :user => matching_user
# Put a matched item in the basket
BasketItem.create! :purchasable => loan, :amount => item.amount
total_spent = total_spent + item.amount
end
end
# If we found anything to put in the match basket, ask the PHP side to fulfill the basket
unless match_basket.nil?
match_basket.state = :paypal
match_basket.save!
job = GearmanJob.new
job.fulfill_paypal_transaction(match_basket, nil)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment