diff --git a/app.rb b/app.rb index 5004f024..8cc6034c 100644 --- a/app.rb +++ b/app.rb @@ -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 diff --git a/lib/ledger.rb b/lib/ledger.rb index 7921ce0c..da009512 100644 --- a/lib/ledger.rb +++ b/lib/ledger.rb @@ -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