dodonki1223 / rails-api

Udemy の REST API with Ruby on Rails: The Complete Guide

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

REST API with Ruby on Rails: The Complete Guide

UdemyREST API with Ruby on Rails: The Complete Guide で作成した Rails 製の REST API です
以下のリポジトリが元コードになります(若干変えています)

環境について

バージョン情報

ソフトウェアスタック バージョン
Rails 6.0.3.2
Ruby 2.6.5
SQLite 3.28.0

Serializerについて

デフォルトだと Jbuilder が入っているがオブジェクト指向的な書き方ができる ActiveModelSerializers を使用してAPIを作成している
ただ、ActiveModelSerializers は更新が止まっているので使うのは止めたほうがよい

候補としては以下の2つ

Gem名 備考
Fast JSON API Netflix製だが開発が止まっているので止めたほうが良さそう
JSON:API Serialization Library Fast JSON API をForkして作成されている Gem です

メンテナンスされていないものが多いのでデフォルトの Jbuilder を使用したほうが無難かもしれない……

APIのJSONベースフォーマット

JSON API の形式を採用しています

サンプル

{
  "links": {
    "self": "http://example.com/articles",
    "next": "http://example.com/articles?page[offset]=2",
    "last": "http://example.com/articles?page[offset]=10"
  },
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON:API paints my bikeshed!"
    },
    "relationships": {
      "author": {
        "links": {
          "self": "http://example.com/articles/1/relationships/author",
          "related": "http://example.com/articles/1/author"
        },
        "data": { "type": "people", "id": "9" }
      },
      "comments": {
        "links": {
          "self": "http://example.com/articles/1/relationships/comments",
          "related": "http://example.com/articles/1/comments"
        },
        "data": [
          { "type": "comments", "id": "5" },
          { "type": "comments", "id": "12" }
        ]
      }
    },
    "links": {
      "self": "http://example.com/articles/1"
    }
  }],
  "included": [{
    "type": "people",
    "id": "9",
    "attributes": {
      "firstName": "Dan",
      "lastName": "Gebhardt",
      "twitter": "dgeb"
    },
    "links": {
      "self": "http://example.com/people/9"
    }
  }, {
    "type": "comments",
    "id": "5",
    "attributes": {
      "body": "First!"
    },
    "relationships": {
      "author": {
        "data": { "type": "people", "id": "2" }
      }
    },
    "links": {
      "self": "http://example.com/comments/5"
    }
  }, {
    "type": "comments",
    "id": "12",
    "attributes": {
      "body": "I like XML better"
    },
    "relationships": {
      "author": {
        "data": { "type": "people", "id": "9" }
      }
    },
    "links": {
      "self": "http://example.com/comments/12"
    }
  }]
}

認証について

OAuthの認証の流れは画像の通りになります

OAuth

認証方法は2通りあります

  • OAuth(Github) を使用して認証
  • ログイン、パスワードを使用して認証(予め sign_up が必要)

UserAuthenticator.rb を元に受け取るパラメータにより上記、認証を切り分ける

未実装機能

  • GitHub の2段階認証に未対応
    • 実装する場合は こちら を参考にすること

修正案

例外処理をもっといい感じに書けるので以下を参考にすると良さそう

About

Udemy の REST API with Ruby on Rails: The Complete Guide


Languages

Language:Ruby 99.7%Language:HTML 0.3%