diff --git a/app.rb b/app.rb index 0e3a1f8c..861485a5 100644 --- a/app.rb +++ b/app.rb @@ -29,16 +29,13 @@ class LedgerRbApp < Sinatra::Base Ledger.accounts( params[ :depth ] ).to_json end - get '/api/ledger/register/:period/?' do - param :period, String, required: true, within: [ 'yearly', 'monthly' ] - param :categories, Array, default: Ledger.accounts( 1 ) + get '/api/ledger/register/?' do + param :period, String, default: nil + param :category, String, required: true - params[ :categories ].map do - |category| - cat = category.first - { key: cat, - values: Ledger.register( cat, "--#{params[ :period ]}" ) } - end.to_json + { key: params[ :category ], + values: Ledger.register( params[ :period ], params[ :category ] ) } + .to_json end get '/api/ledger/balance/?' do diff --git a/lib/ledger.rb b/lib/ledger.rb index beef7b3d..81275509 100644 --- a/lib/ledger.rb +++ b/lib/ledger.rb @@ -1,5 +1,7 @@ # encoding: utf-8 +require 'csv' + # Ruby wrapper module for calling ledger module Ledger module_function @@ -25,26 +27,21 @@ module Ledger end.uniq end - def register( category, options='' ) - run( "#{options} --collapse --amount-data --exchange '#{CURRENCY}'", 'register', "#{category}" ) - .split( "\n" ) - .map do |line| - line_array = line.split + def register( period = nil, categories = '' ) + period = period.nil? ? '' : "-p '#{period}'" - [ Time.new( line_array[ 0 ] ).to_i, - line_array[ 1 ].to_f ] + CSV.parse( run( "--exchange '#{CURRENCY}' #{period}", 'csv', categories ) ) + .map do + |row| + { date: row[ 0 ], + payee: row[ 2 ], + account: row[ 3 ], + amount: row[ 5 ], + currentcy: row[ 4 ] } end end - def monthly_register( category ) - register category, "--monthly" - end - - def yearly_register( category ) - register category, "--yearly" - end - - def balance( cleared=false, depth=nil, period=nil, categories='' ) + def balance( cleared = false, depth = nil, period = nil, categories = '' ) period = period.nil? ? '' : "-p '#{period}'" depth = depth.nil? ? '' : "--depth #{depth}" operation = cleared ? 'cleared' : 'balance'