Files
old-pruning-excersise/lib/api/app.rb

40 lines
862 B
Ruby
Raw Normal View History

2018-08-13 11:25:01 +02:00
require 'json'
require 'sinatra/base'
2018-08-13 15:01:28 +02:00
require 'sinatra/custom_logger'
2018-08-13 11:25:01 +02:00
require_relative '../http/query'
2018-08-13 15:01:28 +02:00
require_relative '../exceptions'
2018-08-13 11:25:01 +02:00
module Pruning
module API
2018-08-13 18:54:12 +02:00
# Handles general concers regarding API calls
2018-08-13 11:25:01 +02:00
class App < Sinatra::Base
before { content_type :json }
after { serialise_response }
2018-08-13 15:01:28 +02:00
set :show_exceptions, true
2018-08-13 18:54:12 +02:00
error Pruning::Exceptions::UnexpectedError do
2018-08-13 15:01:28 +02:00
status 500
end
error Pruning::Exceptions::OriginCannotFindTheResource do
2018-08-13 18:54:12 +02:00
status 404
2018-08-13 15:01:28 +02:00
end
2018-08-13 11:25:01 +02:00
2018-08-13 15:01:28 +02:00
get '/' do
2018-08-13 18:54:12 +02:00
'Try /tree/:name?indicator_ids[]=32&indicator_ids[]=31'
2018-08-13 15:01:28 +02:00
end
private
2018-08-13 18:54:12 +02:00
2018-08-13 11:25:01 +02:00
def serialise_response
return unless content_type == 'application/json'
response.body = [JSON(response.body)]
end
def query
Pruning::HTTP::Query.new(params)
end
end
end
end