From a13dc9a720dab33e0a8d5e8670bd30f93e0bfdba Mon Sep 17 00:00:00 2001 From: obfuscurity Date: Thu, 6 Feb 2014 22:33:07 -0500 Subject: [PATCH] Revert "use request.xhr? with newer Sinatra" This reverts commit cc6b4c055f262b9a5cb5c26e4daf610c550c7d71. --- lib/descartes/routes/cats.rb | 2 +- lib/descartes/routes/chartroulette.rb | 2 +- lib/descartes/routes/dashboards.rb | 12 ++++++------ lib/descartes/routes/favorites.rb | 2 +- lib/descartes/routes/gists.rb | 12 ++++++------ lib/descartes/routes/graphs.rb | 14 +++++++------- lib/descartes/routes/metrics.rb | 4 ++-- lib/descartes/routes/setup.rb | 2 +- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/descartes/routes/cats.rb b/lib/descartes/routes/cats.rb index 2d24d77..ef8ba4a 100644 --- a/lib/descartes/routes/cats.rb +++ b/lib/descartes/routes/cats.rb @@ -2,7 +2,7 @@ module Descartes class Web < Sinatra::Base get '/cats/:count/?' do - if request.xhr? + if request.accept.include?('application/json') content_type 'application/json' @cats = [] begin diff --git a/lib/descartes/routes/chartroulette.rb b/lib/descartes/routes/chartroulette.rb index c1a3c65..db3f1ff 100644 --- a/lib/descartes/routes/chartroulette.rb +++ b/lib/descartes/routes/chartroulette.rb @@ -2,7 +2,7 @@ module Descartes class Web < Sinatra::Base get '/chartroulette/?' do - if request.xhr? + if request.accept.include?("application/json") favorites = User.filter(:email => session['user']['email']).first.favorites content_type 'application/json' status 200 diff --git a/lib/descartes/routes/dashboards.rb b/lib/descartes/routes/dashboards.rb index 797b966..880b43a 100644 --- a/lib/descartes/routes/dashboards.rb +++ b/lib/descartes/routes/dashboards.rb @@ -2,7 +2,7 @@ module Descartes class Web < Sinatra::Base get '/dashboards/?' do - if request.xhr? + if request.accept.include?('application/json') @dashboards = [] if params[:search] @dashboards = Dashboard.get_dashboards_with_graphs_by_search(params[:search]) @@ -19,7 +19,7 @@ class Web < Sinatra::Base end post '/dashboards/?' do - if request.xhr? && params[:uuids] && params[:name] + if request.accept.include?('application/json') && params[:uuids] && params[:name] owner = api_token? ? 'api@localhost' : session['user']['uid'] @dashboard = Dashboard.new({ :owner => owner, :name => params[:name] }) @dashboard.save @@ -93,7 +93,7 @@ class Web < Sinatra::Base @graphs.sort_by! { |k| k[:created_at] }.reverse! end end - if request.xhr? + if request.accept.include?('application/json') content_type 'application/json' { :dashboard => @dashboard, :graphs => @graphs }.to_json else @@ -118,7 +118,7 @@ class Web < Sinatra::Base end post '/dashboards/:id/favorite/?' do - if request.xhr? + if request.accept.include?('application/json') if @dashboard = Dashboard.filter(:enabled => true, :uuid => params[:id]).first User.filter(:uid => session['user']['uid']).first.add_favorite(@dashboard.uuid) session['user']['preferences'] = User.filter(:uid => session['user']['uid']).first.preferences @@ -132,7 +132,7 @@ class Web < Sinatra::Base end delete '/dashboards/:id/favorite/?' do - if request.xhr? + if request.accept.include?('application/json') if @dashboard = Dashboard.filter(:enabled => true, :uuid => params[:id]).first User.filter(:uid => session['user']['uid']).first.remove_favorite(@dashboard.uuid) session['user']['preferences'] = User.filter(:uid => session['user']['uid']).first.preferences @@ -146,7 +146,7 @@ class Web < Sinatra::Base end post '/dashboards/:id/graphs/?' do - if request.xhr? && params[:uuids] + if request.accept.include?('application/json') && params[:uuids] @dashboard = Dashboard.filter(:enabled => true, :uuid => params[:id]).first params[:uuids].split(',').each do |g_uuid| @graph = Graph.filter(:enabled => true, :uuid => g_uuid).first diff --git a/lib/descartes/routes/favorites.rb b/lib/descartes/routes/favorites.rb index 2cb72f5..b196aaa 100644 --- a/lib/descartes/routes/favorites.rb +++ b/lib/descartes/routes/favorites.rb @@ -2,7 +2,7 @@ module Descartes class Web < Sinatra::Base get '/favorites/?' do - if request.xhr? + if request.accept.include?('application/json') content_type 'application/json' status 200 JSON.parse(User.filter(:uid => session['user']['uid']).first.preferences)['favorites'].to_json diff --git a/lib/descartes/routes/gists.rb b/lib/descartes/routes/gists.rb index 33f2386..9d08e94 100644 --- a/lib/descartes/routes/gists.rb +++ b/lib/descartes/routes/gists.rb @@ -2,7 +2,7 @@ module Descartes class Web < Sinatra::Base get '/gists/?' do - if request.xhr? + if request.accept.include?("application/json") content_type "application/json" status 200 Gist.all.to_json @@ -12,7 +12,7 @@ class Web < Sinatra::Base end post '/gists/?' do - if request.xhr? + if request.accept.include?("application/json") content_type "application/json" @gist = Gist.new(:owner => session['user']['uid'], :remote_image_url => params[:url]) @gist.save @@ -24,7 +24,7 @@ class Web < Sinatra::Base end get '/gists/:uuid/?' do - if request.xhr? + if request.accept.include?("application/json") content_type "application/json" status 200 Gist.filter(:uuid => params[:uuid]).first.to_json @@ -34,7 +34,7 @@ class Web < Sinatra::Base end put '/gists/:uuid/?' do - if request.xhr? + if request.accept.include?("application/json") content_type "application/json" @gist = Gist.filter(:uuid => params[:uuid]).first params.delete('uuid') @@ -50,7 +50,7 @@ class Web < Sinatra::Base end delete '/gists/:uuid/?' do - if request.xhr? + if request.accept.include?("application/json") content_type "application/json" Gist.filter(:uuid => params[:uuid]).first.destroy status 204 @@ -60,7 +60,7 @@ class Web < Sinatra::Base end post '/gists/:uuid/comments/?' do - if request.xhr? + if request.accept.include?("application/json") content_type "application/json" @gist = Gist.filter(:uuid => params[:uuid]).first @comment = Comment.new(:owner => session['user']['uid'], :g_uuid => params[:uuid], :body => params[:body]) diff --git a/lib/descartes/routes/graphs.rb b/lib/descartes/routes/graphs.rb index 7c91712..c09b169 100644 --- a/lib/descartes/routes/graphs.rb +++ b/lib/descartes/routes/graphs.rb @@ -2,7 +2,7 @@ module Descartes class Web < Sinatra::Base get '/graphs/?' do - if request.xhr? + if request.accept.include?('application/json') @graphs = [] if params[:search] matching_graphs = [] @@ -84,7 +84,7 @@ class Web < Sinatra::Base end end end - if request.xhr? + if request.accept.include?('application/json') status 200 # XXX - should return tags too @graph.to_json @@ -102,7 +102,7 @@ class Web < Sinatra::Base get '/graphs/:uuid/?' do @graph = Graph.filter(:uuid => params[:uuid]).first - if request.xhr? + if request.accept.include?('application/json') content_type 'application/json' status 200 @graph.to_json @@ -115,7 +115,7 @@ class Web < Sinatra::Base end get '/graphs/:uuid/tags/?' do - if request.xhr? + if request.accept.include?('application/json') content_type 'application/json' @graph = Graph.filter(:uuid => params[:uuid]).first @tags = Tag.select(:id, :name).filter(:graph_id => @graph.id).order(:id).all @@ -127,7 +127,7 @@ class Web < Sinatra::Base end post '/graphs/:uuid/tags/?' do - if request.xhr? + if request.accept.include?('application/json') content_type 'application/json' if params[:name] tags = [] @@ -183,7 +183,7 @@ class Web < Sinatra::Base end post '/graphs/:id/gists/?' do - if request.xhr? + if request.accept.include?('application/json') content_type 'application/json' @graph = Graph.filter(:uuid => params[:id]).first @gist = Gist.new(:owner => session['user']['uid'], :url => params[:url], :name => @graph.name, :data => params[:data], :graph_id => @graph.id) @@ -196,7 +196,7 @@ class Web < Sinatra::Base end post '/graphs/:id/comments/?' do - if request.xhr? + if request.accept.include?('application/json') content_type 'application/json' @graph = Graph.filter(:uuid => params[:uuid]).first @comment = Comment.new(:owner => session['user']['uid'], :uuid => @graph.uuid) diff --git a/lib/descartes/routes/metrics.rb b/lib/descartes/routes/metrics.rb index 81f01da..cd9abfc 100644 --- a/lib/descartes/routes/metrics.rb +++ b/lib/descartes/routes/metrics.rb @@ -2,7 +2,7 @@ module Descartes class Web < Sinatra::Base get '/metrics/?' do - if request.xhr? + if request.accept.include?("application/json") content_type "application/json" status 200 Metric.all.to_json @@ -12,7 +12,7 @@ class Web < Sinatra::Base end get '/metrics/search/?' do - if request.xhr? + if request.accept.include?("application/json") content_type "application/json" status 200 Metric.find(params[:pattern]).to_json diff --git a/lib/descartes/routes/setup.rb b/lib/descartes/routes/setup.rb index de6e2fa..d6e1515 100644 --- a/lib/descartes/routes/setup.rb +++ b/lib/descartes/routes/setup.rb @@ -10,7 +10,7 @@ class Web < Sinatra::Base before do if !(request.path_info =~ /\/auth/) - if !(api_token? && request.xhr?) + if !(api_token? && request.accept.include?("application/json")) if !current_user session.clear session['redirect_to'] = request.path_info