Slide 1

Slide 1 text

REST APIs on Rails

Slide 2

Slide 2 text

Lucas André de Alencar Full Stack Developer, Resultados Digitais

Slide 3

Slide 3 text

Which is the BEST REST API stack ever?!

Slide 4

Slide 4 text

There is no such thing!

Slide 5

Slide 5 text

Follow your heart! <3

Slide 6

Slide 6 text

gem 'rails-api'

Slide 7

Slide 7 text

# instead of class ApplicationController < ActionController::Base end # do class ApplicationController < ActionController::API include ActionController::HttpAuthentication::Token::ControllerMethods include ActionController::MimeResponds include AbstractController::Translation # Your beautiful code goes here! end

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

$ rails new long-trains --api ... $ rails g scaffold train type:string wagons:integer invoke active_record create db/migrate/20150907193647_create_trains.rb create app/models/train.rb invoke test_unit create test/models/train_test.rb create test/fixtures/trains.yml invoke resource_route route resources :trains create app/serializers/train_serializer.rb invoke scaffold_controller create app/controllers/trains_controller.rb invoke test_unit create test/controllers/trains_controller_test.rb

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Too much copying / pasting between versions No versioning included No documentation system included Disadvantages

Slide 12

Slide 12 text

Versioning

Slide 13

Slide 13 text

URL Request Params HTTP Header api.longtrains.com/v1/trains api.longtrains.com/trains?version=1 Accept: application/json; version=1

Slide 14

Slide 14 text

gem 'versionist'

Slide 15

Slide 15 text

LongTrains::Application.routes.draw do api_version(module: "V1", path: {value: "v1"}) do resources :trains end end URL api.longtrains.com/v1/trains config/routes.rb

Slide 16

Slide 16 text

LongTrains::Application.routes.draw do params = {name: "version", value: "1"} api_version(module: "V1", parameter: params) do resources :trains end end Request Params api.longtrains.com/trains?version=1 config/routes.rb

Slide 17

Slide 17 text

LongTrains::Application.routes.draw do params = {name: "Accept", value: "application/json; version=1"} api_version(module: "V1", header: params) do resources :trains end end HTTP Header Accept: application/json; version=1 config/routes.rb

Slide 18

Slide 18 text

Serializers

Slide 19

Slide 19 text

Run from the ones that use your models!

Slide 20

Slide 20 text

JSON is a representation of data

Slide 21

Slide 21 text

HTML is a representation of data

Slide 22

Slide 22 text

gem 'active_model_serializers'

Slide 23

Slide 23 text

class PostSerializer < ActiveModel::Serializer attributes :title, :body has_many :comments end class CommentSerializer < ActiveModel::Serializer attributes :name, :body belongs_to :post end class PostPreviewSerializer < ActiveModel::Serializer attributes :title, :preview end

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Security

Slide 26

Slide 26 text

Pure Rails ActionController::HttpAuthentication::Basic ActionController::HttpAuthentication::Digest ActionController::HttpAuthentication::Token

Slide 27

Slide 27 text

Summary rails-api versioning active_model_serializers JSON/HTML is representation of data use pure rails auth for simple authentication

Slide 28

Slide 28 text

Lucas André de Alencar @lucasalencar alencar.lucas.a@gmail.com RD is hiring!