Skip to content

Commit

Permalink
User account information now includes a balance field, showing the ne…
Browse files Browse the repository at this point in the history
…t amount owed by that user's connections to them.
  • Loading branch information
jplewicke committed Mar 31, 2011
1 parent 865e681 commit 1da9bf4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions neo_classes.rb
@@ -1,4 +1,5 @@
require 'neo4j'
require 'app_classes'

Neo4j::Config[:storage_path] = "#{Dir.pwd}/dbneo"

Expand Down Expand Up @@ -78,6 +79,13 @@ def trustrel(dest)
rel
end

def creditors
self.outgoing(:trusts).incoming(:trusts).depth(1).collect {|dest| CreditRelationship.new(self, dest)}
end

def balance
self.creditors.collect {|creditRel| creditRel.source_offer.amount_used - creditRel.dest_offer.amount_used}.reduce {|a,b| a + b}
end

def self.fromid(id)
ret = nil
Expand Down Expand Up @@ -109,6 +117,7 @@ def to_json
{
:user => self.user_id,
:depth => self.depth,
:balance => self.balance,
:account_url => "/accounts/#{self.user_id}",
:credit_url => "/credits/#{self.user_id}",
:transaction_url => "/transactions/#{self.user_id}",
Expand Down
7 changes: 7 additions & 0 deletions spec/rivulet_spec.rb
Expand Up @@ -240,6 +240,13 @@ def app
JSON.parse(last_response.body)['debit_held'].should == 0.0
JSON.parse(last_response.body)['credit_held'].should == 0.0

get "/accounts/User_#{src_id}", {}, cred(src_id)
last_response.status.should == 200
JSON.parse(last_response.body)['balance'].should == 4.0

get "/accounts/User_#{dest_id}", {}, cred(dest_id)
last_response.status.should == 200
JSON.parse(last_response.body)['balance'].should == -4.0

#Accept full credit.
amt = 10.0
Expand Down
8 changes: 8 additions & 0 deletions test.rb
Expand Up @@ -235,6 +235,14 @@ def test_comprehensive
assert_equal 0.0, JSON.parse(last_response.body)['debit_held']
assert_equal 0.0, JSON.parse(last_response.body)['credit_held']

#Check balances for each user.

get "/accounts/User_#{src_id}", {}, cred(src_id)
assert_equal 200, last_response.status
assert_equal 4.0, JSON.parse(last_response.body)['balance']
get "/accounts/User_#{dest_id}", {}, cred(dest_id)
assert_equal 200, last_response.status
assert_equal -4.0, JSON.parse(last_response.body)['balance']

#Accept full credit.
amt = 10.0
Expand Down

0 comments on commit 1da9bf4

Please sign in to comment.