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

Rails and JSON API in 2019

Rails and JSON API in 2019

Akinori MUSHA

May 07, 2019
Tweet

More Decks by Akinori MUSHA

Other Decks in Technology

Transcript

  1. RESTͱ͸ • 2000೥ɺApache HTTP ServerϓϩδΣΫτͷcofounderͰHTTP/1.1ͷࡦఆʹ ΋ؔΘͬͨRoy Fielding͕ത࢜߸࿦จͰఏএ • REpresentational State

    Transferͷ಄ࣈޠ • REpresentational State = දݱՄೳͳঢ়ଶ = ͍ΘΏΔϦιʔε • Transfer = సૹ • HTTPʹ԰্԰ΛՍͨ͠ϝλRPCϓϩτίϧʢSOAP΍XMLRPCʣɺHTTPΛ୯ ͳΔτϥϯεϙʔτɾτϯωϧͱͯ͠࢖͖ͬͯͨ༻๏ʹର͢ΔΞϯνςʔθ • HTTPݪཧओٛ
  2. JSONͱ͸ • 2001೥ɺDouglas Crockford͕WebϕʔεͷΞϓϦέʔγϣϯΛ։ൃ͢Δࡍʹʮൃݟʯͨ͠σʔλަ׵ϑΥʔϚοτ • The JSON Saga ▶ •

    ୯ͳΔJavaScriptͷΦϒδΣΫτϦςϥϧͰ͸HTMLυΩϡϝϯτʹຒΊࠐΉࡍʹ·͍ͣͱؾ͍ͮͨ • ΩʔΛඞͣΫΦʔτ͢Δ • ES3Ͱ͸༧໿ޠΛΩʔʹͰ͖ͳ͔ͬͨ(ES5ͰՄೳʹͳͬͨ) • Pythonͷdictionaryͷه๏ͱ΋Ұக͍ͯͨ͠ • eval("{a:42}")ͯ͠ΈΔͱ෼͔Δ͕ɺϒϩοΫ಺ʹϥϕϧa:ͱࣜ42͕͋Δͱղऍ͞Εͯ͠·͏ɺͱ͍͏ཧ༝΋͋Δ • จࣈྻϦςϥϧʹ৑௕ͳ \ Λڐ͢ • </script> Λ <\/script> ͱ͢ΔͳͲͯ͠ແޮԽͰ͖ΔΑ͏ʹ • evalͰ͖Δͱ͍͏ख͔ܰ͞Β࢝·͕ͬͨɺ҆શΛ֬ೝੑ͢Δͷ͕େม • ઐ༻ύʔαʔ͕ඞཁ(JSON.parse)
  3. RESTͱ͸ • 4ͭͷݪଇ • Addressability ͢΂ͯͷૢ࡞ର৅͸URIʢϢχόʔαϧʹϢχʔΫͳ໊લʣͰࣔ͞ΕΔ • Statelessness ηογϣϯ౳ͷঢ়ଶ؅ཧ͸ͤͣɺݸʑͷૢ࡞͕ಠཱ͍ͯ͠Δ •

    Connectedness ৘ใͷؔ࿈͸URIͷϦϯΫͰࣔ͢ • Uniform Interface HTTPϝιουΛڞ௨ޠኮͱ͠ɺ͋ΒΏΔૢ࡞ΛGET, POST, PUT, DELETE౳Ͱද͢
  4. OpenAPI Specification • 2017೥ʹग़ͨɺRESTful APIͷ࢓༷Λهड़͢ΔͨΊͷݴޠ ʢJSON|YAMLʣ • https://www.openapis.org/ • Swagger

    Specification (ʙ2.0)ͷޙܧ • Swagger͸RESTful APIΛߏங͢ΔͨΊͷϑϨʔϜϫʔΫɾ πʔϧʢ2011೥ʙʣ
  5. OpenAPI Specification (3.0) • ཤྺ • 2018-10-08 OpenAPI Specification 3.0.2

    • 2017-12-06 OpenAPI Specification 3.0.1 • 2017-07-26 OpenAPI Specification 3.0.0 • 2016-01-01 OpenAPI Initiativeൃ଍ • 2014-09-08 Swagger Specification 2.0 • 2014-03-14 Swagger Specification 1.2 • 2012-08-22 Swagger Specification 1.1 • 2011-08-10 Swagger Specification 1.0
  6. OpenAPI Specification (3.0) • ΤϯυϙΠϯτͱཁٻɾԠ౴ͷσʔλεΩʔϚͳͲΛแׅతʹهड़Ͱ͖Δ • JSON:APIɺHALͱ͸௚ߦͨ֓͠೦ • JSON SchemaΛऔΓࠐΜͰεΩʔϚఆٛݴޠͱͯ͠ར༻͍ͯ͠Δ

    • ׬શʹಉظɾ௥ਵ͍ͯ͠ΔΘ͚Ͱ͸ͳ͍(JSON Schema͸௕͖ʹ౉ΓυϥϑτΛॏͶ ͍ͯͯɺख໭Γ΋ଟ͍) • JSON Hyper Schemaͱ͸গ͔͠Ϳ͍ͬͯΔ • OpenAPIࣗ਎͕ಠࣗʹΤϯυϙΠϯτ৘ใͷهड़ݴޠΛ͍࣋ͬͯΔ • OpenAPI͸ΫΤϦʔύϥϝʔλ΍ύε಺ύϥϝʔλʹ͍ͭͯ΋bodyಉ༷ʹ౷Ұతͳ จ๏Ͱهड़Ͱ͖ΔΑ͏ʹਐԽ
  7. JSON Schema • JSONσʔλͷεΩʔϚͷهड़ݴޠ https://json-schema.org/ • ͦΕࣗମ͕JSONͰهड़͞ΕΔ • ҎԼͷ࢓༷ͷ૯শ •

    JSON Schema Core • JSON Schema Validation • JSON Hyper-Schema • Relative JSON Pointers • ཤྺ • 2018-03-19 draft-07
  8. JSON Schema • JSON Schema Core • JSON Schemaͷج൫࢓༷ εΩʔϚόʔδϣϯͷએݴɺωετɺ಺෦ɾ֎෦ࢀরͳͲʹ͍ͭͯنఆ͞Ε͍ͯΔ

    $schema, $ref, $idͳͲ ͦͷଞͷ۩ମతͳޠኮ͸͜͜Ͱ͸نఆ͞Ε͍ͯͳ͍ • JSON Schema Validation • JSON Schemaͷ࢓༷ͷຊମ σʔλܕɺόϦσʔγϣϯϧʔϧΛهड़͢ΔͨΊͷޠኮ͕ఆٛ͞Ε͍ͯΔ ೔෇ɺ࣌ࠁɺϝʔϧΞυϨεɺURIͳͲ΋σʔλܕͱͯ͠ఆٛ͞Ε͍ͯΔ(JSONදݱࣗମ͸String)
  9. JSON Schema • JSON Hyper-Schema • ϋΠύʔϝσΟΞ(ϦϯΫ)Λهड़͢ΔͨΊͷޠኮ͕ఆٛ͞Ε ͍ͯΔ • Relative

    JSON Pointers • JSONυΩϡϝϯτ಺ͷಛఆͷΩʔΛࢀর͢ΔͨΊͷه๏ $refͰ # Ҏ߱ʹهड़͢ΔΞϯΧʔͰ࢖ΘΕΔ
  10. JSONγϦΞϥΠβʔͷൺֱ • jbuilder • DSL͕͍·͍ͪ • ଎͘΋ͳ͍ • ActiveModel::Serializers •

    ྑ͘΋ѱ͘΋σϑΝΫτελϯμʔυ • ϓϩδΣΫτ͕ಈ͍͍ͯͳ͍ • 0.10.xͰ೩͑ਚ͖ͨʁ
  11. JSONγϦΞϥΠβʔͷൺֱ • fast_jsonapi • Netflix੡ɺ଎ͯ͘Αͦ͞͏ • JSON:APIʹدͤͨσϑΥϧτ • AMSʹ͍ۙ࢖͍৺஍ •

    ొ৔͸৽͍͕͠ɺ͞΄Ͳ։ൃ͕׆ൃͱ͍͏Θ͚Ͱ͸ͳ͍ • rabl • Ϟσϧʹର͢ΔserializerɺͰ͸ͳ͘ϏϡʔςϯϓϨʔτɺίϯϙʔωϯτܕ • චऀ͕࢖͍׳Ε͍ͯΔ
  12. Rabl • RubyʹΑΔDSLͱ͍͏ͷ͸ଞͱಉ͡ • RailsͷϏϡʔͱͯ͠ಈ͘ • JSONΛੜ੒͢ΔςϯϓϨʔτ (*.json.rabl) • ଞͷγϦΞϥΠβʔ͸جຊతʹϞσϧͱJSONදݱ͕1:1͕ͩɺRabl͸ίϯςΩ

    ετʹԠͯࣗ͡༝ʹςϯϓϨʔτΛબͼɺదٓpartialͰऔΓࠐΊΔ • OpenAPIͷcomponentsͰϞσϧΛఆٛͯ͠$refͰࢀরɺͱ͍͏Ϟσϧͱ ૬ੑ΋ྑ͍͸ͣ • rabl-railsΛ࢖͏ͱ݁ߏ଎͍
  13. ࣮ྫ • committeeͱrablͰ࡞ͬͯΈ·ͨ͠ • https://github.com/knu/jsonapi-example • Ԡ౴όϦσʔγϣϯ • ։ൃ؀ڥͷΈͰ༗ޮԽ •

    ςετ؀ڥͰ͸request specͰ໌ࣔతʹassertionΛೖΕΔ ༗ޮԽͯ͠͠·͏ͱHTTPεςʔλεͷ࣌఺Ͱ͚ͯ͜͠·ͬͯ௥͍ͮΒ͍ • ϓϩμΫγϣϯ؀ڥͰ͸ςετΛ௨͍ͬͯΔ͸ͣͳͷͰ৑௕ɺ࿙Εͷ෦෼ͰϋʔυΤϥʔʹͳͬͯ΋ࠔΔ ݕ஌͚ͩͰ͖Δػೳ ͕४උதͳͷͰɺ࣮૷͞ΕͨΒ࢖͏ͱΑͦ͞͏ • JSONϏϡʔ͸ΨϫlayoutɺΤϯυϙΠϯτͷςϯϓϨʔτɺcomponentʹରԠͨ͠partialɺͱOpenAPIఆٛ ʹԊͬͯ࡞੒ • ࡉ͔͍ͱ͜ΖͰ͸ɺJSON:APIͷMIME type (application/vnd.api+json)Λ࢖͏Α͏ʹͨ͠