Browse files

Adding more sinatra stuff & middleware examples

  • Loading branch information...
1 parent b6f71ca commit f0ca70bfee3dd040de87200269e227eeb6a61fcf @jc00ke committed Mar 2, 2010
View
17 examples/middleware/app.rb
@@ -0,0 +1,17 @@
+require 'sinatra/base'
+require 'irack'
+require 'gsubhellogoodbye'
+
+class Middleware < Sinatra::Base
+ use I::Rack
+ use GsubHelloGoodbye
+
+ get '/' do
+ "hello"
+ end
+
+ get '/hello' do
+ "hello hello HELLO!"
+ end
+
+end
View
4 examples/middleware/config.ru
@@ -0,0 +1,4 @@
+require 'rubygems'
+require 'app'
+Middleware.run! :host => 'localhost',
+ :port => 4567
View
19 examples/middleware/gsubhellogoodbye.rb
@@ -0,0 +1,19 @@
+require 'rack'
+
+class GsubHelloGoodbye
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ if env["PATH_INFO"] == '/hello'
+ status, headers, response = @app.call(env)
+ body = response.first.gsub!(/hello/i, 'goodbye')
+ [status, headers, body]
+ else
+ @app.call(env)
+ end
+ end
+
+end
View
21 examples/middleware/irack.rb
@@ -0,0 +1,21 @@
+require 'rack'
+
+module I
+
+ class Rack
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ if env["PATH_INFO"] == "/WMD"
+ ["404 Not Found", {"Content-Type" => "text/plain", "Content-Length" => "0"}, []]
+ else
+ @app.call(env)
+ end
+ end
+
+ end
+
+end
View
52 middleware/middleware.md
@@ -0,0 +1,52 @@
+!SLIDE
+# Did I mention... #
+## It's built on Rack ##
+
+[http://rack.rubyforge.org](http://rack.rubyforge.org/)
+
+So, you get:
+* middleware
+* Rack::Test
+
+!SLIDE center
+# Middleware #
+Apps that change the request or response.
+* I::Rack - [from CodeRack competition](http://coderack.org/users/haruki_zaemon/entries/50-irack)
+* GsubHelloGoodbye (I just made this up)
+
+!SLIDE incremental
+ $ curl localhost:4567/
+ hello
+
+ $ curl -i localhost:4567/WMD
+ HTTP/1.1 404 Not Found Not Found
+ Content-Type: text/plain
+ Content-Length: 0
+ Connection: keep-alive
+ Server: thin 1.2.6 codename Crazy Delicious
+
+ $ curl localhost:4567/hello
+ goodbye goodbye goodbye!
+
+!SLIDE
+ @@@ ruby
+ require 'sinatra/base'
+ require 'irack'
+ require 'gsubhellogoodbye'
+
+ class Middleware < Sinatra::Base
+ use I::Rack
+ use GsubHelloGoodbye
+
+ get '/' do
+ "hello"
+ end
+
+ get '/hello' do
+ "hello hello HELLO!"
+ end
+
+ end
+
+!SLIDE
+# You can see how I did that in examples/middleware #
View
11 more_sinatra/more_sinatra.md
@@ -0,0 +1,11 @@
+!SLIDE incremental
+# Some other things in Sinatra #
+
+* helpers
+* filters
+* halting
+* passing
+* configuration
+* errors
+* mime types
+
View
10 showoff.json
@@ -1,6 +1,8 @@
[
- { "section" : "title" },
- { "section" : "about_me" },
- { "section" : "sinatra" },
- { "section" : "haml" }
+ { "section" : "title" },
+ { "section" : "about_me" },
+ { "section" : "sinatra" },
+ { "section" : "haml" },
+ { "section" : "more_sinatra" },
+ { "section" : "middleware" }
]

0 comments on commit f0ca70b

Please sign in to comment.