Upload
dmytro-shteflyuk
View
1.329
Download
0
Embed Size (px)
DESCRIPTION
Presentation covers following topics:- What is REST? - How do I use ActiveResource?
Citation preview
REST & ActiveResourceRyan Daigle
ALTERthought
1
Overview
• What is REST?
• How do I use ActiveResource?
2
REST
3
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
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
Proper Structure
DELETE http://addressbook/contacts/1
PUT http://addressbook/contacts/1
GET http://addressbook/contacts/1
6
HTTP Verbs
• 4 (Main) Request Methods
• POST
• *GET
• PUT
• DELETE
7
Verb Similarities
HTTP Method CRUD SQL Action
Post Create Insert Create
Get Read Select Show
Put Update Update Update
Delete Delete Delete Destroy
8
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
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
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
Non-CRUD Actions
• How invoke non create/show/update/destroy actions?
• Use Rails practices for consistent service addressing
12
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
ActiveResourceConsuming REST services
14
ActiveResource (ARes)
• ActiveRecord for REST
• O-O abstraction to remote services
15
ActiveResource
Network(Scary)
(Your) Model
Remote REST Service
16
Howto Implement ARes?
17
18
What You Get For Free
Finders
Create/update
Destroy
19
What ARes Does For You
• Form request URI
• Send RESTful request w/ XML body
• Process response
• O-O access to response details
20
‘Create’ Request
HTTP Request:
ARes:
21
Successful ‘Create’Http Response:
ARes Usage:
22
Http Response:
ARes Usage:
Errors on ‘Create’
23
ARes Not Tied to Rails
• Send/receive XML data
• Proper response codes & headers
24
Nested Resources
25
Nested Resources
• Nested != Inherited
• has_many relationships often represented w/ nesting
• Nesting specified via URIhttp://addressbook/users/1/contacts.xml
26
Nested ARes Models
Containing model:
Nested model:
uri prefix
27
Nested Model Initialization
• 2nd parameter hash is uri prefix (used to populate nesting entities in URI)
uri prefix parameters
28
Nested RequestsARes:
Nested HTTP Request:
nesting entity parameters
29
Nested URI Formation
30
Non-CRUD Scenarios
31
Non-CRUD Scenarios
• Access underlying connection
• Build custom URI
• e.g. ‘Register’ different from ‘Create’
32
What We WantARes:
Http Request:
33
How We Get It
34
35
Remember the Basics
• ARes builds URIs
• ARes sends and receives XML
36
Authentication
37
Basic HTTP Authentication
38
Token-Based Authentication
39
ARes Positives
• Very simple framework
• Not explicitly tied to Rails remote service
40
ARes Caveats
• Very raw
• No caching
• No ActiveRecord-like DSL
• Not officially part of Rails (just kind of hanging out in their repository)
41
Play Timehttp://contactsapi.com
42