Slide 1

Slide 1 text

Building APIs on Rails 5 with AMS

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

API

Slide 4

Slide 4 text

make the modern web what it is API

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

João Moura Senior engineer at Ride @joaomdmoura github/joaomdmoura

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Building APIs on Rails 5 with AMS

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 content

Slide 19

Slide 19 text

largest city in the Americas

Slide 20

Slide 20 text

largest city in the Americas

Slide 21

Slide 21 text

largest city in the Americas the most populous city of the southern hemisphere

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

No content

Slide 28

Slide 28 text

9456 ms average time of response

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No background job

Slide 32

Slide 32 text

Wrong Relationships No background job

Slide 33

Slide 33 text

Bad Architectures Wrong Relationships No background job

Slide 34

Slide 34 text

Bad Architectures Wrong Relationships Indexes missing No background job

Slide 35

Slide 35 text

Bad Architectures No cache at all Wrong Relationships Indexes missing No background job

Slide 36

Slide 36 text

Bad Architectures No cache at all Wrong Relationships Indexes missing No background job goo.gl/Jsw4lS

Slide 37

Slide 37 text

cat.exe stop working

Slide 38

Slide 38 text

cat.exe stop working

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

API application programming interface

Slide 42

Slide 42 text

one of the GREA TEST

Slide 43

Slide 43 text

one of the GREA TEST assets

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

one of the GREA TEST

Slide 47

Slide 47 text

one of the GREA TEST liabilities

Slide 48

Slide 48 text

An API should do one thing, and do it well. “ Joshua Blonch, Google tech talk, Jan 2007

Slide 49

Slide 49 text

Boa API

Slide 50

Slide 50 text

Boa API Performance Scalability Reusability Evolvability Documentation Easy to learn Easy to use Hard to misus

Slide 51

Slide 51 text

why Rails is the best tool

Slide 52

Slide 52 text

why Rails is the best tool it’s not

Slide 53

Slide 53 text

why Rails is the best tool it’s not but it’s a great one

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Performance Scalability Reusability Evolvability Documentation Easy to learn Easy to use Hard to misuse

Slide 56

Slide 56 text

Performance Scalability Reusability Evolvability Documentation Easy to learn Easy to use Hard to misuse }

Slide 57

Slide 57 text

Convenções Performance Scalability Reusability Evolvability Documentation Easy to learn Easy to use Hard to misuse }

Slide 58

Slide 58 text

Convenções }

Slide 59

Slide 59 text

} Convenções }

Slide 60

Slide 60 text

}Rails Convenções }

Slide 61

Slide 61 text

Rails is robust

Slide 62

Slide 62 text

rails-api

Slide 63

Slide 63 text

rails-api Subset of a normal Rails application, created for applications that don't require all functionality of a complete Rails application

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Rails 5

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

rails-api

Slide 72

Slide 72 text

rails-api } core ams

Slide 73

Slide 73 text

Active Model Serializer

Slide 74

Slide 74 text

AMS

Slide 75

Slide 75 text

“Bringing Convention over Configuration to your JSON” AMS

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count def comments_count object.comments.size end end

Slide 79

Slide 79 text

class PostSerializer < ActiveModel::Serializer

Slide 80

Slide 80 text

class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count

Slide 81

Slide 81 text

class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count def comments_count object.comments.size end end

Slide 82

Slide 82 text

{ id: 1, title: "ZOMG an amazing post", body: "Indeed, my friend", comments_count: 5 }

Slide 83

Slide 83 text

AMS 0.8.x

Slide 84

Slide 84 text

AMS 0.9.x

Slide 85

Slide 85 text

AMS 0.10

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

1. Adapters Pattern

Slide 88

Slide 88 text

1. Adapters Pattern Adapters describe how attributes and relationships should be serialized.

Slide 89

Slide 89 text

