Permalink
Browse files

add code examples

  • Loading branch information...
1 parent 3aa1bd5 commit 6535192787a2fe6d3b58cdd76ed5af22402ae5bc @stevenwilkin committed Mar 27, 2012
Showing with 68 additions and 0 deletions.
  1. +5 −0 examples/01_classic_style.rb
  2. +22 −0 examples/03_rest_like.rb
  3. +21 −0 examples/04_testing.rb
  4. +20 −0 examples/05_middleware.rb
@@ -0,0 +1,5 @@
+require 'sinatra'
+
+get '/' do
+ "Hello World!"
+end
@@ -0,0 +1,22 @@
+# 03_rest_like.rb
+require 'sinatra'
+
+get '/lentils' do
+ "get all the lentils"
+end
+
+post '/lentils' do
+ "create a lentil"
+end
+
+get '/lentils/:id' do
+ "get lentil: #{params[:id]}"
+end
+
+put "/lentils/:id" do
+ "update lentil: #{params[:id]}"
+end
+
+delete "/lentils/:id" do
+ "delete lentil: #{params[:id]}"
+end
@@ -0,0 +1,21 @@
+# 04_testing.rb
+require 'sinatra/base'
+require 'minitest/autorun'
+require 'rack/test'
+
+class BelfastRuby < Sinatra::Base
+ get '/' do; end
+end
+
+class BelfastRubyTest < MiniTest::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ BelfastRuby
+ end
+
+ def test_get_root
+ get '/'
+ assert_equal 200, last_response.status, 'Should GET /'
+ end
+end
@@ -0,0 +1,20 @@
+# 05_middleware.rb
+require 'sinatra'
+
+class Runtime
+ def initialize(app)
+ @app = app
+ end
+ def call(env)
+ start_time = Time.now
+ status, headers, body = @app.call(env)
+ headers['X-Runtime'] = "%0.6f" % (Time.now - start_time)
+ [status, headers, body]
+ end
+end
+
+use Runtime
+
+get '/' do
+ "Hello World!"
+end

0 comments on commit 6535192

Please sign in to comment.