Slide 1

Slide 1 text

Alex Coles Putting the Hype back in Hypermedia – Practical REST with Roar Mittwoch, 6. Juni 12

Slide 2

Slide 2 text

About me Mittwoch, 6. Juni 12

Slide 3

Slide 3 text

@myabc • @myabc • github.com/myabc • alexbcoles.com Mittwoch, 6. Juni 12

Slide 4

Slide 4 text

Where I live • Berlin Mittwoch, 6. Juni 12

Slide 5

Slide 5 text

Where I work • Payango GmbH Mittwoch, 6. Juni 12

Slide 6

Slide 6 text

‘Spare’ Time • Open-source. Check github.com/myabc • Organise eurucamp.org (August 17 - 19). • Also helping out with Rails Girls. Mittwoch, 6. Juni 12

Slide 7

Slide 7 text

Standing in for @apotonick Mittwoch, 6. Juni 12

Slide 8

Slide 8 text

What’s ROAR? Mittwoch, 6. Juni 12

Slide 9

Slide 9 text

Resource-Oriented Architectures in Ruby Mittwoch, 6. Juni 12

Slide 10

Slide 10 text

Representations Mittwoch, 6. Juni 12

Slide 11

Slide 11 text

Let’s detour… REST Mittwoch, 6. Juni 12

Slide 12

Slide 12 text

REpresentational State Transfer Mittwoch, 6. Juni 12

Slide 13

Slide 13 text

Look at academic research for REST Mittwoch, 6. Juni 12

Slide 14

Slide 14 text

Roy Fielding Mittwoch, 6. Juni 12

Slide 15

Slide 15 text

REST vs SOAP Mittwoch, 6. Juni 12

Slide 16

Slide 16 text

Mittwoch, 6. Juni 12

Slide 17

Slide 17 text

‘You’re doing it wrong’ Mittwoch, 6. Juni 12

Slide 18

Slide 18 text

REST vs Rails REST Mittwoch, 6. Juni 12

Slide 19

Slide 19 text

One thing you shouldn’t be too concerned about. Mittwoch, 6. Juni 12

Slide 20

Slide 20 text

URLs Mittwoch, 6. Juni 12

Slide 21

Slide 21 text

GET http://mysite.com/ search/digital-books Mittwoch, 6. Juni 12

Slide 22

Slide 22 text

GET http://mysite.com/ search/digital-books RESTful! Mittwoch, 6. Juni 12

Slide 23

Slide 23 text

GET http://mysite.com/ search.aspx?query=digital- books¶meter=2 Mittwoch, 6. Juni 12

Slide 24

Slide 24 text

GET http://mysite.com/ search.aspx?query=digital- books¶meter=2 RESTful! Mittwoch, 6. Juni 12

Slide 25

Slide 25 text

Not XML-RPC Mittwoch, 6. Juni 12

Slide 26

Slide 26 text

Read more: http://blog.steveklabnik.com/ posts/2011-07-03-nobody- understands-rest-or-http Mittwoch, 6. Juni 12

Slide 27

Slide 27 text

The ROAR library Mittwoch, 6. Juni 12

Slide 28

Slide 28 text

ROAR consists of the following roar-rails roar representers Mittwoch, 6. Juni 12

Slide 29

Slide 29 text

Bi-directional Serialization - Deserialization #to_json #from_json #to_xml #from_xml Mittwoch, 6. Juni 12

Slide 30

Slide 30 text

Client Side Bi-directional Serialization - Deserialization #to_json #from_json #to_xml #from_xml Mittwoch, 6. Juni 12

Slide 31

Slide 31 text

Client Side Server Side Bi-directional Serialization - Deserialization #to_json #from_json #to_xml #from_xml Mittwoch, 6. Juni 12

Slide 32

Slide 32 text

Allows multiple representations Mittwoch, 6. Juni 12

Slide 33

Slide 33 text

Hypertext Application Language (HAL) Mittwoch, 6. Juni 12

Slide 34

Slide 34 text

Application as state machine orders orders/1 orders/1/ update Mittwoch, 6. Juni 12

Slide 35

Slide 35 text

class Project < ActiveRecord::Base; end Representing models Mittwoch, 6. Juni 12

Slide 36

Slide 36 text

The Representer module ProjectRepresenter include Roar::Representer::JSON property :id property :name property :date_published property :author_name end Mittwoch, 6. Juni 12

Slide 37

Slide 37 text

The Representer module ProjectRepresenter include Roar::Representer::JSON property :id property :name property :date_published property :author, class: User, extend: UserRepresenter, embedded: true end Mittwoch, 6. Juni 12

Slide 38

Slide 38 text

The Representer module ProjectRepresenter include Roar::Representer::JSON property :id property :name property :date_published property :author, class: User, extend: UserRepresenter, embedded: true collection :tickets, class: Ticket, extend: TicketRepresenter, embedded: true end Mittwoch, 6. Juni 12

Slide 39

Slide 39 text

Pick your features • Hypertext Application Language (HAL) • Coersion, using Virtus • HTTP Support, using Net::HTTP or Faraday Mittwoch, 6. Juni 12

Slide 40

Slide 40 text

Roar::Representer::JSON:: HAL module ProjectRepresenter include Roar::Representer::JSON include Roar::Representer::JSON::HAL property :id property :title property :date_published property :author collection :tickets, class: Ticket, extend: TicketRepresenter, embedded: true link :self do "http://projects/#{id}" end end Mittwoch, 6. Juni 12

Slide 41

Slide 41 text

Roar::Representer::Feature:: Coersion module ProjectRepresenter include Roar::Representer::JSON include Roar::Representer::Feature::Coersion property :id, type: Numeric property :name, type: String, default: ‘Untitled Project’ property :date_published, type: DateTime, default: ->{ DateTime.now } property :author_name, type: String, default: default_author_name def default_author_name “An unknown author from #{DateTime.now.year}” end end Mittwoch, 6. Juni 12

Slide 42

Slide 42 text

Roar::Representer::Feature:: HttpVerbs module ProjectRepresenter include Roar::Representer::JSON include Roar::Representer::Feature::HttpVerbs property :id end Project.get(‘http://projects/1/’) #=> Project.new.extend(ProjectRepresenter).post(‘’) #=> Mittwoch, 6. Juni 12

Slide 43

Slide 43 text

Roar::Representer::Feature:: HttpVerbs require ‘faraday’ Roar::Representer::Feature::HttpVerbs.transport_engine = ::Roar::Representer::Transport::NetHTTP ::Roar::Representer::Transport::Faraday Mittwoch, 6. Juni 12

Slide 44

Slide 44 text

ROAR Rails • gives some nice integration with Responders (https://github.com/ plataformatec/responders) •respond_with project Mittwoch, 6. Juni 12

Slide 45

Slide 45 text

How I’ve been using ROAR • An architecture consisting of multiple projects. • One project is an API gem • defines Representers on both sides – client and server-side, as an API ‘contract’. Mittwoch, 6. Juni 12

Slide 46

Slide 46 text

What’s to come… • Better HTTP Support: • Building support using Faraday. Mittwoch, 6. Juni 12

Slide 47

Slide 47 text

More information • https://github.com/apotonick/roar • https://github.com/solnic/virtus • anything by Steve Klabnik: http://blog.steveklabnik.com/ Mittwoch, 6. Juni 12

Slide 48

Slide 48 text

@myabc • @myabc • github.com/myabc • alexbcoles.com Mittwoch, 6. Juni 12