From 2c64b67abdb86ae23cfc775a02cd70019bc8ed10 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 8 Oct 2016 12:42:17 +0800 Subject: [PATCH 1/2] return 404s for all unmatched api routes --- app/controllers/api/v1/deprecated_controller.rb | 2 +- app/controllers/api/v2/restful_controller.rb | 5 +++++ config/routes.rb | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/deprecated_controller.rb b/app/controllers/api/v1/deprecated_controller.rb index b9e07214..aa67d6b1 100644 --- a/app/controllers/api/v1/deprecated_controller.rb +++ b/app/controllers/api/v1/deprecated_controller.rb @@ -4,7 +4,7 @@ module Api class DeprecatedController < ApplicationController # rubocop:disable Style/MethodMissing def method_missing - render json: { error: '/api/v1 is deprecated! Please use /api/v2 instead.' } + render json: { error: '/api/v1 is deprecated! Please use /api/v2 instead.' }, status: :gone end # rubocop:enable Style/MethodMissing end diff --git a/app/controllers/api/v2/restful_controller.rb b/app/controllers/api/v2/restful_controller.rb index 5d8f81b3..b64682f3 100644 --- a/app/controllers/api/v2/restful_controller.rb +++ b/app/controllers/api/v2/restful_controller.rb @@ -29,6 +29,11 @@ module Api head :no_content end + def catch_404 + skip_authorization + render json: { error: '404 Not found' }, status: :not_found + end + private def accessible_records diff --git a/config/routes.rb b/config/routes.rb index 05fe5845..62728fe7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -77,6 +77,7 @@ Metamaps::Application.routes.draw do resources :users, only: [:index, :show] do get :current, on: :collection end + match '*path', to: 'restful#catch_404', via: :all end namespace :v1, path: '/v1' do # api v1 routes all lead to a deprecation error method @@ -88,7 +89,9 @@ Metamaps::Application.routes.draw do resources :tokens, only: [:create, :destroy] do get :my_tokens, on: :collection end + match '*path', to: 'deprecated#method_missing', via: :all end + match '*path', to: 'v2/restful#catch_404', via: :all end devise_for :users, skip: :sessions, controllers: { From 9513087bbddcefdf86c46e8d3284781ff6674228 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 8 Oct 2016 14:00:29 +0800 Subject: [PATCH 2/2] remove unnecessary api v1 code --- app/controllers/api/v1/deprecated_controller.rb | 8 ++++---- app/controllers/api/v1/mappings_controller.rb | 7 ------- app/controllers/api/v1/maps_controller.rb | 7 ------- app/controllers/api/v1/synapses_controller.rb | 7 ------- app/controllers/api/v1/tokens_controller.rb | 7 ------- app/controllers/api/v1/topics_controller.rb | 7 ------- config/routes.rb | 12 ++---------- 7 files changed, 6 insertions(+), 49 deletions(-) delete mode 100644 app/controllers/api/v1/mappings_controller.rb delete mode 100644 app/controllers/api/v1/maps_controller.rb delete mode 100644 app/controllers/api/v1/synapses_controller.rb delete mode 100644 app/controllers/api/v1/tokens_controller.rb delete mode 100644 app/controllers/api/v1/topics_controller.rb diff --git a/app/controllers/api/v1/deprecated_controller.rb b/app/controllers/api/v1/deprecated_controller.rb index aa67d6b1..3269a1a8 100644 --- a/app/controllers/api/v1/deprecated_controller.rb +++ b/app/controllers/api/v1/deprecated_controller.rb @@ -2,11 +2,11 @@ module Api module V1 class DeprecatedController < ApplicationController - # rubocop:disable Style/MethodMissing - def method_missing - render json: { error: '/api/v1 is deprecated! Please use /api/v2 instead.' }, status: :gone + def deprecated + render json: { + error: '/api/v1 has been deprecated! Please use /api/v2 instead.' + }, status: :gone end - # rubocop:enable Style/MethodMissing end end end diff --git a/app/controllers/api/v1/mappings_controller.rb b/app/controllers/api/v1/mappings_controller.rb deleted file mode 100644 index 8ba6e704..00000000 --- a/app/controllers/api/v1/mappings_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true -module Api - module V1 - class MappingsController < DeprecatedController - end - end -end diff --git a/app/controllers/api/v1/maps_controller.rb b/app/controllers/api/v1/maps_controller.rb deleted file mode 100644 index 0ff6f472..00000000 --- a/app/controllers/api/v1/maps_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true -module Api - module V1 - class MapsController < DeprecatedController - end - end -end diff --git a/app/controllers/api/v1/synapses_controller.rb b/app/controllers/api/v1/synapses_controller.rb deleted file mode 100644 index 32522e52..00000000 --- a/app/controllers/api/v1/synapses_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true -module Api - module V1 - class SynapsesController < DeprecatedController - end - end -end diff --git a/app/controllers/api/v1/tokens_controller.rb b/app/controllers/api/v1/tokens_controller.rb deleted file mode 100644 index 9df2094a..00000000 --- a/app/controllers/api/v1/tokens_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true -module Api - module V1 - class TokensController < DeprecatedController - end - end -end diff --git a/app/controllers/api/v1/topics_controller.rb b/app/controllers/api/v1/topics_controller.rb deleted file mode 100644 index d316bfa8..00000000 --- a/app/controllers/api/v1/topics_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true -module Api - module V1 - class TopicsController < DeprecatedController - end - end -end diff --git a/config/routes.rb b/config/routes.rb index 62728fe7..b5078d86 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -80,16 +80,8 @@ Metamaps::Application.routes.draw do match '*path', to: 'restful#catch_404', via: :all end namespace :v1, path: '/v1' do - # api v1 routes all lead to a deprecation error method - # see app/controllers/api/v1/deprecated_controller.rb - resources :maps, only: [:create, :show, :update, :destroy] - resources :synapses, only: [:create, :show, :update, :destroy] - resources :topics, only: [:create, :show, :update, :destroy] - resources :mappings, only: [:create, :show, :update, :destroy] - resources :tokens, only: [:create, :destroy] do - get :my_tokens, on: :collection - end - match '*path', to: 'deprecated#method_missing', via: :all + root to: 'deprecated#deprecated', via: :all + match '*path', to: 'deprecated#deprecated', via: :all end match '*path', to: 'v2/restful#catch_404', via: :all end