Ruby on Rails Grape Gem
루비 온 레일스 Grape Gem 사용하기
Grape 젬은 레일스 환경에서 REST 와 유사한 API 프레임워크이다.
Grape GitHub
Grape Gem GitHub HomePage
Install Gem
Gemfile 에 grape 젬을 추가해준뒤 bundle install 명령어를 실행한다.
설정하기
적용하기 위한 프로젝트의 레일스 버전이 7버전으로 grape github README 를 따라 6버전 이상의 세팅 방법을 따라 진행 하였다.
path: ./config/initializers/inflections.rb
아래의 코드가 이미 작성되어있고 주석처리가 되어있다. 해당부분 주석을 제거하고 작성해주면 된다.
1 | ActiveSupport::Inflector.inflections(:en) do |inflect| |
컨트롤러 생성
path: ./app/api/base_api.rb
1 | class BaseApi < Grape::API |
path: ./app/api/default_controller.rb
1 | class DefaultController < BaseApi |
path: ./config/routes.rb
1 | Rails.application.routes.draw do |
curl을 이용하여 요청하기
User Controller 생성하기
path: ./app/api/user_controller/user_api.rb
1 | module UserController |
POST /users
GET /users/all
Ruby on Rails Grape Gem
http://hanjuren.github.io/2022/03/06/ruby/Ruby-on-Rails-Grape-Gem/