From f05c25e405a863821c8e706bac6f1c3e18303aa1 Mon Sep 17 00:00:00 2001 From: benji Date: Mon, 21 Sep 2015 12:46:39 +0200 Subject: [PATCH] tab callback for user balance --- app/models/user.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index b98deb9..d2d7344 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,6 +45,18 @@ class User < ActiveRecord::Base end def debt - 42.15 + @balance || begin + headers = { + "Authorization" => "Token token=#{Rails.application.secrets.tab_api_key}", + "Content-type" => "application/json" + } + result = HTTParty.get(File.join(Rails.application.config.api_url, "users", "#{name}.json"), headers: headers) + + if result.code == 200 + JSON.parse(result.body)["balance"] + else + 0 + end + end end end