functional web apps with webmachine framework - mikhail bortnyk

80
Ruby Meditation #13 Functional web apps with Webmachine framework Mikhail Bortnyk, Amoniac OÜ

Upload: ruby-meditation

Post on 11-Apr-2017

34 views

Category:

Technology


2 download

TRANSCRIPT

Ruby Meditation #13

Functional web apps with Webmachine framework

Mikhail Bortnyk, Amoniac OÜ

Who am I❖ Mikhail Bortnyk

Who am I❖ Mikhail Bortnyk

❖ github: @vessi

Who am I❖ Mikhail Bortnyk

❖ github: @vessi

❖ twitter: @mikhailbortnyk

Who am I❖ Mikhail Bortnyk

❖ github: @vessi

❖ twitter: @mikhailbortnyk

❖ language researcher

Who am I❖ Mikhail Bortnyk

❖ github: @vessi

❖ twitter: @mikhailbortnyk

❖ language researcher

❖ co-founder of Kottans community school

Who am I❖ Mikhail Bortnyk

❖ github: @vessi

❖ twitter: @mikhailbortnyk

❖ language researcher

❖ co-founder of Kottans community school

❖ 3rd contributor of mruby/c

Who am I❖ Mikhail Bortnyk

❖ github: @vessi

❖ twitter: @mikhailbortnyk

❖ language researcher

❖ co-founder of Kottans community school

❖ 3rd contributor of mruby/c

❖ Ruby developer (not Rails!)

Source code❖ [Link to presentation online]

Source code❖ [Link to presentation online]

❖ [Link to source code on GitHub]

Introduction

Typical experience of web developer

CGI Old school is cool! (Bash, C, Perl, some scripting)

Typical experience of web developer

CGI Old school is cool! (Bash, C, Perl, some scripting)

Servlets I know your pain, bro (PHP, Java)

Typical experience of web developer

CGI Old school is cool! (Bash, C, Perl, some scripting)

Servlets I know your pain, bro (PHP, Java)

MVC The thing we all know (Python/Django, Ruby/Rails etc)

SPA is a new sexy❖ No one uses ol’ goo’ ERB

SPA is a new sexy❖ No one uses ol’ goo’ ERB❖ Javascript rules over the web

SPA is a new sexy❖ No one uses ol’ goo’ ERB❖ Javascript rules over the web❖ Web does not work without JS

SPA is a new sexy❖ No one uses ol’ goo’ ERB❖ Javascript rules over the web❖ Web does not work without JS❖ http://tobyho.com/

2010/03/11/how-much-of-the-web-actually/

SPA is a new sexy❖ No one uses ol’ goo’ ERB❖ Javascript rules over the web❖ Web does not work without JS❖ http://tobyho.com/

2010/03/11/how-much-of-the-web-actually/

❖ In fact, you are limited to StackOverflow, Amazon and Google Search

SPA is a new sexy❖ No one uses ol’ goo’ ERB

❖ Javascript rules over the web

❖ Web does not work without JS

❖ http://tobyho.com/2010/03/11/how-much-of-the-web-actually/

❖ In fact, you are limited to StackOverflow, Amazon and Google Search

❖ Actually, only paranoids disable JS in 2017

We are outdated

No more MPA applications

Web developers are developing APIs

APIs for SPAs, for mobile applications, for another APIs

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api)

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny OH WAI~~. A little too big

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny OH WAI~~. A little too big

❖ Microframeworks like Cuba, NYNY

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny OH WAI~~. A little too big

❖ Microframeworks like Cuba, NYNY - too much to write

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny OH WAI~~. A little too big

❖ Microframeworks like Cuba, NYNY - too much to write

❖ Roda

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny OH WAI~~. A little too big

❖ Microframeworks like Cuba, NYNY - too much to write

❖ Roda - a challenger appears. No one noticed

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny OH WAI~~. A little too big

❖ Microframeworks like Cuba, NYNY - too much to write

❖ Roda - a challenger appears. No one noticed

❖ Rack

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny OH WAI~~. A little too big

❖ Microframeworks like Cuba, NYNY - too much to write

❖ Roda - a challenger appears. No one noticed

❖ Rack - maybe easier to rewrite the whole stack from scratch?

Solutions for APIs❖ Still Rails. Now with API flavor (rails-api). Still Rails.

Still boring. Still overbloated.

❖ Sinatra is a new shiny OH WAI~~. A little too big

❖ Microframeworks like Cuba, NYNY - too much to write

❖ Roda - a challenger appears. No one noticed

❖ Rack - maybe easier to rewrite the whole stack from scratch? (And end up with buggy unfinished Rails)

If you can’t create it, steal it

