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

Functional Web Apps with Webmachine

Functional Web Apps with Webmachine

Speech for RubyMeditation meetup #13

Avatar for Mikhail Bortnyk

Mikhail Bortnyk

February 11, 2017

Other Decks in Programming

Transcript

  1. Who am I ❖ Mikhail Bortnyk ❖ github: @vessi ❖

    twitter: @mikhailbortnyk ❖ language researcher
  2. Who am I ❖ Mikhail Bortnyk ❖ github: @vessi ❖

    twitter: @mikhailbortnyk ❖ language researcher ❖ co-founder of Kottans community school
  3. Who am I ❖ Mikhail Bortnyk ❖ github: @vessi ❖

    twitter: @mikhailbortnyk ❖ language researcher ❖ co-founder of Kottans community school ❖ 3rd contributor of mruby/c
  4. 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!)
  5. Typical experience of web developer CGI Old school is cool!

    (Bash, C, Perl, some scripting) Servlets I know your pain, bro (PHP, Java)
  6. 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)
  7. SPA is a new sexy ❖ No one uses ol’

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

    goo’ ERB ❖ Javascript rules over the web ❖ Web does not work without JS
  9. 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/
  10. 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
  11. 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
  12. Solutions for APIs ❖ Still Rails. Now with API flavor

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

    (rails-api). Still Rails. Still boring. Still overbloated. ❖ Sinatra is a new shiny
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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?
  21. 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)
  22. WebMachine-ruby ❖ Based on Erlang’s web machine ❖ Created by

    Sean Cribbs ❖ More than 5 years old ❖ Still alive!
  23. 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
  24. 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
  25. 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
  26. 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
  27. Fundamental idea ❖ Each API resource is a typical state

    machine ❖ Each request is a flow through this state machine
  28. Fundamental idea ❖ Each API resource is a typical state

    machine ❖ Each request is a flow through this state machine ❖ Most requests are pretty standard
  29. 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
  30. 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
  31. Why state machine? ❖ Typical frameworks: request is running by

    predefined procedure. They are not your bros
  32. Why state machine? ❖ Typical frameworks: request is running by

    predefined procedure. They are not your bros ❖ Webmachine determines facts about requests
  33. 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
  34. 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
  35. 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
  36. Parsing request ❖ Initial request: 
 curl "http://localhost/kudos/1" -H "Accept:

    application/json" -u test -p test ❖ Does route /kudos/:id exist?
  37. 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?
  38. 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?
  39. 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?
  40. 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?
  41. Typical resource class 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