{ "links": { "self": "http://example.com/posts", "next": "http://example.com/posts?page[offset]=2", "last": "http://example.com/posts?page[offset]=10" }, "data": [{ "type": "posts", "id": "1", "title": "JSON API paints my bikeshed!", "links": { "self": "http://example.com/posts/1", "author": { "self": "http://example.com/posts/1/links/author", "related": "http://example.com/posts/1/author", "linkage": { "type": "people", "id": "9" } }, "comments": { "self": "http://example.com/posts/1/links/comments", "related": "http://example.com/posts/1/comments", "linkage": [ { "type": "comments", "id": "5" }, { "type": "comments", "id": "12" } ] } } }], "included": [{ "type": "people", "id": "9", "first-name": "Dan", "last-name": "Gebhardt", "twitter": "dgeb", "links": { "self": "http://example.com/people/9" } }, { "type": "comments", "id": "5", "body": "First!", "links": { "self": "http://example.com/comments/5" } }, { "type": "comments", "id": "12", "body": "I like XML better", "links": { "self": "http://example.com/comments/12" } }] }

Slide 90

Slide 90 text

2. JSONAPI { "links": { "self": "http://example.com/posts", "next": "http://example.com/posts?page[offset]=2", "last": "http://example.com/posts?page[offset]=10" }, "data": [{ "type": "posts", "id": "1", "title": "JSON API paints my bikeshed!", "links": { "self": "http://example.com/posts/1", "author": { "self": "http://example.com/posts/1/links/author", "related": "http://example.com/posts/1/author", "linkage": { "type": "people", "id": "9" } }, "comments": { "self": "http://example.com/posts/1/links/comments", "related": "http://example.com/posts/1/comments", "linkage": [ { "type": "comments", "id": "5" }, { "type": "comments", "id": "12" } ] } } }], "included": [{ "type": "people", "id": "9", "first-name": "Dan", "last-name": "Gebhardt", "twitter": "dgeb", "links": { "self": "http://example.com/people/9" } }, { "type": "comments", "id": "5", "body": "First!", "links": { "self": "http://example.com/comments/5" } }, { "type": "comments", "id": "12", "body": "I like XML better", "links": { "self": "http://example.com/comments/12" } }] }

Slide 91

Slide 91 text

2. JSONAPI A standard for building APIs in JSON. { "links": { "self": "http://example.com/posts", "next": "http://example.com/posts?page[offset]=2", "last": "http://example.com/posts?page[offset]=10" }, "data": [{ "type": "posts", "id": "1", "title": "JSON API paints my bikeshed!", "links": { "self": "http://example.com/posts/1", "author": { "self": "http://example.com/posts/1/links/author", "related": "http://example.com/posts/1/author", "linkage": { "type": "people", "id": "9" } }, "comments": { "self": "http://example.com/posts/1/links/comments", "related": "http://example.com/posts/1/comments", "linkage": [ { "type": "comments", "id": "5" }, { "type": "comments", "id": "12" } ] } } }], "included": [{ "type": "people", "id": "9", "first-name": "Dan", "last-name": "Gebhardt", "twitter": "dgeb", "links": { "self": "http://example.com/people/9" } }, { "type": "comments", "id": "5", "body": "First!", "links": { "self": "http://example.com/comments/5" } }, { "type": "comments", "id": "12", "body": "I like XML better", "links": { "self": "http://example.com/comments/12" } }] }

Slide 92

Slide 92 text

2. JSONAPI A standard for building APIs in JSON. { "links": { "self": "http://example.com/posts", "next": "http://example.com/posts?page[offset]=2", "last": "http://example.com/posts?page[offset]=10" }, "data": [{ "type": "posts", "id": "1", "title": "JSON API paints my bikeshed!", "links": { "self": "http://example.com/posts/1", "author": { "self": "http://example.com/posts/1/links/author", "related": "http://example.com/posts/1/author", "linkage": { "type": "people", "id": "9" } }, "comments": { "self": "http://example.com/posts/1/links/comments", "related": "http://example.com/posts/1/comments", "linkage": [ { "type": "comments", "id": "5" }, { "type": "comments", "id": "12" } ] } } }], "included": [{ "type": "people", "id": "9", "first-name": "Dan", "last-name": "Gebhardt", "twitter": "dgeb", "links": { "self": "http://example.com/people/9" } }, { "type": "comments", "id": "5", "body": "First!", "links": { "self": "http://example.com/comments/5" } }, { "type": "comments", "id": "12", "body": "I like XML better", "links": { "self": "http://example.com/comments/12" } }] }

Slide 93

Slide 93 text

