Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #159 from bitprophet/better-api-request-detection
Browse files Browse the repository at this point in the history
Better api request detection
  • Loading branch information
obfuscurity committed Aug 4, 2014
2 parents a13dc9a + 2296203 commit 7b2de00
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions lib/descartes/routes/cats.rb
Expand Up @@ -2,7 +2,7 @@ module Descartes
class Web < Sinatra::Base

get '/cats/:count/?' do
if request.accept.include?('application/json')
if json?
content_type 'application/json'
@cats = []
begin
Expand All @@ -21,4 +21,4 @@ class Web < Sinatra::Base
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/descartes/routes/chartroulette.rb
Expand Up @@ -2,7 +2,7 @@ module Descartes
class Web < Sinatra::Base

get '/chartroulette/?' do
if request.accept.include?("application/json")
if json?
favorites = User.filter(:email => session['user']['email']).first.favorites
content_type 'application/json'
status 200
Expand All @@ -12,4 +12,4 @@ class Web < Sinatra::Base
end
end
end
end
end
14 changes: 7 additions & 7 deletions lib/descartes/routes/dashboards.rb
Expand Up @@ -2,7 +2,7 @@ module Descartes
class Web < Sinatra::Base

get '/dashboards/?' do
if request.accept.include?('application/json')
if json?
@dashboards = []
if params[:search]
@dashboards = Dashboard.get_dashboards_with_graphs_by_search(params[:search])
Expand All @@ -19,7 +19,7 @@ class Web < Sinatra::Base
end

post '/dashboards/?' do
if request.accept.include?('application/json') && params[:uuids] && params[:name]
if json? && params[:uuids] && params[:name]
owner = api_token? ? 'api@localhost' : session['user']['uid']
@dashboard = Dashboard.new({ :owner => owner, :name => params[:name] })
@dashboard.save
Expand Down Expand Up @@ -93,7 +93,7 @@ class Web < Sinatra::Base
@graphs.sort_by! { |k| k[:created_at] }.reverse!
end
end
if request.accept.include?('application/json')
if json?
content_type 'application/json'
{ :dashboard => @dashboard, :graphs => @graphs }.to_json
else
Expand All @@ -118,7 +118,7 @@ class Web < Sinatra::Base
end

post '/dashboards/:id/favorite/?' do
if request.accept.include?('application/json')
if 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
Expand All @@ -132,7 +132,7 @@ class Web < Sinatra::Base
end

delete '/dashboards/:id/favorite/?' do
if request.accept.include?('application/json')
if 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
Expand All @@ -146,7 +146,7 @@ class Web < Sinatra::Base
end

post '/dashboards/:id/graphs/?' do
if request.accept.include?('application/json') && params[:uuids]
if 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
Expand Down Expand Up @@ -178,4 +178,4 @@ class Web < Sinatra::Base
status 204
end
end
end
end
4 changes: 2 additions & 2 deletions lib/descartes/routes/favorites.rb
Expand Up @@ -2,7 +2,7 @@ module Descartes
class Web < Sinatra::Base

get '/favorites/?' do
if request.accept.include?('application/json')
if json?
content_type 'application/json'
status 200
JSON.parse(User.filter(:uid => session['user']['uid']).first.preferences)['favorites'].to_json
Expand All @@ -11,4 +11,4 @@ class Web < Sinatra::Base
end
end
end
end
end
14 changes: 7 additions & 7 deletions lib/descartes/routes/gists.rb
Expand Up @@ -2,7 +2,7 @@ module Descartes
class Web < Sinatra::Base

get '/gists/?' do
if request.accept.include?("application/json")
if json?
content_type "application/json"
status 200
Gist.all.to_json
Expand All @@ -12,7 +12,7 @@ class Web < Sinatra::Base
end

post '/gists/?' do
if request.accept.include?("application/json")
if json?
content_type "application/json"
@gist = Gist.new(:owner => session['user']['uid'], :remote_image_url => params[:url])
@gist.save
Expand All @@ -24,7 +24,7 @@ class Web < Sinatra::Base
end

get '/gists/:uuid/?' do
if request.accept.include?("application/json")
if json?
content_type "application/json"
status 200
Gist.filter(:uuid => params[:uuid]).first.to_json
Expand All @@ -34,7 +34,7 @@ class Web < Sinatra::Base
end

