Building Web Apps with Rack and Sinatra

01936081830eb609b634e8b14a2a654e?s=47 Tom Black
October 16, 2012

Building Web Apps with Rack and Sinatra

Project files and notes at www.blacktm.com

01936081830eb609b634e8b14a2a654e?s=128

Tom Black

October 16, 2012
Tweet

Transcript

  1. 2.

    Objectives ✔ Describe the HTTP request process ✔ Define Rack,

    describe its purpose ✔ Build a simple Rack web app ✔ Define, build, and use middleware ✔ Define DSL (compare with “framework”) ✔ Build a simple Sinatra app
  2. 3.

    Things You’ll Need ✔ gem install thin ✔ gem install

    sinatra ✔ gem install heroku ✔ Project files and notes at blacktm.com
  3. 8.
  4. 11.

    What is a Rack app? A Ruby object that responds

    to a “call” method, taking a single hash parameter, and returning an array which contains the response status code, response headers, and response body as an array of strings. “In English, please.”
  5. 12.

    $ rackup config.ru Run it! Open “simple_rack” and edit the

    “config.ru” file. # config.ru - rackup file require 'rack' class MyApp def call(env) headers = { "Content-Type" => "text/html" } [200, headers, ["Hello world!"]] end end run MyApp.new
  6. 14.

    Who cares? ✔ You just built a web app. Damn.

    ✔ Important in understanding (and building) web frameworks (e.g. Rails) ✔ Middleware!
  7. 17.

    Let’s explore Middleware! 1. Open “rack_middleware” 2. Stop the server

    (ctrl-c) 3. Run: $ rackup config.ru 4. Open: http://0.0.0.0:9292 or: http://localhost:9292
  8. 18.
  9. 20.

    # app.rb class MyApp def call(env) puts "Hello from MyAPP"

    return [200, {}, ["Hello"]] end end No “Content-Type”
  10. 22.

    class ContentType def initialize(app) @app = app end def call(env)

    status, headers, body = @app.call(env) puts "Hello from ContentType" # TODO: Add "Content-Type" to the HTTP # headers and return the response. end end Open “config.ru”
  11. 23.

    Add the “Content-Type” to the headers. # TODO: Add "Content-Type"

    to the HTTP # headers and return the response. headers.merge!( "Content-Type" => "text/html" ) return [status, headers, body]
  12. 24.

    class FinishSentence def initialize(app) @app = app end def call(env)

    status, headers, body = @app.call(env) puts "Hello from FinishSentence" # TODO: Add " world!" to the HTTP # body and return the response. end end
  13. 25.

    Add the “ world!” to the body. # TODO: Add

    " world!" to the HTTP # body and return the response. body.push(" world!") return [status, headers, body]
  14. 26.

    Use the middleware. # TODO: Tell the app to use

    the "ContentType" # and "FinishSentence" middleware. use ContentType use FinishSentence
  15. 28.

    rack_middleware:$ rackup config.ru >> Thin web server (v1.5.0 codename Knife)

    >> Maximum connections set to 1024 >> Listening on 0.0.0.0:9292, CTRL+C to stop Hello from MyAPP Hello from FinishSentence Hello from ContentType 127.0.0.1 - - [14/Nov/2012 17:01:07] "GET / HT {
  16. 31.
  17. 32.
  18. 33.

    “Sinatra is a DSL for quickly creating web applications in

    Ruby with minimal e ort.” A “human interface” to Rack. “Domain Specific Language”
  19. 34.
  20. 38.

    The Showdown Plain ‘ol Rack Sinatra get '/' do "Hello

    world!" end class MyApp def call(env) headers = { "Content-Type" => "text/html" } [200, headers, ["Hello world!"]] end end
  21. 39.

    $ rackup config.ru Run it! Open “simple_sinatra” and edit the

    “config.ru” file. # config.ru require 'sinatra' get '/' do "Hello Sinatra!" end run Sinatra::Application
  22. 41.

    # config.ru require 'sinatra' get '/' do "Hello Sinatra!" end

    get '/hello/:name' do |n| "Hello #{n}!" end get '/wildcard/*' do request.inspect end error 404 do "OMG, 404!" end get '/breakit' do 500 end run Sinatra::Application
  23. 42.
  24. 43.

    Rails email database ajax orm csrf routes tests sessions models

    views controllers “A full-stack web application framework.” logging tasks
  25. 46.
  26. 47.
  27. 48.
  28. 49.
  29. 50.

    me

  30. 51.
  31. 52.
  32. 53.