Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ActiveModel::Attributes APIとSwaggerでRailsのAPIレ...

takanamito
November 08, 2018

ActiveModel::Attributes APIとSwaggerでRailsのAPIレスポンスに型を持ち込む

表参道.rb #40 〜Railsアップグレード〜 - connpass
https://omotesandorb.connpass.com/event/105365/

takanamito

November 08, 2018
Tweet

More Decks by takanamito

Other Decks in Programming

Transcript

  1. Copyright © ZOZO Technologies, Inc. All Right Reserved. גࣜձࣾ;0;0ςΫϊϩδʔζ ։ൃ෦!UBLBOBNJUP

    "DUJWF.PEFM"UUSJCVUFT"1*ͱ 4XBHHFSͰ3BJMTͷ"1*Ϩεϙϯεʹ ܕΛ࣋ͪࠐΉ
  2. Copyright © ZOZO Technologies, Inc. All Right Reserved. ղܾࡦ w

    4XBHHFS 0QFO"1* Ͱ
 ϨεϙϯεͷεΩʔϚ ܕ Λఆٛ w εΩʔϚΛදݱ͢ΔΫϥεΛ࡞੒ "DUJWF.PEFM"UUSJCVUFT"1* 
 3BJMTͷ৽ػೳ w εΩʔϚ͔Β+40/γϦΞϥΠβΛࣗಈੜ੒
  3. Copyright © ZOZO Technologies, Inc. All Right Reserved. "1*ϨεϙϯεͷεΩʔϚ TXBHHFSZBNM

    υϝΠϯϞσϧ "DUJWF.PEFM"UUSJCVUFT"1* "DUJWF.PEFM4FSJBMJ[FS ࣗಈੜ੒ͨ͠γϦΞϥΠβͰ
 υϝΠϯϞσϧΛγϦΞϥΠζ͢Δ͜ͱͰ εΩʔϚఆٛ௨ΓͷϨεϙϯεΛอূ͍ͨ͠ ࣗಈੜ੒ γϦΞϥΠζ +40/
  4. Copyright © ZOZO Technologies, Inc. All Right Reserved. ղܾࡦ w

    4XBHHFS 0QFO"1* Ͱ
 ϨεϙϯεͷεΩʔϚ ܕ Λఆٛ w εΩʔϚΛදݱ͢ΔΫϥεΛ࡞੒ "DUJWF.PEFM"UUSJCVUFT"1*  w εΩʔϚ͔Β+40/γϦΞϥΠβΛࣗಈੜ੒
  5. Copyright © ZOZO Technologies, Inc. All Right Reserved. εΩʔϚఆٛ %#ʹVTFST

    UXFFUTςʔϒϧ QSPpMFܕͷϨεϙϯεΛฦ͢ΤϯυϙΠϯτ # swagger.yaml components: schemas: profile: type: object properties: user_name: type: string tweet: type: string
  6. Copyright © ZOZO Technologies, Inc. All Right Reserved. ղܾࡦ w

    4XBHHFS 0QFO"1* Ͱ
 ϨεϙϯεͷεΩʔϚΛఆٛ w εΩʔϚΛදݱ͢ΔΫϥεΛ࡞੒ "DUJWF.PEFM"UUSJCVUFT"1*  w εΩʔϚ͔Β+40/γϦΞϥΠβΛࣗಈੜ੒
  7. Copyright © ZOZO Technologies, Inc. All Right Reserved. 1SPGJMFܕͷυϝΠϯϞσϧ "DUJWF.PEFM"UUSJCVUFT"1*Λ࢖ͬͯBUUSJCVUFͷܕΛఆٛ


    υϝΠϯϩδοΫ΋͜ͷΫϥεʹ class Profile < ActiveModelSerializers::Model include ActiveModel::Model include ActiveModel::Attributes attribute :user_name, :string attribute :tweet, :string, default: -> { 'πΠʔτͳ͠' } end
  8. Copyright © ZOZO Technologies, Inc. All Right Reserved. ղܾࡦ w

    4XBHHFS 0QFO"1* Ͱ
 ϨεϙϯεͷεΩʔϚΛఆٛ w εΩʔϚΛදݱ͢ΔΫϥεΛ࡞੒ "DUJWF.PEFM"UUSJCVUFT"1*  w εΩʔϚ͔Β+40/γϦΞϥΠβΛࣗಈੜ੒
  9. Copyright © ZOZO Technologies, Inc. All Right Reserved. γϦΞϥΠβ ࣗ࡞HFNPQFOBQJSVCZͰεΩʔϚఆ͔ٛΒࣗಈੜ੒

    class ProfileSerializer < ActiveModel::Serializer attributes :user_name, :tweet def user_name type_check(:user_name, [String]) object.user_name end def tweet type_check(:tweet, [String]) object.tweet end private def type_check(name, types) raise "Field type is invalid. #{name}" unless types.include?(object.send(name).class) end end
  10. Copyright © ZOZO Technologies, Inc. All Right Reserved. $POUSPMMFS 0QFO"1*Ͱఆٛͨ͠εΩʔϚͲ͓Γͷ

    Ϩεϙϯε͕ฦͤΔ class ProfilesController < ApplicationController def index ~ লུ ~ profile = Profile.new(user_name: xxx, tweet: xxx) render json: profile end end => {"user_name":"takanamito","tweet":"ΦϑΟεݟֶͥͻདྷ͍ͯͩ͘͞"}
  11. Copyright © ZOZO Technologies, Inc. All Right Reserved. Ͱ΋3BJMT͡Όͳ͍͠ class

    Profile include ActiveModel::Model # Rails 5.2 include ActiveModel::Attributes # Rails 5.1 include ActiveModelAttributes ... ɾҎԼͰ࢖͍͍ͨ৔߹͸
 IUUQTHJUIVCDPNBMQBDBUDBDUJWF@NPEFM@BUUSJCVUFT@CBDLQPSU