Scaffolding, REST, and APIs

9th lecture, June 10, 2019

Projects in Information Technology II

http://www.sw.it.aoyama.ac.jp/2019/Projects2/lecture9.html

Martin J. Dürst

AGU

© 2019 Martin J. Dürst 青山学院大学

ミニテスト

 

Today's Schedule

 

MVC and Scaffolding

  

Scaffolding and CRUD

Scaffolding creates actions that correspond to the database CRUD operations:

controller action database operation SQL operation
create create insert
show read select
update update update
destroy delete delete

Scaffolding additionally creates the following actions:

  

Scaffolding and REST

HTTP methods controller action database operation
POST create create
GET show read
PUT (also PATCH) update update
DELETE destroy delete

  

HTTP: GET vs. POST

GET POST
Purpose obtain data send data
Data location in request URI (incl. query part) URI or body
Side effects (changes on the server)? no (except for logging) yes (create data in database, buy, pay,...)
Idempotent? yes (same result on repeated access) no (different result with multiple accessses)

  

HTTP: POST vs. PUT

PUT PATCH POST
Purpose replace item at address replace part of an item at address accept data at address
Response usually same address usually newly created address
Use in REST/Rails update create

  

Resource Routing

HTML method address controller action
GET /wrestlers wrestlers#index
POST /wrestlers wrestlers#create
GET /wrestlers/new wrestlers#new
GET /wrestlers/123/edit wrestlers#edit
GET /wrestlers/123 wrestlers#show
PATCH /wrestlers/123 wrestlers#update
PUT /wrestlers/123 wrestlers#update
DELETE /wrestlers/123 wrestlers#destroy

  

Example Flow for Update

  

Web APIs

  

XML and JSON

XML JSON
Full name Extensible Markup Language JavaScript Object Notation
Origin generalization of HTML extraction from JavaScript
Since 1998 early 2000s
Strengths document-like structures close match to programming language data structures
Problems mapping from/to programming language data structures handling of document-like structures
Pieces elements, attributes arrays, hashes (called objects), strings, numbers
Syntax example
<items>
  <item>
    <id>1</id>
    <title>Buy bread</title>
  </item>
</items>
[{"id": 1, "title": "Buy bread"}, {...}]
Ruby generating library builder jbuilder
Ruby parsing library rexml, nokogiri json (JSON.parse)

  

Resource vs. Representation

  

How to Use builder with Rails

  

How to Use Net::HTTP

  

Exercise 9a: Use Scaffold to Create a Todo Application

 

Exercise 9b: Create an XML API

 

Exercise 9c: Use the JSON API

 

Homework