rest & activeresource

42
REST & ActiveResource Ryan Daigle ALTERthought 1

Upload: dmytro-shteflyuk

Post on 10-Apr-2015

1.331 views

Category:

Documents


0 download

DESCRIPTION

Presentation covers following topics:- What is REST? - How do I use ActiveResource?

TRANSCRIPT

Page 1: REST & ActiveResource

REST & ActiveResourceRyan Daigle

ALTERthought

1

Page 2: REST & ActiveResource

Overview

• What is REST?

• How do I use ActiveResource?

2

Page 3: REST & ActiveResource

REST

3

Page 4: REST & ActiveResource

We Are Guilty of Poor Grammar

• Think of REST as a sentence

• HTTP Methods are verbs

• URIs are nouns

• This grammar is currently being abused

4

Page 5: REST & ActiveResource

Grammar Abused

• Action implied in the URI

• Implied action conflicts with HTTP method

Conflicting

GET http://addressbook/contacts/destroy/1

What’s wrong with this?

Embedded verb

5

Page 6: REST & ActiveResource

Proper Structure

DELETE http://addressbook/contacts/1

PUT http://addressbook/contacts/1

GET http://addressbook/contacts/1

6

Page 7: REST & ActiveResource

HTTP Verbs

• 4 (Main) Request Methods

• POST

• *GET

• PUT

• DELETE

7

Page 8: REST & ActiveResource

Verb Similarities

HTTP Method CRUD SQL Action

Post Create Insert Create

Get Read Select Show

Put Update Update Update

Delete Delete Delete Destroy

8

Page 9: REST & ActiveResource

Nouns

• Represent Resources with URIs (Uniform Resource Identifier)

• ‘contacts’ = http://addressbook/contacts

• Different representations of the same resource (html, xml, json)

• contacts.html, contacts.xml etc...

9

Page 10: REST & ActiveResource

Sentences

“View the XML representation of all contacts”GET http://addressbook/contacts.xml

“Get the vCard representation of the contact with id 1”GET http://addressbook/contacts/1.vcd

“Create a new contact”POST http://addressbook/contacts.xml

“Update the contact with id 1”PUT http://addressbook/contacts/1

10

Page 11: REST & ActiveResource

Surely This Can’t Be It?

• REST’s simplicity is not a limitation

• Think in terms of properly structured sentences

• Build upon this foundation

11

Page 12: REST & ActiveResource

Non-CRUD Actions

• How invoke non create/show/update/destroy actions?

• Use Rails practices for consistent service addressing

12

Page 13: REST & ActiveResource

Example“Get the XML representation of all contacts whose first name starts with the letter ‘r’ ”

GET http://addressbook/contacts.xml

Action name

;search

Query parameters

?first=r

13

Page 14: REST & ActiveResource

ActiveResourceConsuming REST services

14

Page 15: REST & ActiveResource

ActiveResource (ARes)

• ActiveRecord for REST

• O-O abstraction to remote services

15

Page 16: REST & ActiveResource

ActiveResource

Network(Scary)

(Your) Model

Remote REST Service

16

Page 17: REST & ActiveResource

Howto Implement ARes?

17

Page 18: REST & ActiveResource

18

Page 19: REST & ActiveResource

What You Get For Free

Finders

Create/update

Destroy

19

Page 20: REST & ActiveResource

What ARes Does For You

• Form request URI

• Send RESTful request w/ XML body

• Process response

• O-O access to response details

20

Page 21: REST & ActiveResource

‘Create’ Request

HTTP Request:

ARes:

21

Page 22: REST & ActiveResource

Successful ‘Create’Http Response:

ARes Usage:

22

Page 23: REST & ActiveResource

Http Response:

ARes Usage:

Errors on ‘Create’

23

Page 24: REST & ActiveResource

ARes Not Tied to Rails

• Send/receive XML data

• Proper response codes & headers

24

Page 25: REST & ActiveResource

Nested Resources

25

Page 26: REST & ActiveResource

Nested Resources

• Nested != Inherited

• has_many relationships often represented w/ nesting

• Nesting specified via URIhttp://addressbook/users/1/contacts.xml

26

Page 27: REST & ActiveResource

Nested ARes Models

Containing model:

Nested model:

uri prefix

27

Page 28: REST & ActiveResource

Nested Model Initialization

• 2nd parameter hash is uri prefix (used to populate nesting entities in URI)

uri prefix parameters

28

Page 29: REST & ActiveResource

Nested RequestsARes:

Nested HTTP Request:

nesting entity parameters

29

Page 30: REST & ActiveResource

Nested URI Formation

30

Page 31: REST & ActiveResource

Non-CRUD Scenarios

31

Page 32: REST & ActiveResource

Non-CRUD Scenarios

• Access underlying connection

• Build custom URI

• e.g. ‘Register’ different from ‘Create’

32

Page 33: REST & ActiveResource

What We WantARes:

Http Request:

33

Page 34: REST & ActiveResource

How We Get It

34

Page 35: REST & ActiveResource

35

Page 36: REST & ActiveResource

Remember the Basics

• ARes builds URIs

• ARes sends and receives XML

36

Page 37: REST & ActiveResource

Authentication

37

Page 38: REST & ActiveResource

Basic HTTP Authentication

38

Page 39: REST & ActiveResource

Token-Based Authentication

39

Page 40: REST & ActiveResource

ARes Positives

• Very simple framework

• Not explicitly tied to Rails remote service

40

Page 41: REST & ActiveResource

ARes Caveats

• Very raw

• No caching

• No ActiveRecord-like DSL

• Not officially part of Rails (just kind of hanging out in their repository)

41

Page 42: REST & ActiveResource

Play Timehttp://contactsapi.com

42