Permalink
Browse files

refactoring

  • Loading branch information...
1 parent 7a04ccc commit 9d328e544f8320418cd8417630457e32c36fe3cb @kzytateishi committed Mar 7, 2015
Showing with 387 additions and 363 deletions.
  1. +0 −4 app/api/api.rb
  2. +0 −30 app/api/v1/base.rb
  3. +0 −68 app/api/v1/comments.rb
  4. +0 −70 app/api/v1/message_boards.rb
  5. +0 −30 app/api/v2/base.rb
  6. +0 −68 app/api/v2/comments.rb
  7. +0 −68 app/api/v2/message_boards.rb
  8. +6 −0 app/apis/api/base.rb
  9. +36 −0 app/apis/api/v1/base.rb
  10. +70 −0 app/apis/api/v1/comments.rb
  11. +72 −0 app/apis/api/v1/message_boards.rb
  12. +36 −0 app/apis/api/v2/base.rb
  13. +70 −0 app/apis/api/v2/comments.rb
  14. +72 −0 app/apis/api/v2/message_boards.rb
  15. +0 −3 app/views/api/v1/comments/index.jbuilder
  16. +0 −3 app/views/api/v1/message_boards/show.jbuilder
  17. +0 −3 app/views/api/v2/comments/index.jbuilder
  18. +0 −3 app/views/api/v2/message_boards/show.jbuilder
  19. 0 app/views/{api/v2 → apis/api/v1}/comments/_comment.jbuilder
  20. +3 −0 app/views/apis/api/v1/comments/index.jbuilder
  21. 0 app/views/{api/v2 → apis/api/v1}/message_boards/_message_board.jbuilder
  22. +2 −2 app/views/{ → apis}/api/v1/message_boards/index.jbuilder
  23. +3 −0 app/views/apis/api/v1/message_boards/show.jbuilder
  24. 0 app/views/{api/v1 → apis/api/v2}/comments/_comment.jbuilder
  25. +3 −0 app/views/apis/api/v2/comments/index.jbuilder
  26. 0 app/views/{api/v1 → apis/api/v2}/message_boards/_message_board.jbuilder
  27. +2 −2 app/views/{ → apis}/api/v2/message_boards/index.jbuilder
  28. +3 −0 app/views/apis/api/v2/message_boards/show.jbuilder
  29. +3 −3 config/application.rb
  30. +1 −1 config/routes.rb
  31. +1 −1 lib/tasks/routes.rake
  32. +1 −1 spec/{ → apis}/api/v1/comments_spec.rb
  33. +1 −1 spec/{ → apis}/api/v1/message_boards_spec.rb
  34. +1 −1 spec/{ → apis}/api/v2/comments_spec.rb
  35. +1 −1 spec/{ → apis}/api/v2/message_boards_spec.rb
