crud is not rest! hypermedia for y'all!

37
Roar ! Nick „The German“ Sutterer OMG, he's on the twitter, finally! @apotonick Hypermedia for y'all!

Upload: apotonick

Post on 01-Jul-2015

1.322 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: CRUD Is Not REST! Hypermedia For Y'All!

R o a r!

Nick „The German“ Sutterer

OMG, he's on the twitter, finally!@apotonick

Hypermediafor y'all!

Page 2: CRUD Is Not REST! Hypermedia For Y'All!

2

R o a r!

45 minutes...

REST

Hypermedia

ROAR

Page 3: CRUD Is Not REST! Hypermedia For Y'All!

3

R o a r!

„architectural style for distributed hypermedia systems“

Roy Fielding's dissertation, chapter 5, line 1

Page 4: CRUD Is Not REST! Hypermedia For Y'All!

4

R o a r!

require 'rest'

class Application include Restfulend

Page 5: CRUD Is Not REST! Hypermedia For Y'All!

5

R o a r!

GET http://nick/state

{„status“ : „tired“,„status-fr“: „fatigue“}

Page 6: CRUD Is Not REST! Hypermedia For Y'All!

R o a r!

Resource Unique URL

http://wines/red/1

UniformInterface GET

POSTPUT...

Represen-tation

Page 7: CRUD Is Not REST! Hypermedia For Y'All!

R o a r!

GET http://wines/red/1

{„color“: „red“, „alc“: 12.8, …}

Page 8: CRUD Is Not REST! Hypermedia For Y'All!

R o a r!

POSThttp://wines/

{„color“: „red“, „alc“: 12.8 }

http://wines/red/1

{„id“: 1, „color“: „red“, „alc“: 12.8,}

Page 9: CRUD Is Not REST! Hypermedia For Y'All!

R o a r!

GET http://wines/red/1Content-Type: app/wine+json

<body>

Page 10: CRUD Is Not REST! Hypermedia For Y'All!

R o a r!

JSON

Representations==

Documents

Page 11: CRUD Is Not REST! Hypermedia For Y'All!

11

R o a r!

GET: showPOST: createPUT: update...

REST is

CRUD is notREST!

Page 12: CRUD Is Not REST! Hypermedia For Y'All!

12

R o a r!

Hypermedia?

<a href=“http://wine.fr/buy“> Buy good wine!</a>

Page 13: CRUD Is Not REST! Hypermedia For Y'All!

R o a r!

<wine> <id>1</id> <color>red</color>

<link rel=“buy“ href=“http://wine/buy/1“ />

Page 14: CRUD Is Not REST! Hypermedia For Y'All!

14

R o a r!

Order

Article

User

Variant

Size

RoleClient

Address Shipping

Authenti-cation

Price

Page 15: CRUD Is Not REST! Hypermedia For Y'All!

15

R o a r!

Order

Article

User

Variant

Size

RoleClientAddress Shipping Authenti-cation

Price

Page 16: CRUD Is Not REST! Hypermedia For Y'All!

16

R o a r!

„Tab“ = Ordered drinks per Table

RESTaurant

Page 17: CRUD Is Not REST! Hypermedia For Y'All!

17

R o a r!

RESTaurant.tabs.add(table_3)

POST http://tabs

Page 18: CRUD Is Not REST! Hypermedia For Y'All!

18

R o a r!

{„tab“: { „table“: 3, „drinks“: [], ...}}

?

!

POST http://tabs{„tab“: {„table“: 3}}

REST client!

Page 19: CRUD Is Not REST! Hypermedia For Y'All!

19

R o a r!

