mirror of
https://github.com/gwenhael-le-moine/ledgerrb.git
synced 2024-12-26 09:59:18 +01:00
refactoring register methods and API
This commit is contained in:
parent
90d343636e
commit
12a482b16e
2 changed files with 14 additions and 5 deletions
5
app.rb
5
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
|
||||
|
||||
|
|
|
@ -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 )
|
||||
|
|
Loading…
Reference in a new issue