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

Building with Rack

Building with Rack

Let's build a Rack app!

Alexander Clark

February 23, 2016
Tweet

More Decks by Alexander Clark

Other Decks in Programming

Transcript

  1. ?

  2. ?

  3. ?

  4. ?

  5. What does a Rack app look like? GET /foo HTTP/1.1

    Host: localhost:9292 Accept: text/html … ?
  6. What does a Rack app look like? GET /foo HTTP/1.1

    Host: localhost:9292 Accept: text/html … ? <!DOCTYPE html> <html> <head> …
  7. run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]

    } Responds to #call HTTP Status code Headers Response body Run
  8. run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]

    } Responds to #call Much HTTP Status code Such Headers Response body Wow Run
  9. <!DOCTYPE html> <html> <head> <title>Is it April Fools?</title> </head> <body>

    <h1><%= @april_fools ? 'YES' : 'NO' %></h1> </body> </html>
  10. require 'erb' class MyRackApp def initialize @april_fools = Time.now.strftime('%m%d') ==

    '0401' end def call(_env) ['200', {'Content-Type' => 'text/html'}, view] end def view [ERB.new(template).result(binding)] end def template File.open('index.html.erb', 'r').read end end
  11. ?

  12. run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']]

    } run MyRackApp.new ... def call(_env) ['200', {'Content-Type' => 'text/html'}, view] end