View
@@ -1,4 +0,0 @@
-class API < Grape::API
- mount V1::Base
- mount V2::Base
-end
View
@@ -1,30 +0,0 @@
-module V1
- class Base < Grape::API
- format :json
- default_format :json
-
- # for Grape::Jbuilder
- formatter :json, Grape::Formatter::Jbuilder
-
- prefix :api # /apiというパスになる
- version 'v1', using: :path # /api/v1というパスになる
-
- # 例外ハンドル 404
- rescue_from ActiveRecord::RecordNotFound do |e|
- rack_response({ message: e.message, status: 404 }.to_json, 404)
- end
-
- # 例外ハンドル 400
- rescue_from Grape::Exceptions::ValidationErrors do |e|
- rack_response e.to_json, 400
- end
-
- # 例外ハンドル 500
- rescue_from :all do |e|
- error_response(message: "Internal server error", status: 500)
- end
-
- mount V1::MessageBoards
- mount V1::Comments
- end
-end
@@ -1,68 +0,0 @@
-module V1
- class Comments < Grape::API
- helpers do
- # Strong Parametersの設定
- def comment_params
- ActionController::Parameters.new(params).permit(:body)
- end
-
- def set_message_board
- @message_board = MessageBoard.find(params[:message_board_id])
- end
-
- def set_comment
- @comment = @message_board.comments.find(params[:id])
- end
-
- # パラメータのチェック
- params :attributes do
- requires :body, type: String, desc: "MessageBoard body."
- end
-
- # パラメータのチェック
- params :message_board_id do
- requires :message_board_id, type: Integer, desc: "MessageBoard id."
- end
-
- # パラメータのチェック
- params :id do
- requires :id, type: Integer, desc: "MessageBoard id."
- end
- end
-
- resource :message_boards do
- params do
- use :message_board_id
- end
-
- route_param :message_board_id do
- resource :comments do
- desc 'GET /api/v1/message_boards/:message_board_id/comments'
- get '/', jbuilder: 'v1/comments/index' do
- set_message_board
- @comments = @message_board.comments
- end
-
- desc 'POST /api/v1/message_boards/:message_board_id/comments'
- params do
- use :attributes
- end
- post do
- set_message_board
- @message_board.comments.create(comment_params)
- end
-
- desc "DELETE /api/v1/message_boards/:message_board_id/comments/:id"
- params do
- use :id
- end
- delete '/:id' do
- set_message_board
- set_comment
- @comment.destroy
- end
- end
- end
- end
- end
-end
@@ -1,70 +0,0 @@
-module V1
- class MessageBoards < Grape::API
- helpers do
- # Strong Parametersの設定
- def message_board_params
- ActionController::Parameters.new(params).permit(:title, :body)
- end
-
- def set_message_board
- @message_board = MessageBoard.find(params[:id])
- end
-
- # パラメータのチェック
- # パラメーターの必須、任意を指定することができる。
- # use :attributesという形で使うことができる。
- params :attributes do
- requires :title, type: String, desc: "MessageBoard title."
- optional :body, type: String, desc: "MessageBoard body."
- end
-
- # パラメータのチェック
- params :id do
- requires :id, type: Integer, desc: "MessageBoard id."
- end
- end
-
- resource :message_boards do
- desc 'GET /api/v1/message_boards'
- get '/', jbuilder: 'v1/message_boards/index' do
- @message_boards = MessageBoard.all
- end
-
- desc 'POST /api/v1/message_boards'
- params do
- use :attributes
- end
- post '/' do
- message_board = MessageBoard.new(message_board_params)
- message_board.save
- end
-
- desc 'GET /api/v1/message_boards/:id'
- params do
- use :id
- end
- get '/:id', jbuilder: 'v1/message_boards/show' do
- set_message_board
- end
-
- desc 'PUT /api/v1/message_boards/:id'
- params do
- use :id
- use :attributes
- end
- put '/:id' do
- set_message_board
- @message_board.update(message_board_params)
- end
-
- desc 'DELETE /api/v1/message_boards/:id'
- params do
- use :id
- end
- delete '/:id' do
- set_message_board
- @message_board.destroy
- end
- end
- end
-end
View
@@ -1,30 +0,0 @@
-module V2
- class Base < Grape::API
- format :json
- default_format :json
-
- # for Grape::Jbuilder
- formatter :json, Grape::Formatter::Jbuilder
-
- prefix :api # /apiというパスになる
- version 'v2', using: :path # /api/v2というパスになる
-
- # 例外ハンドル 404
- rescue_from ActiveRecord::RecordNotFound do |e|
- rack_response({ message: e.message, status: 404 }.to_json, 404)
- end
-
- # 例外ハンドル 400
- rescue_from Grape::Exceptions::ValidationErrors do |e|
- rack_response e.to_json, 400
- end
-
- # 例外ハンドル 500
- rescue_from :all do |e|
- error_response(message: "Internal server error", status: 500)
- end
-
- mount V2::MessageBoards
- mount V2::Comments
- end
-end
@@ -1,68 +0,0 @@
-module V2
- class Comments < Grape::API
- helpers do
- # Strong Parametersの設定
- def comment_params
- ActionController::Parameters.new(params).permit(:body)
- end
-
- def set_message_board
- @message_board = MessageBoard.find(params[:message_board_id])
- end
-
- def set_comment
- @comment = @message_board.comments.find(params[:id])
- end
-
- # パラメータのチェック
- params :attributes do
- requires :body, type: String, desc: "MessageBoard body."
- end
-
- # パラメータのチェック
- params :message_board_id do
- requires :message_board_id, type: Integer, desc: "MessageBoard id."
- end
-
- # パラメータのチェック
- params :id do
- requires :id, type: Integer, desc: "MessageBoard id."
- end
- end
-
- resource :message_boards do
- params do
- use :message_board_id
- end
-
- route_param :message_board_id do
- resource :comments do
- desc 'GET /api/v2/message_boards/:message_board_id/comments'
- get '/', jbuilder: 'v2/comments/index' do
- set_message_board
- @comments = @message_board.comments
- end
-
- desc 'POST /api/v2/message_boards/:message_board_id/comments'
- params do
- use :attributes
- end
- post do
- set_message_board
- @message_board.comments.create(comment_params)
- end
-
- desc "DELETE /api/v2/message_boards/:message_board_id/comments/:id"
- params do
- use :id
- end
- delete '/:id' do
- set_message_board
- set_comment
- @comment.destroy
- end
- end
- end
- end
- end
-end
@@ -1,68 +0,0 @@
-module V2
- class MessageBoards < Grape::API
- helpers do
- # Strong Parametersの設定
- def message_board_params
- ActionController::Parameters.new(params).permit(:title, :body)
- end
-
- def set_message_board
- @message_board = MessageBoard.find(params[:id])
- end
-
- # パラメータのチェック
- params :attributes do
- requires :title, type: String, desc: "MessageBoard title."
- optional :body, type: String, desc: "MessageBoard body."
- end
-
- # パラメータのチェック
- params :id do
- requires :id, type: Integer, desc: "MessageBoard id."
- end
- end
-
- resource :message_boards do
- desc 'GET /api/v2/message_boards'
- get '/', jbuilder: 'v2/message_boards/index' do
- @message_boards = MessageBoard.all
- end
-
- desc 'POST /api/v2/message_boards'
- params do
- use :attributes
- end
- post '/' do
- message_board = MessageBoard.new(message_board_params)
- message_board.save
- end
-
- desc 'GET /api/v2/message_boards/:id'
- params do
- use :id
- end
- get '/:id', jbuilder: 'v2/message_boards/show' do
- set_message_board
- end
-
- desc 'PUT /api/v2/message_boards/:id'
- params do
- use :id
- use :attributes
- end
- put '/:id' do
- set_message_board
- @message_board.update(message_board_params)
- end
-
- desc 'DELETE /api/v2/message_boards/:id'
- params do
- use :id
- end
- delete '/:id' do
- set_message_board
- @message_board.destroy
- end
- end
- end
-end
@@ -0,0 +1,6 @@
+module API
+ class Base < Grape::API
+ mount V1::Base
+ mount V2::Base
+ end
+end
@@ -0,0 +1,36 @@
+module API
+ module V1
+ class Base < Grape::API
+ format :json
+ default_format :json
+
+ # for Grape::Jbuilder
+ formatter :json, Grape::Formatter::Jbuilder
+
+ prefix :api # /apiというパスになる
+ version 'v1', using: :path # /api/v1というパスになる
+
+ # 例外ハンドル 404
+ rescue_from ActiveRecord::RecordNotFound do |e|
+ rack_response({ message: e.message, status: 404 }.to_json, 404)
+ end
+
+ # 例外ハンドル 400
+ rescue_from Grape::Exceptions::ValidationErrors do |e|
+ rack_response e.to_json, 400
+ end
+
+ # 例外ハンドル 500
+ rescue_from :all do |e|
+ if Rails.env.development?
+ raise e
+ else
+ error_response(message: "Internal server error", status: 500)
+ end
+ end
+
+ mount V1::MessageBoards
+ mount V1::Comments
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 9d328e5

Please sign in to comment.