APIs for balance and cleared balance

This commit is contained in:
Gwenhael Le Moine 2014-07-27 22:04:26 +02:00
parent 12a482b16e
commit 9f2ea4913d
2 changed files with 30 additions and 6 deletions

26
app.rb
View file

@ -40,6 +40,32 @@ class LedgerRbApp < Sinatra::Base
end.to_json
end
get '/api/ledger/balance/?' do
content_type :json
Ledger.balance.to_json
end
get '/api/ledger/balance/depth/:depth/?' do
content_type :json
param :depth, Integer, required: true
Ledger.balance( false, params[ :depth ] ).to_json
end
get '/api/ledger/cleared/?' do
content_type :json
Ledger.balance( true ).to_json
end
get '/api/ledger/cleared/depth/:depth/?' do
content_type :json
param :depth, Integer, required: true
Ledger.balance( true, params[ :depth ] ).to_json
end
get '/api/ledger/version/?' do
content_type :json

View file

@ -25,10 +25,6 @@ module Ledger
end.uniq
end
def cleared
run '', 'cleared'
end
def register( category, options='' )
run( "#{options} --collapse --amount-data --exchange '€'", 'register', "#{category}" )
.split( "\n" )
@ -48,8 +44,10 @@ module Ledger
register category, "--yearly"
end
def balance( period = nil )
def balance( cleared=false, depth=nil, period=nil )
period = period.nil? ? '' : "-p #{period}"
output run "--flat --exchange '€' #{period}", 'balance', "#{category}"
depth = depth.nil? ? '' : "--depth #{depth}"
operation = cleared ? 'cleared' : 'balance'
run "--flat --exchange '€' #{period} #{depth}", operation
end
end