From 6d0a5d11f7595ccdc952c5cc55f0e675234fd08f Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Mon, 14 Jul 2014 16:19:05 +0200 Subject: [PATCH] basic API in front of ledgerrb lib --- Gemfile | 2 +- Gemfile.lock | 15 +++------------ app.rb | 46 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Gemfile b/Gemfile index 8c483b8f..4aec6d77 100644 --- a/Gemfile +++ b/Gemfile @@ -3,4 +3,4 @@ source 'https://rubygems.org' gem 'sinatra' -gem 'sinatra-contrib' +gem 'sinatra-param' diff --git a/Gemfile.lock b/Gemfile.lock index 3fbd2881..f7c23855 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,24 +1,15 @@ GEM remote: https://rubygems.org/ specs: - backports (3.6.0) - multi_json (1.10.1) rack (1.5.2) rack-protection (1.5.3) rack - rack-test (0.6.2) - rack (>= 1.0) sinatra (1.4.5) rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) - sinatra-contrib (1.4.2) - backports (>= 2.0) - multi_json - rack-protection - rack-test - sinatra (~> 1.4.0) - tilt (~> 1.3) + sinatra-param (1.2.2) + sinatra (~> 1.3) tilt (1.4.1) PLATFORMS @@ -26,4 +17,4 @@ PLATFORMS DEPENDENCIES sinatra - sinatra-contrib + sinatra-param diff --git a/app.rb b/app.rb index c54a5df3..b9b915be 100644 --- a/app.rb +++ b/app.rb @@ -1,8 +1,46 @@ # encoding: utf-8 -require 'sinatra' -require 'sinatra/reloader' +require 'json' + +Bundler.require( :default, :development ) # require tout les gems définis dans Gemfile + +require_relative './lib/ledger' + +# Sinatra app serving API +class LedgerRbApp < Sinatra::Base + helpers Sinatra::Param + + get '/' do + 'OH HAI!' + end + + get '/api/ledger/accounts/?' do + content_type :json + + Ledger.accounts.to_json + end + + get '/api/ledger/accounts/depth/:depth' do + content_type :json + param :depth, Integer + + Ledger.accounts( params[ :depth ] ).to_json + end + + get '/api/ledger/monthly/?' do + content_type :json + param :categories, Array + + params[ :categories ].map do |category| + { category: category, + data: Ledger.monthly_register( category ).to_json } + end + end + + get '/api/ledger/version/?' do + content_type :json + + Ledger.version + end -get '/' do - 'OH HAI!' end