ledgerrb/app.rb

68 lines
1.4 KiB
Ruby
Raw Normal View History

2014-07-14 12:30:48 +02:00
# encoding: utf-8
2014-07-14 16:19:05 +02:00
require 'json'
2014-07-28 11:35:28 +02:00
Bundler.require( :default, ENV[ 'RACK_ENV' ].to_sym )
2014-07-14 16:19:05 +02:00
require_relative './lib/ledger'
# Sinatra app serving API
class LedgerRbApp < Sinatra::Base
helpers Sinatra::Param
2014-07-28 11:35:55 +02:00
before do
content_type :json, 'charset' => 'utf-8'
end
2014-07-14 16:19:05 +02:00
get '/' do
2014-07-28 11:35:55 +02:00
content_type :html
2014-07-14 19:59:46 +02:00
send_file './public/app/index.html'
2014-07-14 16:19:05 +02:00
end
get '/api/ledger/accounts/?' do
Ledger.accounts.to_json
end
2014-07-27 22:02:59 +02:00
get '/api/ledger/accounts/depth/:depth/?' do
2014-07-27 10:10:02 +02:00
param :depth, Integer, required: true
2014-07-14 16:19:05 +02:00
Ledger.accounts( params[ :depth ] ).to_json
end
2014-07-27 22:04:00 +02:00
get '/api/ledger/register/:period/?' do
2014-07-28 16:51:55 +02:00
param :period, String, required: true, within: [ 'yearly', 'monthly' ]
2014-07-27 10:10:02 +02:00
param :categories, Array, default: Ledger.accounts( 1 )
2014-07-27 10:10:40 +02:00
params[ :categories ].map do
|category|
cat = category.first
{ category: cat,
2014-07-27 22:04:00 +02:00
data: Ledger.register( cat, "--#{params[ :period ]}" ) }
2014-07-27 10:10:54 +02:00
end.to_json
2014-07-14 16:19:05 +02:00
end
2014-07-27 22:04:26 +02:00
get '/api/ledger/balance/?' do
Ledger.balance.to_json
2014-07-27 22:04:26 +02:00
end
get '/api/ledger/balance/depth/:depth/?' do
param :depth, Integer, required: true
Ledger.balance( false, params[ :depth ] ).to_json
2014-07-27 22:04:26 +02:00
end
# get '/api/ledger/cleared/?' do
# Ledger.balance( true ).to_json
# end
2014-07-27 22:04:26 +02:00
# get '/api/ledger/cleared/depth/:depth/?' do
# param :depth, Integer, required: true
2014-07-27 22:04:26 +02:00
# Ledger.balance( true, params[ :depth ] ).to_json
# end
2014-07-27 22:04:26 +02:00
2014-07-14 16:19:05 +02:00
get '/api/ledger/version/?' do
Ledger.version
end
2014-07-14 12:30:48 +02:00
end