JSONify balance output, disable cleared for the moment

This commit is contained in:
Gwenhael Le Moine 2014-07-28 16:47:33 +02:00
parent 09678bd48e
commit 84e9350676
3 changed files with 21 additions and 11 deletions

18
app.rb
View file

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

View file

@ -1,3 +1,6 @@
# encoding: utf-8
ENV[ 'RACK_ENV' ] = 'development'
CURRENCY = '€'
SEPARATOR = ','

View file

@ -26,7 +26,7 @@ module Ledger
end
def register( category, options='' )
run( "#{options} --collapse --amount-data --exchange ''", 'register', "#{category}" )
run( "#{options} --collapse --amount-data --exchange '#{CURRENCY}'", 'register', "#{category}" )
.split( "\n" )
.map do |line|
line_array = line.split
@ -48,6 +48,13 @@ module Ledger
period = period.nil? ? '' : "-p #{period}"
depth = depth.nil? ? '' : "--depth #{depth}"
operation = cleared ? 'cleared' : 'balance'
run "--flat --exchange '€' #{period} #{depth}", operation
run( "--flat --no-total --exchange '#{CURRENCY}' #{period} #{depth}", operation )
.split( "\n" )
.map do |line|
line_array = line.split( "#{CURRENCY}" )
{ account: line_array[ 1 ].strip,
amount: line_array[ 0 ].tr( SEPARATOR, '.' ).to_f }
end
end
end