Skip to content
This repository was archived by the owner on Nov 17, 2018. It is now read-only.

Commit d312785

Browse files
committed
Model: formed get_balance to api v3.
1 parent 1d1219b commit d312785

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616

1717
# Ignore application configuration
1818
/config/application.yml
19+
/public/assets
20+
/log

app/assets/stylesheets/oauth.css.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ body, input {
3535

3636
#title {
3737
position: relative;
38-
top: 15px;
38+
top: 29px;
3939
margin: 0 1.5em 0 2.8em;
4040
color: #444;
4141
font-size: 1.7em;

app/models/user.rb

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def initialize access_token
1414
@access_token = access_token
1515
end
1616

17-
['get_current_user', 'get_friendships'].each do |method|
17+
['get_current_user', 'get_friends'].each do |method|
1818
define_method method.to_sym do
1919
data = @access_token.get(API_URL+method)
2020
body = data.body
@@ -34,9 +34,9 @@ def get_current_user_id
3434
def get_friend_ids
3535
id = get_current_user_id
3636
friend_ids = []
37-
each_friendship do |friendship|
38-
candidates = friendship['users'].reject{|u| u['id'] == id}
39-
throw "I find not 1 but #{candidates.length} other users in a friendship." unless candidates.length == 1
37+
each_friend do |friend|
38+
candidates = friend['users'].reject{|u| u['id'] == id}
39+
throw "I find not 1 but #{candidates.length} other users in a friend." unless candidates.length == 1
4040
friend_ids.push(candidates[0]['id'])
4141
end
4242
end
@@ -97,23 +97,31 @@ def each_expense_and_share_newest_to_oldest &block
9797
end
9898
end
9999

100-
def each_friendship &block
101-
get_friendships['friendships'].each &block
100+
def each_friend &block
101+
get_friends['friends'].each &block
102102
end
103103

104+
=begin
104105
def each_friend &block
105106
id = get_current_user_id
106-
each_friendship do |friendship|
107-
candidates = friendship['users'].reject{|u| u['id'] == id}
108-
throw "I find not 1 but #{candidates.length} other users in a friendship." unless candidates.length == 1
107+
each_friend do |friend|
108+
candidates = friend['users'].reject{|u| u['id'] == id}
109+
throw "I find not 1 but #{candidates.length} other users in a friend." unless candidates.length == 1
109110
block.call(candidates[0])
110111
end
111112
end
113+
=end
112114

113115
def get_current_balance
114116
balance = 0
115-
each_friendship do |friendship|
116-
balance += friendship['balance'].to_f
117+
each_friend do |friend|
118+
balance += friend['balance'].inject 0 do |rest, b|
119+
if b['currency_code'].downcase == 'usd'
120+
next rest + b['amount'].to_f
121+
else
122+
next rest
123+
end
124+
end
117125
end
118126
balance
119127
end
@@ -132,10 +140,10 @@ def get_current_balances_with_friends
132140
id = get_current_user_id
133141
d = get_current_user_id
134142
friends = Hash.new(-1)
135-
each_friendship do |friendship|
136-
candidates = friendship['users'].reject{|u| u['id'] == id}
137-
throw "I find not 1 but #{candidates.length} other users in a friendship." unless candidates.length == 1
138-
friends[candidates[0]['id']] = friendship['balance'].to_f
143+
each_friend do |friend|
144+
candidates = friend['users'].reject{|u| u['id'] == id}
145+
throw "I find not 1 but #{candidates.length} other users in a friend." unless candidates.length == 1
146+
friends[candidates[0]['id']] = friend['balance'].to_f
139147
end
140148
friends
141149
end

0 commit comments

Comments
 (0)