This document provides an overview and introduction to the Webmachine framework for building functional web applications in Ruby. The summary is:
Webmachine is a Ruby framework based on Erlang's web machine that treats each API resource as a finite state machine, allowing each request to be modeled as a flow through that state machine. It determines facts about requests like authentication, authorization, and content negotiation to respond appropriately. Webmachine parses requests to validate routes and resources exist, check permissions, and select the proper response format. This functional approach is well-suited for building RESTful APIs.
4. Who am I
❖ Mikhail Bortnyk
❖ github: @vessi
❖ twitter: @mikhailbortnyk
5. Who am I
❖ Mikhail Bortnyk
❖ github: @vessi
❖ twitter: @mikhailbortnyk
❖ language researcher
6. Who am I
❖ Mikhail Bortnyk
❖ github: @vessi
❖ twitter: @mikhailbortnyk
❖ language researcher
❖ co-founder of Kottans community school
7. Who am I
❖ Mikhail Bortnyk
❖ github: @vessi
❖ twitter: @mikhailbortnyk
❖ language researcher
❖ co-founder of Kottans community school
❖ 3rd contributor of mruby/c
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!)
12. Typical experience of web developer
CGI Old school is cool! (Bash, C, Perl, some scripting)
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. 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)
17. SPA is a new sexy
❖ No one uses ol’ goo’ ERB
❖ Javascript rules over the web
18. SPA is a new sexy
❖ No one uses ol’ goo’ ERB
❖ Javascript rules over the web
❖ Web does not work without JS
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/
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
21. 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
27. Solutions for APIs
❖ Still Rails. Now with API flavor (rails-api). Still Rails.
Still boring. Still overbloated.
28. Solutions for APIs
❖ Still Rails. Now with API flavor (rails-api). Still Rails.
Still boring. Still overbloated.
❖ Sinatra is a new shiny
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
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
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
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
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
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
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?
36. 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)
41. WebMachine-ruby
❖ Based on Erlang’s web machine
❖ Created by Sean Cribbs
❖ More than 5 years old
❖ Still alive!
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
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
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
45. 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
47. Fundamental idea
❖ Each API resource is a typical state machine
❖ Each request is a flow through this state machine
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
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
50. 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
51. Why state machine?
❖ Typical frameworks: request is running by predefined
procedure.
52. Why state machine?
❖ Typical frameworks: request is running by predefined
procedure. They are not your bros
53. Why state machine?
❖ Typical frameworks: request is running by predefined
procedure. They are not your bros
❖ Webmachine determines facts about requests
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
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
56. 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
68. Parsing request
❖ Initial request:
curl "http://localhost/kudos/1" -H "Accept:
application/json" -u test -p test
69. Parsing request
❖ Initial request:
curl "http://localhost/kudos/1" -H "Accept: application/json" -u test -p test
❖ Does route /kudos/:id exist?
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?
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?
72. 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?
73. 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?
74. 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