JSONAPI 1.0

Slide 94

Slide 94 text

+

Slide 95

Slide 95 text

ActiveModel::Serializer.config.adapter = :json_api

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

3. Cache

Slide 98

Slide 98 text

3. Cache New implementation, optimised and following Rails conventions

Slide 99

Slide 99 text

class PostSerializer < ActiveModel::Serializer attributes :title, :body end

Slide 100

Slide 100 text

class PostSerializer < ActiveModel::Serializer cache attributes :title, :body end

Slide 101

Slide 101 text

class PostSerializer < ActiveModel::Serializer cache key: 'post' attributes :title, :body end

Slide 102

Slide 102 text

class PostSerializer < ActiveModel::Serializer cache key: 'post', expires_in: 3.hours attributes :title, :body end

Slide 103

Slide 103 text

class PostsController < ApplicationController def index @posts = Post.all render json: @posts end end

Slide 104

Slide 104 text

class PostsController < ApplicationController def index @posts = Post.all render json: @posts end end

Slide 105

Slide 105 text

class PostsController < ApplicationController def index @posts = Post.all render json: @posts end def show @post = Post.find(params[:id]) render json: @post end end

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

4. Fragment Cache

Slide 108

Slide 108 text

4. Fragment Cache Rails conventions to cache specific attributes

Slide 109

Slide 109 text

class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count def title "Post - #{object.title}" end def comments_count object.comments.size end end

Slide 110

Slide 110 text

def title "Post - #{object.title}" end def comments_count object.comments.size end

Slide 111

Slide 111 text

def title "Post - #{object.title}" end def comments_count object.comments.size end cacheable

Slide 112

Slide 112 text

def title "Post - #{object.title}" end def comments_count object.comments.size end cacheable non-cacheable

Slide 113

Slide 113 text

class PostSerializer < ActiveModel::Serializer attributes :title, :body, :comments_count def title "Post - #{object.title}" end def comments_count object.comments.size end end

Slide 114

Slide 114 text

class PostSerializer < ActiveModel::Serializer cache only: [:title] attributes :title, :body, :comments_count def title "Post - #{object.title}" end def comments_count object.comments.size end end

Slide 115

Slide 115 text

class PostSerializer < ActiveModel::Serializer cache except: [:comments_count] attributes :title, :body, :comments_count def title "Post - #{object.title}" end def comments_count object.comments.size end end

Slide 116

Slide 116 text

class PostSerializer < ActiveModel::Serializer cache key: 'post', expires_in: 3.hours, only: [:title] attributes :title, :body end

Slide 117

Slide 117 text

In God we trust… All the others bring data

Slide 118

Slide 118 text

0.9.x 42.680000 user system total real 17.630000 60.310000 71.201321

Slide 119

Slide 119 text

0.9.x 0.10.0 real 71.201321 63.926189

Slide 120

Slide 120 text

0.9.x 0.10.0 real 71.201321 57.851192

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

freaking amazing features

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

Deserialization Output equals the input

Slide 125

Slide 125 text

@post = Post.create(post_params) render json: @post, statue: :created end def post_params PostSerialization.deserialize(params) end

Slide 126

Slide 126 text

def create @post = Post.create(post_params) render json: @post, status: :created end def post_params PostSerialization.deserialize(params) end

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

Documentação Gift!

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

fetch multi pagination e more!

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

AMS 0.10.0

Slide 133

Slide 133 text

AMS 0.10.0

Slide 134

Slide 134 text

AMS 0.10.0

Slide 135

Slide 135 text

No content

Slide 136

Slide 136 text

0.10.0 rc3

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

one more thing

Slide 139

Slide 139 text

Thank you!

Slide 140

Slide 140 text

Thank you!

Slide 141

Slide 141 text

Thank you!

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

AMS 0.10

Slide 144

Slide 144 text

Adapter Pattern

Slide 145

Slide 145 text

Json API

Slide 146

Slide 146 text

Cache

Slide 147

Slide 147 text

Fragment Cache

Slide 148

Slide 148 text

Rails 5

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

ElixirbyaRubyist.com #ElixirbyaRubyist

Slide 153

Slide 153 text

@joaomdmoura ElixirbyaRubyist.com