Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Functional Web Apps with WebMachine Framework - Mikhail Bortnyk

96 views

Published on

Ruby Meditation 13 - 11.02.2017, BC Incom, Kyiv, 31-33 Smolenska str.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Functional Web Apps with WebMachine Framework - Mikhail Bortnyk

  1. 1. Ruby Meditation #13 Functional web apps with Webmachine framework Mikhail Bortnyk, Amoniac OÜ
  2. 2. Who am I ❖ Mikhail Bortnyk
  3. 3. Who am I ❖ Mikhail Bortnyk ❖ github: @vessi
  4. 4. Who am I ❖ Mikhail Bortnyk ❖ github: @vessi ❖ twitter: @mikhailbortnyk
  5. 5. Who am I ❖ Mikhail Bortnyk ❖ github: @vessi ❖ twitter: @mikhailbortnyk ❖ language researcher
  6. 6. Who am I ❖ Mikhail Bortnyk ❖ github: @vessi ❖ twitter: @mikhailbortnyk ❖ language researcher ❖ co-founder of Kottans community school
  7. 7. Who am I ❖ Mikhail Bortnyk ❖ github: @vessi ❖ twitter: @mikhailbortnyk ❖ language researcher ❖ co-founder of Kottans community school ❖ 3rd contributor of mruby/c
  8. 8. 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!)
  9. 9. Source code ❖ [Link to presentation online]
  10. 10. Source code ❖ [Link to presentation online] ❖ [Link to source code on GitHub]
  11. 11. Introduction
  12. 12. Typical experience of web developer CGI Old school is cool! (Bash, C, Perl, some scripting)
  13. 13. Typical experience of web developer CGI Old school is cool! (Bash, C, Perl, some scripting) Servlets I know your pain, bro (PHP, Java)
  14. 14. 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)
  15. 15. SPA is a new sexy ❖ No one uses ol’ goo’ ERB
  16. 16. SPA is a new sexy ❖ No one uses ol’ goo’ ERB ❖ Javascript rules over the web
  17. 17. SPA is a new sexy ❖ No one uses ol’ goo’ ERB ❖ Javascript rules over the web ❖ Web does not work without JS
  18. 18. 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/
  19. 19. 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
  20. 20. 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
  21. 21. We are outdated
  22. 22. No more MPA applications
  23. 23. Web developers are developing APIs
  24. 24. APIs for SPAs, for mobile applications, for another APIs
  25. 25. Solutions for APIs ❖ Still Rails. Now with API flavor (rails-api)
  26. 26. Solutions for APIs ❖ Still Rails. Now with API flavor (rails-api). Still Rails. Still boring. Still overbloated.
  27. 27. Solutions for APIs ❖ Still Rails. Now with API flavor (rails-api). Still Rails. Still boring. Still overbloated. ❖ Sinatra is a new shiny
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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
  32. 32. 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
  33. 33. 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
  34. 34. 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?
  35. 35. 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)
  36. 36. If you can’t create it, steal it
  37. 37. WebMachine-ruby ❖ Based on Erlang’s web machine
  38. 38. WebMachine-ruby ❖ Based on Erlang’s web machine ❖ Created by Sean Cribbs
  39. 39. WebMachine-ruby ❖ Based on Erlang’s web machine ❖ Created by Sean Cribbs ❖ More than 5 years old
  40. 40. WebMachine-ruby ❖ Based on Erlang’s web machine ❖ Created by Sean Cribbs ❖ More than 5 years old ❖ Still alive!
  41. 41. 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
  42. 42. 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
  43. 43. 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
  44. 44. 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
  45. 45. Fundamental idea ❖ Each API resource is a typical state machine
  46. 46. Fundamental idea ❖ Each API resource is a typical state machine ❖ Each request is a flow through this state machine
  47. 47. Fundamental idea ❖ Each API resource is a typical state machine ❖ Each request is a flow through this state machine ❖ Most requests are pretty standard
  48. 48. 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
  49. 49. 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
  50. 50. Why state machine? ❖ Typical frameworks: request is running by predefined procedure.
  51. 51. Why state machine? ❖ Typical frameworks: request is running by predefined procedure. They are not your bros
  52. 52. Why state machine? ❖ Typical frameworks: request is running by predefined procedure. They are not your bros ❖ Webmachine determines facts about requests
  53. 53. 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
  54. 54. 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
  55. 55. 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
  56. 56. State machine diagram
  57. 57. Request to resource flow
  58. 58. Validation and authorization
  59. 59. Content negotiation
  60. 60. Existence and redirection
  61. 61. Conditional requests
  62. 62. PUT/POST (resource creation)
  63. 63. DELETE (resource deletion)
  64. 64. render body
  65. 65. How it parses your request?
  66. 66. Parsing request ❖ Initial request: 
 curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test
  67. 67. Parsing request ❖ Initial request: 
 curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test ❖ Does route /kudos/:id exist?
  68. 68. 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?
  69. 69. 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?
  70. 70. 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?
  71. 71. 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?
  72. 72. 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
  73. 73. Little coding session
  74. 74. Bonus: request trace
  75. 75. Even more
  76. 76. Lattice: Webmachine on actors
  77. 77. Thank you!
  78. 78. Questions?

×