ams, api, rails and a developer, a love story

117
AMS, API, RAILS and a developer LOVE STORY

Upload: joao-moura

Post on 12-Apr-2017

500 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: AMS, API, RAILS and a developer, a Love Story

AMS, API, RAILSand a developer

LOVE STORY

Page 2: AMS, API, RAILS and a developer, a Love Story
Page 3: AMS, API, RAILS and a developer, a Love Story

API

Page 4: AMS, API, RAILS and a developer, a Love Story

make the modern web what it is

API

Page 5: AMS, API, RAILS and a developer, a Love Story
Page 6: AMS, API, RAILS and a developer, a Love Story
Page 7: AMS, API, RAILS and a developer, a Love Story
Page 8: AMS, API, RAILS and a developer, a Love Story
Page 9: AMS, API, RAILS and a developer, a Love Story

João Moura co-founder at Gioco

@joaomdmoura gioco.pro

Page 10: AMS, API, RAILS and a developer, a Love Story
Page 11: AMS, API, RAILS and a developer, a Love Story

AMS, API, RAILS

Page 12: AMS, API, RAILS and a developer, a Love Story

AMS, API, RAILSand a developer

Page 13: AMS, API, RAILS and a developer, a Love Story

AMS, API, RAILSand a developer

LOVE STORY

Page 14: AMS, API, RAILS and a developer, a Love Story
Page 15: AMS, API, RAILS and a developer, a Love Story
Page 16: AMS, API, RAILS and a developer, a Love Story
Page 17: AMS, API, RAILS and a developer, a Love Story
Page 18: AMS, API, RAILS and a developer, a Love Story

largest city in the Americas

Page 19: AMS, API, RAILS and a developer, a Love Story

largest city in the Americas

Page 20: AMS, API, RAILS and a developer, a Love Story

largest city in the Americas

the most populous city of the southern hemisphere

Page 21: AMS, API, RAILS and a developer, a Love Story
Page 22: AMS, API, RAILS and a developer, a Love Story
Page 23: AMS, API, RAILS and a developer, a Love Story
Page 24: AMS, API, RAILS and a developer, a Love Story
Page 25: AMS, API, RAILS and a developer, a Love Story
Page 26: AMS, API, RAILS and a developer, a Love Story
Page 27: AMS, API, RAILS and a developer, a Love Story
Page 28: AMS, API, RAILS and a developer, a Love Story

No Background Job

Page 29: AMS, API, RAILS and a developer, a Love Story

Bad Relationships

No Background Job

Page 30: AMS, API, RAILS and a developer, a Love Story

Bad Architecture

Bad Relationships

No Background Job

Page 31: AMS, API, RAILS and a developer, a Love Story

Bad Architecture

Bad Relationships

Indexes Missing

No Background Job

Page 32: AMS, API, RAILS and a developer, a Love Story

Bad Architecture

No Cache at all

Bad Relationships

Indexes Missing

No Background Job

Page 33: AMS, API, RAILS and a developer, a Love Story
Page 34: AMS, API, RAILS and a developer, a Love Story
Page 35: AMS, API, RAILS and a developer, a Love Story

Open Source

Page 36: AMS, API, RAILS and a developer, a Love Story

😘Open Source

Page 37: AMS, API, RAILS and a developer, a Love Story

APIapplication programming interface

Page 38: AMS, API, RAILS and a developer, a Love Story

APIapplication programming interface

in·ter·face /ˈin(t)ərˌfās/

Page 39: AMS, API, RAILS and a developer, a Love Story

APIapplication programming interface

in·ter·face /ˈin(t)ərˌfās/

a point where two systems meet and interact.

Page 40: AMS, API, RAILS and a developer, a Love Story

ONE OF THE GREATEST

assets

Page 41: AMS, API, RAILS and a developer, a Love Story

ONE OF THE GREATEST

liabilities

Page 42: AMS, API, RAILS and a developer, a Love Story

Good API