put '/gists/:uuid/?' do
if request.accept.include?("application/json")
if json?
content_type "application/json"
@gist = Gist.filter(:uuid => params[:uuid]).first
params.delete('uuid')
Expand All @@ -50,7 +50,7 @@ class Web < Sinatra::Base
end

delete '/gists/:uuid/?' do
if request.accept.include?("application/json")
if json?
content_type "application/json"
Gist.filter(:uuid => params[:uuid]).first.destroy
status 204
Expand All @@ -60,7 +60,7 @@ class Web < Sinatra::Base
end

post '/gists/:uuid/comments/?' do
if request.accept.include?("application/json")
if 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])
Expand All @@ -72,4 +72,4 @@ class Web < Sinatra::Base
end
end
end
end
end
16 changes: 8 additions & 8 deletions lib/descartes/routes/graphs.rb
Expand Up @@ -2,7 +2,7 @@ module Descartes
class Web < Sinatra::Base

get '/graphs/?' do
if request.accept.include?('application/json')
if json?
@graphs = []
if params[:search]
matching_graphs = []
Expand Down Expand Up @@ -84,7 +84,7 @@ class Web < Sinatra::Base
end
end
end
if request.accept.include?('application/json')
if json?
status 200
# XXX - should return tags too
@graph.to_json
Expand All @@ -102,7 +102,7 @@ class Web < Sinatra::Base

get '/graphs/:uuid/?' do
@graph = Graph.filter(:uuid => params[:uuid]).first
if request.accept.include?('application/json')
if json?
content_type 'application/json'
status 200
@graph.to_json
Expand All @@ -115,7 +115,7 @@ class Web < Sinatra::Base
end

get '/graphs/:uuid/tags/?' do
if request.accept.include?('application/json')
if 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
Expand All @@ -127,7 +127,7 @@ class Web < Sinatra::Base
end

post '/graphs/:uuid/tags/?' do
if request.accept.include?('application/json')
if json?
content_type 'application/json'
if params[:name]
tags = []
Expand Down Expand Up @@ -183,7 +183,7 @@ class Web < Sinatra::Base
end

post '/graphs/:id/gists/?' do
if request.accept.include?('application/json')
if 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)
Expand All @@ -196,7 +196,7 @@ class Web < Sinatra::Base
end

post '/graphs/:id/comments/?' do
if request.accept.include?('application/json')
if json?
content_type 'application/json'
@graph = Graph.filter(:uuid => params[:uuid]).first
@comment = Comment.new(:owner => session['user']['uid'], :uuid => @graph.uuid)
Expand All @@ -208,4 +208,4 @@ class Web < Sinatra::Base
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/descartes/routes/helpers.rb
Expand Up @@ -5,9 +5,11 @@ class Web < Sinatra::Base
def api_token?
request.env['HTTP_X_DESCARTES_API_TOKEN'].eql?(ENV['API_TOKEN'])
end

def current_user
@current_user ||= session['user']
end

def google_callback
if session['redirect_to']
redirect_to = session['redirect_to']
Expand All @@ -23,6 +25,10 @@ def google_callback
end
redirect redirect_to || '/'
end

def json?
request.accept.map(&:to_s).include?('application/json')
end
end
end
end
6 changes: 3 additions & 3 deletions lib/descartes/routes/metrics.rb
Expand Up @@ -2,7 +2,7 @@ module Descartes
class Web < Sinatra::Base

get '/metrics/?' do
if request.accept.include?("application/json")
if json?
content_type "application/json"
status 200
Metric.all.to_json
Expand All @@ -12,7 +12,7 @@ class Web < Sinatra::Base
end

get '/metrics/search/?' do
if request.accept.include?("application/json")
if json?
content_type "application/json"
status 200
Metric.find(params[:pattern]).to_json
Expand All @@ -22,4 +22,4 @@ class Web < Sinatra::Base
end

end
end
end
4 changes: 2 additions & 2 deletions lib/descartes/routes/setup.rb
Expand Up @@ -10,7 +10,7 @@ class Web < Sinatra::Base

before do
if !(request.path_info =~ /\/auth/)
if !(api_token? && request.accept.include?("application/json"))
if !(api_token? && json?)
if !current_user
session.clear
session['redirect_to'] = request.path_info
Expand All @@ -27,4 +27,4 @@ class Web < Sinatra::Base
p e.message.split(',').first
end
end
end
end

0 comments on commit 7b2de00

Please sign in to comment.