mirror of
https://github.com/gwenhael-le-moine/ledgerrb.git
synced 2025-01-27 07:58:31 +01:00
redefined register API
This commit is contained in:
parent
c1b43df9eb
commit
f93412b2d5
2 changed files with 19 additions and 25 deletions
15
app.rb
15
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
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Add table
Reference in a new issue