Page 43: AMS, API, RAILS and a developer, a Love Story

Good APIPerformanceScalabilityReusabilityEvolvability

Documentation Easy to learnEasy to useHard to misuse

Page 44: AMS, API, RAILS and a developer, a Love Story

An API should do one thing, and do it well.“

 Joshua Blonch, Google tech talk, Jan 2007

Page 45: AMS, API, RAILS and a developer, a Love Story

why Rails

is the best tool!

Page 46: AMS, API, RAILS and a developer, a Love Story

why Rails

is the best tool!

It’s not

Page 47: AMS, API, RAILS and a developer, a Love Story

why Rails

is the best tool!

It’s notBut it’s a great one

Page 48: AMS, API, RAILS and a developer, a Love Story
Page 49: AMS, API, RAILS and a developer, a Love Story

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

Page 50: AMS, API, RAILS and a developer, a Love Story

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

Page 51: AMS, API, RAILS and a developer, a Love Story

Conventions

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

Page 52: AMS, API, RAILS and a developer, a Love Story

RailsConventions

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

Page 53: AMS, API, RAILS and a developer, a Love Story

}RailsConventions

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

Page 54: AMS, API, RAILS and a developer, a Love Story

Railsis robust

Page 55: AMS, API, RAILS and a developer, a Love Story

rails-api

Active Model Serializer

Page 56: AMS, API, RAILS and a developer, a Love Story

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

Active Model Serializer

Page 57: AMS, API, RAILS and a developer, a Love Story

Active Model Serializer

Page 58: AMS, API, RAILS and a developer, a Love Story

AMS

Page 59: AMS, API, RAILS and a developer, a Love Story

Bringing Convention over Configuration to your JSON

AMS

Page 60: AMS, API, RAILS and a developer, a Love Story

class PostSerializer < ActiveModel::Serializer

attributes :title, :body, :comments_count

def comments_count object.comments.size end

end

Page 61: AMS, API, RAILS and a developer, a Love Story

class PostSerializer < ActiveModel::Serializer

Page 62: AMS, API, RAILS and a developer, a Love Story

class PostSerializer < ActiveModel::Serializer

attributes :title, :body, :comments_count

Page 63: AMS, API, RAILS and a developer, a Love Story

class PostSerializer < ActiveModel::Serializer

attributes :title, :body, :comments_count

def comments_count object.comments.size end

end

Page 64: AMS, API, RAILS and a developer, a Love Story

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

Page 65: AMS, API, RAILS and a developer, a Love Story

AMS 0.8.x

Page 66: AMS, API, RAILS and a developer, a Love Story

AMS 0.9.x

Page 67: AMS, API, RAILS and a developer, a Love Story

AMS 0.10

Page 68: AMS, API, RAILS and a developer, a Love Story
Page 69: AMS, API, RAILS and a developer, a Love Story

1. Adapters Pattern

Page 70: AMS, API, RAILS and a developer, a Love Story

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

Page 71: AMS, API, RAILS and a developer, a Love Story

{ "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"

} }] }

Page 72: AMS, API, RAILS and a developer, a Love Story

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"

} }] }

Page 73: AMS, API, RAILS and a developer, a Love Story

2. JSONAPIA 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"

} }] }

Page 74: AMS, API, RAILS and a developer, a Love Story

2. JSONAPIA 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"

} }] }

Page 75: AMS, API, RAILS and a developer, a Love Story
Page 76: AMS, API, RAILS and a developer, a Love Story

JSONAPI RC3

Page 77: AMS, API, RAILS and a developer, a Love Story

JSONAPI 1.0

Page 78: AMS, API, RAILS and a developer, a Love Story

JSONAPI 1.0on May 21, 2015

Page 79: AMS, API, RAILS and a developer, a Love Story
Page 80: AMS, API, RAILS and a developer, a Love Story

3. Cache

Page 81: AMS, API, RAILS and a developer, a Love Story

