Slide 1

Slide 1 text

Build a Web API with Hanami

Slide 2

Slide 2 text

Bonjour, Je m'appelle Christophe.

Slide 3

Slide 3 text

pullreview.com rubybelgium.be euranova.eu

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

toch _toch ibakesoftware.com

Slide 6

Slide 6 text

Hanami?

Slide 7

Slide 7 text

A Ruby Web Framework

Slide 8

Slide 8 text

Light & Simple

Slide 9

Slide 9 text

Modular

Slide 10

Slide 10 text

Non Intrusive

Slide 11

Slide 11 text

Test Friendly ✅

Slide 12

Slide 12 text

Thread Safe ☂

Slide 13

Slide 13 text

Why Hanami?

Slide 14

Slide 14 text

What do we need?

Slide 15

Slide 15 text

Sinatra?

Slide 16

Slide 16 text

Rails?

Slide 17

Slide 17 text

Hanami!

Slide 18

Slide 18 text

A Web API

Slide 19

Slide 19 text

GET /api/speakers

Slide 20

Slide 20 text

$ hanami new takeoffconf \ --database=postgres \ --application-name=api

Slide 21

Slide 21 text

. ├── apps ├── config ├── db ├── lib ├── public └── spec

Slide 22

Slide 22 text

Gemfile

Slide 23

Slide 23 text

gem 'bundler' gem 'rake' gem 'hanami', '~> 0.8' gem 'hanami-model', '~> 0.6' gem 'pg'

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Container Architecture

Slide 26

Slide 26 text

apps └── api ├── application.rb ├── config ├── controllers ├── templates └── views

Slide 27

Slide 27 text

# config/environment.rb Hanami::Container.configure do mount Api::Application, at: '/api' end

Slide 28

Slide 28 text

Business logic

Slide 29

Slide 29 text

lib ├── takeoffconf │ ├── entities │ ├── mailers │ └── repositories └── takeoffconf.rb

Slide 30

Slide 30 text

Test First

Slide 31

Slide 31 text

# spec/api/features/list_speakers_spec.rb require 'api_helper' describe 'List speakers' do it 'is successful' do header 'Content-Type', 'application/json;' get '/api/speakers' expect(last_response).must_be :ok? expect(last_response.content_type) .must_include "application/json" end

Slide 32

Slide 32 text

it 'is empty by default' do header 'Content-Type', 'application/json;' get '/api/speakers' expect(last_response.body).must_equal '[]' end end

Slide 33

Slide 33 text

Default Content & Accept type

Slide 34

Slide 34 text

# api/application.rb module Api class Application < Hanami::Application configure do # ... default_request_format :json default_response_format :json body_parsers :json end end end

Slide 35

Slide 35 text

One Route

Slide 36

Slide 36 text

# apps/api/config/routes.rb get '/speakers', to: 'speakers#list'

Slide 37

Slide 37 text

One Action

Slide 38

Slide 38 text

$ hanami generate action api speakers#list

Slide 39

Slide 39 text

# apps/api/controllers/speakers/list.rb module Api::Controllers::Speakers class List include Api::Action accept :json def call(params) end end end

Slide 40

Slide 40 text

No Template One View

Slide 41

Slide 41 text

# apps/api/views/speakers/list.rb module Api::Views::Speakers class List include Api::View layout false def render "[]" end end end

Slide 42

Slide 42 text

Test First

Slide 43

Slide 43 text

# spec/api/features/list_speakers_spec.rb #... describe 'List speakers' do # ... describe 'When speakers are recorded' do before do SpeakerRepository.clear SpeakerRepository.create(Speaker.new(name: 'Christophe Philemotte', twitter: '_toch', talk: 'Build a Web API with Hanami')) end

Slide 44

Slide 44 text

it 'returns an array of those speakers' do header 'Content-Type', 'application/json;' get '/api/speakers' expect(last_response.body).must_include "\"name\":\"Christophe Philemotte\",\"twitter\":\"_toch\",\"talk\":\"Build a Web API with Hanami\"" end end end

Slide 45

Slide 45 text

Entity & Repository

Slide 46

Slide 46 text

$ hanami generate model speaker $ hanami generate migration \ create_speakers

Slide 47

Slide 47 text

# db/migrations/20161019092946_create_speakers.rb Hanami::Model.migration do change do create_table :speakers do primary_key :id column :name, String, null: false column :twitter, String column :talk, String, null: false end end end

Slide 48

Slide 48 text

$ hanami db create $ hanami db migrate

Slide 49

Slide 49 text

# lib/takeoffconf/entities/speaker.rb class Speaker include Hanami::Entity attributes :name, :twitter, :talk end

Slide 50

Slide 50 text

# lib/takeoffconf/entities/speaker_repository.rb class SpeakerRepository include Hanami::Repository end

Slide 51

Slide 51 text

# lib/takeoffconf.rb # ... mapping do collection :speakers do entity Speaker repository SpeakerRepository attribute :id, Integer attribute :name, String attribute :twitter, String attribute :talk, String end end # ...

Slide 52

Slide 52 text

Retrieve & Expose

Slide 53

Slide 53 text

# apps/api/controllers/speakers/list.rb module Api::Controllers::Speakers class List include Api::Action accept :json expose :speakers def call(params) @speakers = SpeakerRepository.all end end end

Slide 54

Slide 54 text

Serialize & View

Slide 55

Slide 55 text

# apps/api/views/speakers/list.rb require 'json' module Api::Views::Speakers class List include Api::View layout false def render _raw JSON.dump(speakers.map{ |speaker| speaker.to_h }) end end end

Slide 56

Slide 56 text

Lessons

Slide 57

Slide 57 text

Not yet stable But play the game

Slide 58

Slide 58 text

Read The Code Luke

Slide 59

Slide 59 text

Ruby FTW

Slide 60

Slide 60 text

Very Friendly Community

Slide 61

Slide 61 text

More Code But Clear Intent

Slide 62

Slide 62 text

❓ toch/takeoffconf2016-jsonapi toch _toch ibakesoftware.com