From 84e9350676f401989f30a00fc45caa64bac4ce9c Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Mon, 28 Jul 2014 16:47:33 +0200 Subject: [PATCH] JSONify balance output, disable cleared for the moment --- app.rb | 18 +++++++++--------- config/options.rb | 3 +++ lib/ledger.rb | 11 +++++++++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app.rb b/app.rb index fe82b9ca..ac65523c 100644 --- a/app.rb +++ b/app.rb @@ -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 diff --git a/config/options.rb b/config/options.rb index 5b66c4ea..67338c5a 100644 --- a/config/options.rb +++ b/config/options.rb @@ -1,3 +1,6 @@ # encoding: utf-8 ENV[ 'RACK_ENV' ] = 'development' + +CURRENCY = '€' +SEPARATOR = ',' diff --git a/lib/ledger.rb b/lib/ledger.rb index da009512..5c8428b9 100644 --- a/lib/ledger.rb +++ b/lib/ledger.rb @@ -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