mirror of
https://github.com/gwenhael-le-moine/ledgerrb.git
synced 2025-01-28 07:58:29 +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
|
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
|
||||||
|
|
|
@ -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'
|
||||||
|
|
Loading…
Add table
Reference in a new issue