3. CacheNew implementation, optimised and following Rails conventions

Page 82: AMS, API, RAILS and a developer, a Love Story

class PostSerializer < ActiveModel::Serializer

attributes :title, :body end

Page 83: AMS, API, RAILS and a developer, a Love Story

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

Page 84: AMS, API, RAILS and a developer, a Love Story

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

Page 85: AMS, API, RAILS and a developer, a Love Story

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

Page 86: AMS, API, RAILS and a developer, a Love Story

class PostsController < ApplicationController

def index @posts = Post.all render json: @posts end

end

Page 87: AMS, API, RAILS and a developer, a Love Story

class PostsController < ApplicationController

def index @posts = Post.all render json: @posts end

end

Page 88: AMS, API, RAILS and a developer, a Love Story

class PostsController < ApplicationController

def index @posts = Post.all render json: @posts end

def show @post = Post.find(params[:id]) render json: @post end

end

Page 89: AMS, API, RAILS and a developer, a Love Story
Page 90: AMS, API, RAILS and a developer, a Love Story

4. Fragment Cache

Page 91: AMS, API, RAILS and a developer, a Love Story

4. Fragment CacheRails conventions to cache specific attributes

Page 92: AMS, API, RAILS and a developer, a Love Story

class PostSerializer < ActiveModel::Serializer

attributes :title, :body, :comments_count def title "Post - #{object.title}" end

def comments_count object.comments.size end

end

Page 93: AMS, API, RAILS and a developer, a Love Story

def title "Post - #{object.title}" end

def comments_count object.comments.size end

Page 94: AMS, API, RAILS and a developer, a Love Story

def title "Post - #{object.title}" end

def comments_count object.comments.size end

cacheable

Page 95: AMS, API, RAILS and a developer, a Love Story

def title "Post - #{object.title}" end

def comments_count object.comments.size end

cacheable

non-cacheable

Page 96: AMS, API, RAILS and a developer, a Love Story

class PostSerializer < ActiveModel::Serializer

attributes :title, :body, :comments_count def title "Post - #{object.title}" end

def comments_count object.comments.size end

end

Page 97: AMS, API, RAILS and a developer, a Love Story

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

Page 98: AMS, API, RAILS and a developer, a Love Story

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

Page 99: AMS, API, RAILS and a developer, a Love Story

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

Page 100: AMS, API, RAILS and a developer, a Love Story

In god we trust…all others bring data

Page 101: AMS, API, RAILS and a developer, a Love Story

0.9.x42.680000user

system

total

real

17.630000

60.310000

71.201321

Page 102: AMS, API, RAILS and a developer, a Love Story

0.9.x 0.10.0real 71.201321 63.926189

Page 103: AMS, API, RAILS and a developer, a Love Story

0.9.x 0.10.0real 71.201321 57.851192

Page 104: AMS, API, RAILS and a developer, a Love Story
Page 105: AMS, API, RAILS and a developer, a Love Story

WIP

Page 106: AMS, API, RAILS and a developer, a Love Story

WIPfetch multibenchmarkand more…

Page 107: AMS, API, RAILS and a developer, a Love Story
Page 108: AMS, API, RAILS and a developer, a Love Story

one more thing

Page 109: AMS, API, RAILS and a developer, a Love Story
Page 110: AMS, API, RAILS and a developer, a Love Story

AMS 0.10.0

Page 111: AMS, API, RAILS and a developer, a Love Story
Page 112: AMS, API, RAILS and a developer, a Love Story

another thing

Page 113: AMS, API, RAILS and a developer, a Love Story
Page 114: AMS, API, RAILS and a developer, a Love Story

Rails 5maybe?

Page 115: AMS, API, RAILS and a developer, a Love Story

special tks!

Page 116: AMS, API, RAILS and a developer, a Love Story

special tks!

Page 117: AMS, API, RAILS and a developer, a Love Story

João Moura co-founder at Gioco

@joaomdmoura gioco.pro