2017-11-24 10:58:58 +01:00
|
|
|
require "kemal"
|
|
|
|
|
|
|
|
require "./ledger"
|
|
|
|
|
|
|
|
CURRENCY = "€"
|
|
|
|
SEPARATOR = ","
|
|
|
|
|
2017-11-24 15:13:50 +01:00
|
|
|
ENV["PORT"] ||= "3000"
|
|
|
|
|
2017-11-24 15:32:19 +01:00
|
|
|
WD = File.dirname( Process.executable_path.to_s )
|
|
|
|
|
2017-11-24 10:58:58 +01:00
|
|
|
ledger = Ledger.new
|
|
|
|
|
2017-11-24 15:32:19 +01:00
|
|
|
public_folder( "#{WD}/public" )
|
|
|
|
|
2017-11-24 10:58:58 +01:00
|
|
|
# Matches GET "http://host:port/"
|
|
|
|
get "/" do |env|
|
|
|
|
env.response.content_type = "text/html"
|
2017-11-24 14:58:56 +01:00
|
|
|
|
2017-11-24 15:32:19 +01:00
|
|
|
send_file( env, "#{WD}/public/index.html" )
|
2017-11-24 14:58:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
get "/api/ledger/version" do |env|
|
|
|
|
env.response.content_type = "text"
|
|
|
|
|
|
|
|
ledger.version
|
2017-11-24 10:58:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
get "/api/ledger/accounts" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
2017-11-24 14:58:56 +01:00
|
|
|
|
2017-11-24 10:58:58 +01:00
|
|
|
ledger.accounts.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/api/ledger/balance" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2017-11-24 16:12:18 +01:00
|
|
|
ledger.balance( env.params.query.has_key?( "cleared" ) ? env.params.query[ "cleared" ] == "true" : false,
|
2017-11-24 10:58:58 +01:00
|
|
|
env.params.query[ "depth" ].to_i,
|
|
|
|
env.params.query[ "period" ],
|
|
|
|
env.params.query[ "categories" ] )
|
|
|
|
.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/api/ledger/graph_values" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2017-11-24 16:12:18 +01:00
|
|
|
ledger.graph_values( env.params.query["period"],
|
|
|
|
env.params.query["categories"].split(" ") ).to_json
|
2017-11-24 10:58:58 +01:00
|
|
|
end
|
|
|
|
|
2017-11-24 14:58:56 +01:00
|
|
|
get "/api/ledger/register" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
|
|
|
{ key: env.params.query[ "categories" ],
|
|
|
|
values: ledger.register( env.params.query[ "period" ],
|
|
|
|
env.params.query[ "categories" ].split(" ") ) }
|
|
|
|
.to_json
|
2017-11-24 10:58:58 +01:00
|
|
|
end
|
|
|
|
|
2017-11-24 15:13:50 +01:00
|
|
|
Kemal.run( ENV["PORT"].to_i )
|