Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build a Web API with Hanami (ConFoo Montreal 2017)

Build a Web API with Hanami (ConFoo Montreal 2017)

Today, when building a web application, you very likely need to provide a web API. In Ruby, both web frameworks Rails or Sinatra are common options.

If you need something simpler and lighter than Rails but that doesn't require to reimplement a lot of common pieces, Hanami is a convenient and trendy option.

I'll present the reasons that pushed me to pick Hanami to develop a web API, and how to test and develop a web API with it.

https://github.com/toch/confoomontreal2017-jsonapi

Christophe Philemotte

March 09, 2017
Tweet

More Decks by Christophe Philemotte

Other Decks in Programming

Transcript

  1. # 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
  2. it 'is empty by default' do header 'Content-Type', 'application/json;' get

    '/api/speakers' expect(last_response.body).must_equal '[]' end end
  3. $ rake # Running: FF 2 runs, 0 assertions, 2

    failures, 0 errors, 0 skips
  4. # apps/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
  5. $ rake # Running: .. 2 runs, 3 assertions, 0

    failures, 0 errors, 0 skips
  6. # spec/api/features/list_speakers_spec.rb #... describe 'List speakers' do # ... describe

    'When speakers are recorded' do let(:repository) { SpeakerRepository.new } before do repository.clear repository.create(Speaker.new(name: 'Christophe Philemotte', twitter: '_toch', talk: 'Build a Web API with Hanami')) end
  7. 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
  8. $ rake # Running: EEE 3 runs, 0 assertions, 0

    failures, 3 errors, 0 skips
  9. # 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
  10. # apps/api/controllers/speakers/list.rb module Api::Controllers::Speakers class List include Api::Action accept :json

    expose :speakers def call(params) @speakers = SpeakerRepository.new.all end end end
  11. # 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
  12. $ rake # Running: … 3 runs, 5 assertions, 0

    failures, 0 errors, 0 skips