From 12a482b16e8d825228556133f1ff226699cee1f8 Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Sun, 27 Jul 2014 22:04:00 +0200 Subject: [PATCH] refactoring register methods and API --- app.rb | 5 +++-- lib/ledger.rb | 14 +++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) 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 )