diff --git a/app.rb b/app.rb index 5edd2b53..5004f024 100644 --- a/app.rb +++ b/app.rb @@ -27,15 +27,16 @@ class LedgerRbApp < Sinatra::Base Ledger.accounts( params[ :depth ] ).to_json end - get '/api/ledger/monthly/?' do + get '/api/ledger/register/:period/?' do content_type :json + param :period, String, required: true # TODO: restrict possible values to [ 'yearly', 'monthly' ] param :categories, Array, default: Ledger.accounts( 1 ) params[ :categories ].map do |category| cat = category.first { category: cat, - data: Ledger.monthly_register( cat ) } + data: Ledger.register( cat, "--#{params[ :period ]}" ) } end.to_json end diff --git a/lib/ledger.rb b/lib/ledger.rb index deabb481..7921ce0c 100644 --- a/lib/ledger.rb +++ b/lib/ledger.rb @@ -29,15 +29,23 @@ module Ledger run '', 'cleared' end - def monthly_register( category ) - run( "--monthly --collapse --amount-data --exchange '€'", 'register', "#{category}" ) + def register( category, options='' ) + run( "#{options} --collapse --amount-data --exchange '€'", 'register', "#{category}" ) .split( "\n" ) .map do |line| line_array = line.split { date: line_array[ 0 ], amount: line_array[ 1 ].to_f } - end + end + end + + def monthly_register( category ) + register category, "--monthly" + end + + def yearly_register( category ) + register category, "--yearly" end def balance( period = nil )