WebMachine-ruby❖ Based on Erlang’s web machine

WebMachine-ruby❖ Based on Erlang’s web machine

❖ Created by Sean Cribbs

WebMachine-ruby❖ Based on Erlang’s web machine

❖ Created by Sean Cribbs

❖ More than 5 years old

WebMachine-ruby❖ Based on Erlang’s web machine

❖ Created by Sean Cribbs

❖ More than 5 years old

❖ Still alive!

WebMachine-ruby❖ Based on Erlang’s web machine

❖ Created by Sean Cribbs

❖ More than 5 years old

❖ Still alive! Last commit - on 30 Jan 2017

WebMachine-ruby❖ Based on Erlang’s web machine

❖ Created by Sean Cribbs

❖ More than 5 years old

❖ Still alive! Last commit - on 30 Jan 2017

❖ 13 issues opened and 80 closed

WebMachine-ruby❖ Based on Erlang’s web machine

❖ Created by Sean Cribbs

❖ More than 5 years old

❖ Still alive! Last commit - on 30 Jan 2017

❖ 13 issues opened and 80 closed

❖ Author works with Erlang, Ruby and Elixir

WebMachine-ruby❖ Based on Erlang’s web machine

❖ Created by Sean Cribbs

❖ More than 5 years old

❖ Still alive! Last commit - on 30 Jan 2017

❖ 13 issues opened and 80 closed

❖ Author works with Erlang, Ruby and Elixir

❖ github://webmachine/webmachine-ruby.git

Fundamental idea❖ Each API resource is a typical state machine

Fundamental idea❖ Each API resource is a typical state machine

❖ Each request is a flow through this state machine

Fundamental idea❖ Each API resource is a typical state machine

❖ Each request is a flow through this state machine

❖ Most requests are pretty standard

Fundamental idea❖ Each API resource is a typical state machine

❖ Each request is a flow through this state machine

❖ Most requests are pretty standard

❖ Functional behavior is more suiting case for this

Fundamental idea❖ Each API resource is a typical state machine

❖ Each request is a flow through this state machine

❖ Most requests are pretty standard

❖ Functional behavior is more suiting case for this

❖ Define standards and override only needed things

Why state machine?❖ Typical frameworks: request is running by predefined

procedure.

Why state machine?❖ Typical frameworks: request is running by predefined

procedure. They are not your bros

Why state machine?❖ Typical frameworks: request is running by predefined

procedure. They are not your bros

❖ Webmachine determines facts about requests

Why state machine?❖ Typical frameworks: request is running by predefined

procedure. They are not your bros

❖ Webmachine determines facts about requests

❖ Uses finite state machine to determine proper response

Why state machine?❖ Typical frameworks: request is running by predefined

procedure. They are not your bros

❖ Webmachine determines facts about requests

❖ Uses finite state machine to determine proper response

❖ Each fact can easily be tested

Why state machine?❖ Typical frameworks: request is running by predefined

procedure. They are not your bros

❖ Webmachine determines facts about requests

❖ Uses finite state machine to determine proper response

❖ Each fact can easily be tested

❖ Webmachine is your bro

State machine diagram

Request to resource flow

Validation and authorization

Content negotiation

Existence and redirection

Conditional requests

PUT/POST (resource creation)

DELETE (resource deletion)

render body

How it parses your request?

Parsing request❖ Initial request: curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test

Parsing request❖ Initial request:

curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test

❖ Does route /kudos/:id exist?

Parsing request❖ Initial request:

curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test

❖ Does route /kudos/:id exist?

❖ Does resource with ID 1 exist?

Parsing request❖ Initial request:

curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test

❖ Does route /kudos/:id exist?

❖ Does resource with ID 1 exist?

❖ Is this user with this password allowed to execute this request on resource?

Parsing request❖ Initial request:

curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test

❖ Does route /kudos/:id exist?

❖ Does resource with ID 1 exist?

❖ Is this user with this password allowed to execute this request on resource?

❖ Can this method be called for this resource?

Parsing request❖ Initial request:

curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test

❖ Does route /kudos/:id exist?

❖ Does resource with ID 1 exist?

❖ Is this user with this password allowed to execute this request on resource?

❖ Can this method be called for this resource?

❖ Can this resource be rendered with this content type?

Typical resourceclass KudoResource < Webmachine::Resource def allowed_methods ["GET"] end

def content_types_provided [["application/json", :to_json]] end

def resource_exists? kudo # Truthy or falsey end

def to_json kudo.to_json end

private

def kudo @kudo ||= Kudo.find(id) end

def id request.path_info[:id] end end

Little coding session

Bonus: request trace

Even more

Lattice: Webmachine on actors

Thank you!

Questions?