class FnordMetric::App < Sinatra::Base @@sessions = Hash.new #use Rack::Reloader, 0 enable :session set :haml, :format => :html5 set :views, ::File.expand_path('../../../haml', __FILE__) set :public, ::File.expand_path('../../../pub', __FILE__) helpers do include Rack::Utils alias_method :h, :escape_html def path_prefix request.env["SCRIPT_NAME"] end end if ENV['RACK_ENV'] == "test" set :raise_errors, true end get '/' do redirect "#{request.env["SCRIPT_NAME"]}/dashboard/default" end get '/dashboard/:name' do @dashboard = FnordMetric.dashboards.detect{|d| d.token == params[:name] } @dashboard ||= FnordMetric.dashboards.first haml :app end get '/metric/:name' do content_type 'application/json' FnordMetric::MetricAPI.new(params).render end get '/widget/:name' do @dashboard = FnordMetric.dashboards.first @widget = @dashboard.widgets.first haml :widget end post '/events' do halt 400, 'please specify the event_type' unless params["type"] event_type = params.delete("type") FnordMetric.track(event_type, parse_params(params)) end private def parse_params(hash) hash.tap do |h| h.keys.each{ |k| h[k] = parse_param(h[k]) } end end def parse_param(object) return object unless object.is_a?(String) return object.to_f if object.match(/^[0-9]+[,\.][0-9]+$/) return object.to_i if object.match(/^[0-9]+$/) object end end