redefined register API

This commit is contained in:
Gwenhael Le Moine 2014-08-01 22:25:36 +02:00
parent c1b43df9eb
commit f93412b2d5
2 changed files with 19 additions and 25 deletions

15
app.rb
View file

@ -29,16 +29,13 @@ class LedgerRbApp < Sinatra::Base
Ledger.accounts( params[ :depth ] ).to_json Ledger.accounts( params[ :depth ] ).to_json
end end
get '/api/ledger/register/:period/?' do get '/api/ledger/register/?' do
param :period, String, required: true, within: [ 'yearly', 'monthly' ] param :period, String, default: nil
param :categories, Array, default: Ledger.accounts( 1 ) param :category, String, required: true
params[ :categories ].map do { key: params[ :category ],
|category| values: Ledger.register( params[ :period ], params[ :category ] ) }
cat = category.first .to_json
{ key: cat,
values: Ledger.register( cat, "--#{params[ :period ]}" ) }
end.to_json
end end
get '/api/ledger/balance/?' do get '/api/ledger/balance/?' do

View file

@ -1,5 +1,7 @@
# encoding: utf-8 # encoding: utf-8
require 'csv'
# Ruby wrapper module for calling ledger # Ruby wrapper module for calling ledger
module Ledger module Ledger
module_function module_function
@ -25,26 +27,21 @@ module Ledger
end.uniq end.uniq
end end
def register( category, options='' ) def register( period = nil, categories = '' )
run( "#{options} --collapse --amount-data --exchange '#{CURRENCY}'", 'register', "#{category}" ) period = period.nil? ? '' : "-p '#{period}'"
.split( "\n" )
.map do |line|
line_array = line.split
[ Time.new( line_array[ 0 ] ).to_i, CSV.parse( run( "--exchange '#{CURRENCY}' #{period}", 'csv', categories ) )
line_array[ 1 ].to_f ] .map do
|row|
{ date: row[ 0 ],
payee: row[ 2 ],
account: row[ 3 ],
amount: row[ 5 ],
currentcy: row[ 4 ] }
end end
end end
def monthly_register( category ) def balance( cleared = false, depth = nil, period = nil, categories = '' )
register category, "--monthly"
end
def yearly_register( category )
register category, "--yearly"
end
def balance( cleared=false, depth=nil, period=nil, categories='' )
period = period.nil? ? '' : "-p '#{period}'" period = period.nil? ? '' : "-p '#{period}'"
depth = depth.nil? ? '' : "--depth #{depth}" depth = depth.nil? ? '' : "--depth #{depth}"
operation = cleared ? 'cleared' : 'balance' operation = cleared ? 'cleared' : 'balance'