Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

User account information now includes a balance field, showing the ne…

…t amount owed by that user's connections to them.
  • Loading branch information...
commit 1da9bf4600be8ac99238a7fd2a8a1bc43d334dfb 1 parent 865e681
@jplewicke authored
Showing with 24 additions and 0 deletions.
  1. +9 −0 neo_classes.rb
  2. +7 −0 spec/rivulet_spec.rb
  3. +8 −0 test.rb
View
9 neo_classes.rb
@@ -1,4 +1,5 @@
require 'neo4j'
+require 'app_classes'
Neo4j::Config[:storage_path] = "#{Dir.pwd}/dbneo"
@@ -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
@@ -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}",
View
7 spec/rivulet_spec.rb
@@ -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
View
8 test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.