LoginSignup
15
12

More than 5 years have passed since last update.

Grapeを使ってRESTfulAPIを簡単作成 その2 Grape::Entity

Last updated at Posted at 2016-12-10

Grape::Entityとは

前項のgrapeのみだと、値が全て表示されてしまいます。
そこで、grape-entityを使うと、表示項目の制限、整形をすることができます。
GitHub: ruby-grape/grape-entity

目次

1.Grapeを使ってRESTfulAPIを簡単作成 その1 Grape
2.Grapeを使ってRESTfulAPIを簡単作成 その2 Grape::Entity
3.Grapeを使ってRESTfulAPIを簡単作成 その3 Grape::Swagger

環境

  • rails 5.0.0.1
  • ruby 2.3.1

まずはgemをインストール

下記を追記

Gemfile
# api
gem 'grape'
gem 'grape-entity'

bundle install

$ bundle install --path vendor/bundle

ディレクトリ構成

下記のディレクトリ構成にします。

~/sample_api/
  ├ app/api/
  │    └ api.rb
  │    └ resources/
  │          └ v1/
  │            └ root.rb
  │        └ user.rb
  │       └ entities/
  │      └ v1/
  │        └ root_entity.rb
  │        └ user_entity.rb
  • app/api/entities/v1/root_entity.rb
    このファイル名は好きなもので構いません。
    その場合は、app/api/entities/v1/user_entity.rbで継承する親クラスの名前も適宜変更してください。

Entityの親クラスを作成

app/api/entities/v1/root_entity.rb

app/api/entities/v1/root_entity.rb
module Entities
  module V1
    class RootEntity < Grape::Entity

    end
  end
end

userAPIのEntityファイルを作成

name, email, ageのみ表示するようにします。
(created_at、updated_atは表示させない)

app/api/entities/v1/user_entity.rb
module Entities
  module V1
    class UserEntity < RootEntity
      # name, email, ageのみ表示する
      expose :name, :email, :age
    end
  end
end

userAPIのファイルにEntity定義を追記

下記のようにすることで、entityの設定を反映することができます。

app/api/resources/v1/user.rb
module Resources
  module V1
    class Users < Grape::API
      resource :users do
        # /api/v1/user
        desc 'user list'
        get do
-         present User.all
+         present User.all, with: Entities::V1::UserEntity
        end

        # /api/v1/user/{:id}
        desc 'user'
        params do
          requires :id, type: Integer, desc: 'user id'
        end
        get ':id' do
-         present User.find(params[:id])
+         present User.find(params[:id]), with: Entities::V1::UserEntity
        end
      end
    end
  end
end

表示確認

http://localhost:3000/api/v1/users/
created_at、updated_atがなくなり、name, email, ageのみ表示されているはずです。

[

    {
        "name": "Sony",
        "email": "sony@sample.com",
        "age": 40
    },
    {
        "name": "Fredo",
        "email": "fred@sample.com",
        "age": 38
    },
    {
        "name": "Tom",
        "email": "tom@sample.com",
        "age": 35
    },
    {
        "name": "Michael",
        "email": "michael@sample.com",
        "age": 33
    },
    {
        "name": "Connie",
        "email": "connie@sample.com",
        "age": 30
    }

]

{

    "name": "Michael",
    "email": "michael@sample.com",
    "age": 33

}

次項: Grapeを使ってRESTfulAPIを簡単作成 その3 Grape::Swagger

参考

Grape::Entityを使用してAPIで取得できるプロパティを簡単に設定する

15
12
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
15
12