lib/ardtweeno/restapi.rb in ardtweeno-0.5.0 vs lib/ardtweeno/restapi.rb in ardtweeno-0.6.0

- old
+ new

@@ -1,27 +1,45 @@ $stdout.sync = true + +=begin #################################################################################################### -# @author David Kirwan <davidkirwanirl@gmail.com> -# @description Ardtweeno Application Gateway HTTP REST API Sinatra Web App +# @author David Kirwan https://github.com/davidkirwan/ardtweeno +# @description Ardtweeno Gateway # -# @date 14-06-2013 +# @date 2014-08-12 #################################################################################################### + +This file is part of Ardtweeno. + +Ardtweeno is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +any later version. + +Ardtweeno is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Foobar. If not, see <http://www.gnu.org/licenses/>. +=end + ##### Require statements require 'rubygems' require 'sinatra/base' -require 'ardtweeno' require 'logger' +require 'ardtweeno' - class RESTAPI < Sinatra::Base ##### Sinatra Variables enable :static, :sessions, :logging set :root, File.join(File.dirname(__FILE__) + '/../../') set :public_folder, File.join(root, '/public') set :views, File.join(root, '/views') - + set :server, :puma ############################################################################################# # Create the logger instance set :log, Logger.new(STDOUT) set :level, Logger::DEBUG @@ -121,20 +139,33 @@ end get '/graph/v1/punchcard/:node' do |node| begin - theData, theDays, theRange= @@theDispatcher.constructPunchcard(params) + theData, theDays, theRange = @@theDispatcher.constructPunchcard(params) rescue Exception => e throw :halt, [ 500, "500 Internal Server Error" ] end erb :punchcard, :locals => {:node=>params[:node], :ourGraphData=>theData, :ourGraphDays=>theDays, :ourGraphRange=>theRange} end + get '/graph/v1/lineplot/:node' do |node| + begin + theData = @@theDispatcher.construct_lineplot(params) + + rescue Exception => e + raise e + #throw :halt, [ 500, "500 Internal Server Error" ] + end + + erb :lineplot, :locals => {:data=>theData, :node=>node} + end + + get "/#{settings.newsURI}/create/post" do running = @@theDispatcher.running? erb :createpost, :locals => {:running => running} end @@ -228,9 +259,11 @@ begin @@theDispatcher.store(params[:payload]) rescue Ardtweeno::NodeNotAuthorised => e throw :halt, [ 401, "401 Unauthorised" ] + rescue Ardtweeno::SensorException + throw :halt, [ 400, "400 Bad Request" ] rescue Exception => e throw :halt, [ 400, "400 Bad Request" ] end end