Slide 1

Slide 1 text

Fast, testable and SANE APIs Ben Lovell

Slide 2

Slide 2 text

_benlovell

Slide 3

Slide 3 text

benlovell j

Slide 4

Slide 4 text

113581334398839860922

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

“I HAVE KILLED, AND I WILL KILL AGAIN”

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

no caption

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Building JSON APIs awesome!

Slide 28

Slide 28 text

Rails-API warning: there may be ranting

Slide 29

Slide 29 text

JSON API your tool against bikeshedding

Slide 30

Slide 30 text

ActiveModel::Serializers because #to_json kinda sucks

Slide 31

Slide 31 text

HTTP/REST you’re doing it wrong (maybe)

Slide 32

Slide 32 text

/etc too meta to categorise

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Components of good API design

Slide 35

Slide 35 text

FAST STANDARDISED INTUITIVE

Slide 36

Slide 36 text

FAST STANDARDISED INTUITIVE

Slide 37

Slide 37 text

FAST STANDARDISED INTUITIVE

Slide 38

Slide 38 text

JSON API

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

application/vnd.api+json IANA registered

Slide 42

Slide 42 text

W IP! http://git.io/Uh8Y8w

Slide 43

Slide 43 text

{ "links": { "posts.author": { "href": "http://example.com/people/{posts.author}", "type": "people" }, "posts.comments": { "href": "http://example.com/comments/{posts.comments}", "type": "comments" } }, "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "author": "9", "comments": [ "5", "12", "17", "20" ] } }] }

Slide 44

Slide 44 text

{ "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "author": "9", "comments": [ "5", "12", "17", "20" ] } }] }

Slide 45

Slide 45 text

{ "posts": [{ "id": 1 // a post document }] } { }, { }] } Singular Resource Resource Collection

Slide 46

Slide 46 text

{ }] } { "posts": [{ "id": 1 // a post document }, { "id": 2 // a post document }] } Singular Resource Resource Collection

Slide 47

Slide 47 text

{ "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "comments": [ "5", "12", "17", "20" ] } }] } To-Many Relationships

Slide 48

Slide 48 text

{ "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "comments": { "href": "http://example.com/comments/5,12,17", "ids": [ "5", "12", “17" ], "type": "comments" } } }] } To-Many Relationships (link style)

Slide 49

Slide 49 text

{ "links": { "posts.comments": "http://example.com/posts/{posts.id}/comments" }, "posts": [{ "id": "1", "title": "Rails is Omakase" }, { "id": "2", "title": "The Parley Letter" }] } Shorthand (link style) RFC6570 URI Template

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

/etc I did warn you

Slide 55

Slide 55 text

COMPOUND DOCUMENTS the fastest HTTP is no HTTP

Slide 56

Slide 56 text

GET /posts/1?include=comments GET /posts/1?include=comments.author GET /posts/1?include=author,comments

Slide 57

Slide 57 text

{ "links": { "posts.author": { "href": "http://example.com/people/{posts.author}", "type": "people" }, "posts.comments": { "href": "http://example.com/comments/{posts.comments}", "type": "comments" } }, "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "author": "9", "comments": [ “1" ] }}], "linked": { "authors": [{ "id": "9", "name": "@dhh" }], "comments": [{ "id": "1", "body": "Mmmmmakase" }] } }

Slide 58

Slide 58 text

SPARSE FIELDSETS

Slide 59

Slide 59 text

GET /posts/1?fields=id,title,body

Slide 60

Slide 60 text

HTTP SEMANTICS

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

wat

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

RAILS-API

Slide 69

Slide 69 text

❤️

Slide 70

Slide 70 text

Ruby is not as slow as you might think…

Slide 71

Slide 71 text

Ruby is not as slow as you might think… …IT’S SLOWER

Slide 72

Slide 72 text

So, how do we make Ruby fast?

Slide 73

Slide 73 text

By running less Ruby. DERP! So, how do we make Ruby fast?

Slide 74

Slide 74 text

So, how do we make Rails fast?

Slide 75

Slide 75 text

So, how do we make Rails fast? By running less Rails. DERP!

Slide 76

Slide 76 text

YO DAWG! I heard you like rails so I took some rails out of your rails so you could rails (a little bit faster than usual)

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

http://goo.gl/qfJG2d

Slide 80

Slide 80 text

use Rack::Sendfile use ActionDispatch::Static use Rack::Lock use # use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use Rack::Head use Rack::ConditionalGet use Rack::ETag run KitchenSinkFullOfKitchenSinks::Application.routes

Slide 81

Slide 81 text

E.B.T.K.S

Slide 82

Slide 82 text

use Rack::Sendfile use ActionDispatch::Static use Rack::Lock use # use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use Rack::Head use Rack::ConditionalGet use Rack::ETag run KitchenSink::Application.routes

Slide 83

Slide 83 text

A.B.T.K.S

Slide 84

Slide 84 text

require “rails/all”

Slide 85

Slide 85 text

require “rails/all”

Slide 86

Slide 86 text

irb(main):003:0> pp ActionController::API.ancestors - ActionController::Metal.ancestors [ ActionController::API, ActiveRecord::Railties::ControllerRuntime, ActionDispatch::Routing::RouteSet::MountedHelpers, ActionController::StrongParameters, ActionController::Instrumentation, ActionController::Rescue, ActiveSupport::Rescuable, ActionController::DataStreaming, ActionController::ForceSSL, AbstractController::Callbacks, ActiveSupport::Callbacks, ActionController::ConditionalGet, ActionController::Head, ActionController::Renderers::All, ActionController::Renderers, ActionController::Rendering, AbstractController::Rendering, AbstractController::ViewPaths, ActionController::Redirecting, ActionController::RackDelegation, ActiveSupport::Benchmarkable, AbstractController::Logger, ActionController::UrlFor, AbstractController::UrlFor, ActionDispatch::Routing::UrlFor, ActionDispatch::Routing::PolymorphicRoutes, ActionController::ModelNaming, ActionController::HideActions ]

Slide 87

Slide 87 text

NO CAPTION

Slide 88

Slide 88 text

Y U NO Sinatra!!!

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

HEY LADIES

Slide 94

Slide 94 text

❤️

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

Y U NO Sinatra!!! “Any sufficiently advanced sinatra application is indistinguishable from rails”

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

don’t reinvent this

Slide 100

Slide 100 text

ActiveModel Serializers

Slide 101

Slide 101 text

Jbuilder.encode do |json| json.content format_content(@message.content) json.(@message, :created_at, :updated_at) ! json.author do json.name @message.creator.name.familiar json.email_address @message.creator.email_address_with_name json.url url_for(@message.creator, format: :json) end ! if current_user.admin? json.visitors calculate_visitors(@message) end ! json.comments @message.comments, :content, :created_at ! json.attachments @message.attachments do |attachment| json.filename attachment.filename json.url url_for(attachment) end end

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

DHH CHANGES NAME TO BASECAMP

Slide 104

Slide 104 text

Jbuilder json json ! json json json json end ! if json end ! json ! json json json end end

Slide 105

Slide 105 text

% rails g serializer …

Slide 106

Slide 106 text

class PostSerializer < ActiveModel::Serializer attributes :id, :title, :body has_many :comments end

Slide 107

Slide 107 text

class PostSerializer < ActiveModel::Serializer attributes :id, :title, :body, :synopsis has_many :comments ! def comments object.comments.where(:author => scope) end ! def synopsis object.body.truncate(30) end end

Slide 108

Slide 108 text

class PostsController < ApplicationController def show @post = Post.find(params[:id]) respond_with @post end end

Slide 109

Slide 109 text

CONVENTION!

Slide 110

Slide 110 text

VIEWS

Slide 111

Slide 111 text

class PostSerializer < ActiveModel::Serializer attributes :id, :title, :body has_many :comments has_many :tags has_many :images belongs_to :author end WARNING!

Slide 112

Slide 112 text

class attributes has_many has_many has_many belongs_to end CONGRATULATIONS!!! YOU SERIALIZED YOUR DATABASE

Slide 113

Slide 113 text

GET, BUT GET CONDITIONALLY

Slide 114

Slide 114 text

class PostsController < ApplicationController def show @post = Post.find(params[:id]) ! if stale? @post respond_with @post end end end

Slide 115

Slide 115 text

class PostsController < ApplicationController def show @post = Post.find(params[:id]) ! if stale? @post respond_with @post end end end etag: Model#cache_key last_modified: Model#updated_at

Slide 116

Slide 116 text

class PostSerializer < ActiveModel::Serializer cached ! delegate :cache_key, :to => :object end

Slide 117

Slide 117 text

ActiveModel Serializers CACHE

Slide 118

Slide 118 text

GATEWAY CACHING excuse me while I sidestep the tricky

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

TESTABLE?

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

rack-test

Slide 127

Slide 127 text

THE REST…?

Slide 128

Slide 128 text

THANKS! @benlovell