Commits

Sean Wilkinson committed 2dda273

Made the Rack app a little prettier

Comments (0)

Files changed (1)

src/rack-app/config.ru

 #   I do plan to merge this program with the Ruby gem in the future, which is
 #   why the database schema matches the Node.js implementation's (which is not
 #   as straight-forward as it could be). For now, it serves its purpose, and it
-#   does so in just 94 lines of source code ;-)
+#   does so in just 95 lines of source code ;-)
 #
 #   NOTE: Using a "%" character incorrectly in a URL will cause you great
 #   anguish, and there isn't a good way for me to handle this problem "softly"
 #   of a 'box', 'key', or 'status' value.
 #
 #                                                       ~~ (c) SRW, 24 Apr 2013
-#                                                   ~~ last updated 17 Jun 2013
+#                                                   ~~ last updated 22 Jun 2013
 
 require 'rubygems'
 require 'bundler'
     get '/box/:box' do
       # This route responds to API calls that "read" from persistent storage,
       # such as when checking for new tasks to run or downloading results.
-        hang_up unless (params[:key] or params[:status])
-        if (params[:key]) then
+        if (params[:key].is_a?(String)) then
           # This arm runs when a client requests the value of a specific avar.
             bk = "#{params[:box]}&#{params[:key]}"
             x = db_query("SELECT body FROM avars WHERE box_key = '#{bk}'")
             y = (x.length == 0) ? '{}' : x[0][0]
-        elsif (params[:status]) then
+        elsif (params[:status].is_a?(String)) then
           # This arm runs when a client requests a task queue.
             bs = "#{params[:box]}&#{params[:status]}"
             x = db_query("SELECT key FROM avars WHERE box_status = '#{bs}'")
             y = (x.length == 0) ? '[]' : (x.map {|row| row[0]}).to_json
+        else
+            hang_up
         end
         cross_origin if (settings.enable_CORS == true)
         [200, {'Content-Type' => 'application/json'}, [y]]
     post '/box/:box' do
       # This route responds to API calls that "write" to persistent storage,
       # such as when uploading results or submitting new tasks.
-        hang_up unless (params[:key])
+        hang_up unless (params[:key].is_a?(String))
         body, ed = [request.body.read, now_plus(settings.avar_ttl)]
         x = JSON.parse(body)
         hang_up unless (params[:key] == x['key'])
         bk, bs = "#{x['box']}&#{x['key']}", "#{x['box']}&#{x['status']}"
-        if (x['status']) then
+        if (x['status'].is_a?(String)) then
           # This arm runs only when a client writes a task description.
             db_query <<-sql
                 INSERT OR REPLACE INTO avars