{„tab“: { „id“: 42 „table“: 3, „drinks“: [], „links“: [ {„rel“ => „self“, „href“ => „http://tabs/42“},

{„rel“ => „order“, „href“ => „http://tabs/42/order/{name}“}

{„rel“ => „pay“, „href“ => „http://tabs/42/pay“} ]}}

Hyper

med

ia!

Page 20: CRUD Is Not REST! Hypermedia For Y'All!

20

R o a r!

{„rel“ => „pay“, „href“ => „http://tabs/42/pay“}

Page 21: CRUD Is Not REST! Hypermedia For Y'All!

21

R o a r!

<tab> <id>42</id> <table>3</table> <drinks />

<link rel=„self“ href=„http://tabs/42“ /> <link rel =„order“ href=„http://tabs/42/order/{name}“ />

<link rel=„pay“ href=„http://tabs/42/pay“ /></tab>

Page 22: CRUD Is Not REST! Hypermedia For Y'All!

22

R o a r!

{„tab“: { „id“: 42 „table“: 3, „drinks“: [], „links“: [ {„rel“ => „self“, „href“ => „http://tabs/42“},

{„rel“ => „order“, „href“ => „http://tabs/42/order/{name}“}

{„rel“ => „pay“, „href“ => „http://tabs/42/pay“} ]}}

Page 23: CRUD Is Not REST! Hypermedia For Y'All!

23

R o a r!

RESTaurant.tab(42).order(11)

1. look for rel=“order“2. extract href3. expand URL template4. POST

Page 24: CRUD Is Not REST! Hypermedia For Y'All!

24

R o a r!

{„tab“: { „id“: 42 „table“: 3, „drinks“: [], „links“: [ {„rel“ => „self“, „href“ => „http://tabs/42“},

{„rel“ => „order“, „href“ => „http://tabs/42/order/{name}“}

{„rel“ => „pay“, „href“ => „http://tabs/42/pay“} ]}}

Page 25: CRUD Is Not REST! Hypermedia For Y'All!

25

R o a r!

http://tabs/42/order/bordeaux

Page 26: CRUD Is Not REST! Hypermedia For Y'All!

26

R o a r!

?

!

POST http://tabs/42/order/bordeaux

{„tab“: { „id“: 42 „table“: 3, „drinks“: [bordeaux],

Page 27: CRUD Is Not REST! Hypermedia For Y'All!

27

R o a r!

http://tabs/42/order/bordeaux

NO URL computationNO guessingJust following links

Page 28: CRUD Is Not REST! Hypermedia For Y'All!

28

R o a r!

Code!

Page 29: CRUD Is Not REST! Hypermedia For Y'All!

29

R o a r!

{„tab“: { „table“: 3, „drinks“: [11], „links“: [ {„rel“: „self“,}}

?

!

GET http://tabs/42

Page 30: CRUD Is Not REST! Hypermedia For Y'All!

30

R o a r!

class TabRepresenter < Roar::Representer::JSON

property :id, :table collection :drinks

link :self do tab_url(represented) end link :order do order_url(represented) end ...

Page 31: CRUD Is Not REST! Hypermedia For Y'All!

31

R o a r!

TabRepresenter. from_model(@tab). serialize

# => {„tab“: { „id“: 42 „table“: 3, „drinks“: [11], „links“: [ {„rel“: „self“, „href“: „http://tabs/42“}...

Page 32: CRUD Is Not REST! Hypermedia For Y'All!

32

R o a r!

{„tab“: { „table“: 3, „drinks“: [11], „links“: [...

?

!

POST http://tabs{„tab“: {„table“: 3, „drinks“: 11}}

Page 33: CRUD Is Not REST! Hypermedia For Y'All!

33

R o a r!

TabRepresenter. deserialize(json_body). to_nested_attributes

# => {:table => 3, :drink_ids => [11]}

TableOrder.create(attrs)

Page 34: CRUD Is Not REST! Hypermedia For Y'All!

34

R o a r!

?

!

POST http://tabs/42/order/11

Page 35: CRUD Is Not REST! Hypermedia For Y'All!

35

R o a r!

tab = TabRepresenter. from(:table => 3). post(„http://tabs“)

Page 36: CRUD Is Not REST! Hypermedia For Y'All!

36

R o a r!

tab.links[:order]. # => „http://tabs/42/order/{id}

expand(:id => 11).

post!

Page 37: CRUD Is Not REST! Hypermedia For Y'All!

37

R o a r!

@apotonick

http://github.com/apotonick/roar