Slide 1

Slide 1 text

Building Web Apps with Rack and Sinatra @blacktm

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Things You’ll Need ✔ gem install thin ✔ gem install sinatra ✔ gem install heroku ✔ Project files and notes at blacktm.com

Slide 4

Slide 4 text

HTML CSS JS Rails / Sinatra Ruby Deploy

Slide 5

Slide 5 text

Web Server HTTP Request Middleware Rack Your App

Slide 6

Slide 6 text

Web Server HTTP Request Middleware Rack Your App

Slide 7

Slide 7 text

What is Rack? A modular web server interface. (Connects your Ruby app to the web.)

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

A common interface.

Slide 10

Slide 10 text

Your App Rack Thin WEBrick Mongrel CGI

Slide 11

Slide 11 text

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.”

Slide 12

Slide 12 text

$ 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

Slide 13

Slide 13 text

http://0.0.0.0:9292 #=> Hello world! http://0.0.0.0:9292/there/isnt/a/route/here #=> Hello world! A very basic server.

Slide 14

Slide 14 text

Who cares? ✔ You just built a web app. Damn. ✔ Important in understanding (and building) web frameworks (e.g. Rails) ✔ Middleware!

Slide 15

Slide 15 text

HTTP Request Middleware Rack Your App Web Server

Slide 16

Slide 16 text

HTTP Request Middleware Rack Your App Web Server

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Rack’s Default Middleware (in development mode): Rack::CommonLogger Rack::ShowExceptions Rack::Lint

Slide 20

Slide 20 text

# app.rb class MyApp def call(env) puts "Hello from MyAPP" return [200, {}, ["Hello"]] end end No “Content-Type”

Slide 21

Slide 21 text

Solution? Middleware!

Slide 22

Slide 22 text

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”

Slide 23

Slide 23 text

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]

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Add the “ world!” to the body. # TODO: Add " world!" to the HTTP # body and return the response. body.push(" world!") return [status, headers, body]

Slide 26

Slide 26 text

Use the middleware. # TODO: Tell the app to use the "ContentType" # and "FinishSentence" middleware. use ContentType use FinishSentence

Slide 27

Slide 27 text

Run it! $ rackup config.ru http://0.0.0.0:9292 127.0.0.1 - - [13/Oct/2012 21:18:58] "GET / HTTP/1.1" 200 12 0.0009

Slide 28

Slide 28 text

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 {

Slide 29

Slide 29 text

What’s wrong with this?

Slide 30

Slide 30 text

What’s wrong with this?

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

“Sinatra is a DSL for quickly creating web applications in Ruby with minimal e ort.” A “human interface” to Rack. “Domain Specific Language”

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

HTTP Request Middleware Rack Your App Web Server

Slide 36

Slide 36 text

HTTP Request Middleware Rack Your App Web Server

Slide 37

Slide 37 text

HTTP Request Middleware Rack Your App Sinatra Web Server

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

$ 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

Slide 40

Slide 40 text

What else can do?

Slide 41

Slide 41 text

# 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

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Rails email database ajax orm csrf routes tests sessions models views controllers “A full-stack web application framework.” logging tasks

Slide 44

Slide 44 text

Middleware Rack Your App Rails Web Server Middleware Rack Your App Sinatra Web Server

Slide 45

Slide 45 text

Middleware Rack Your App Web Server Middleware Rack Your App Sinatra Web Server RAILS

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

me

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Rack + Sinatra + DataMapper + Heroku Messages App

Slide 55

Slide 55 text

Thanks! @blacktm