LoginSignup
11
8

More than 5 years have passed since last update.

Ruby on Railsを使ってノンコーディングでAPIサーバーを作る

Last updated at Posted at 2016-05-01

前提

  • ページングを扱いません
  • モデルの関連を扱いません
  • Rails::APIを使いません
  • Ubuntu 14.04
  • Ruby 2.2.5
  • Rails 4.2.6
  • MySQL 5.7

準備

OttoでRubyアプリケーション向けの環境を作りました。
ライブラリ類はだいたい入っている前提です。

アプリケーションを作る前は、MySQLで必要なライブラリが入らないので

sudo apt-get install libmysqlclient-dev

を実行します。

Ottoの環境の作り方はottoで構築する環境のubuntuのバージョンを上げるを見てください。
今日の時点では割と高いハードルです。

手順

Railsをインストール

bundle init

で、Gemifileを作ります。
Railsのコメントアウトを外します。

bundle install

で、Railsをインストールします。

Railsアプリケーションを作成

bundle exec rails new battle_beast -BJTS -d mysql

オプションで次の指定をします。

  • bundle installを行わない
  • javascriptを組み込まない
  • test::unitを組み込まない
  • sprocketsを組み込まない
  • データベースの種類にmysqlを指定

Gemfile編集

使わない

  • sdoc
  • byebug
  • web-console
  • spring

を消します。

source 'https://rubygems.org'

gem 'rails', '4.2.6'
gem 'mysql2'
gem 'jbuilder', '~> 2.0'

bundle install

bundle install --without staging production --jobs=4

jobsオプションは高速化のためのものです。

Bundlerで並列処理??bundle installを爆速で処理する方法。

下記のコマンドで並列処理されたbundle installが実行できます。
bundle install --jobs=4

config/application.rb編集

scaffoldでAPIで使わないものを生成しない設定にします。

    config.generators do |g|
      g.assets false
      g.helper false
      g.template_engine false
    end

scaffold

bundle exec rails generate scaffold User name:string

config/database.ymlを修正

username: root
password: root
host: mysql.service.consul

DB作成

bundle exec rake db:create
bundle exec rake db:migrate

protect_from_forgeryの無効化

APIの認証は別途行う前提で、RailsのCSRF対策機能を無効化します。

app/controllers/application_controller.rbを修正

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session
end

ノンコーディングというのは厳密には嘘です。

Rails4でトークン認証のアクションに対してCSRFを無効にする - hokaccha hamalog v3

APIとかでトークン使って認証する場合はCSRFの対策いらないので無効にしたい。
その場合は
protect_from_forgery with: :null_session

起動

bundle exec rackup -o 0.0.0.0

動作確認

IPアドレスはVirtualBox上の仮想環境のIPアドレスです。
各自の環境に合わせて、適当に読み替えてください。

POST

curl -XPOST 'http://100.92.136.162:9292/users' -H content-type:application/json -d '{
  "name": "桂歌丸"
}'

キー名にuserを指定しても良いです。

curl -XPOST 'http://100.92.136.162:9292/users' -H content-type:application/json -d '{
  "user": {"name": "桂歌丸"}
}'

GET

curl 'http://100.92.136.162:9292/users'

レスポンスは

[{"id":1,"name":"桂歌丸","url":"http://100.92.136.162:9292/users/1.json"}]

です。

PUT

curl -XPUT 'http://100.92.136.162:9292/users/1' -H content-type:application/json -d '{
  "name": "イチロー"
}'

キー名にuserを指定しても良いです。

curl -XPUT 'http://100.92.136.162:9292/users/1' -H content-type:application/json -d '{
  "user": {"name": "イチロー"}
}'

DELETE

curl -XDELETE 'http://100.92.136.162:9292/users/1'

参考

11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8