This document discusses how Sinatra is designed to work with "friends" like Rack, allowing developers to build web applications with minimal effort. It provides examples of how to perform common tasks like CRUD operations and authentication using Sinatra and middleware like Warden. It also highlights how Sinatra applications can be easily deployed to platforms like Heroku with just a "push" and how services like Disqus can be added with minimal effort to provide commenting functionality. The document emphasizes that Sinatra focuses on being lightweight, modular and extensible while still being powerful for real-world applications.
15. Seperate apps by path
パスでアプリを分割
# in config.ru
map '/blogs' do
run App::SimpleBlog
end
map '/us' do
run App::UrlShortener
end
14/51
16. Seperate apps by host
ホスト名でアプリを分割
# in config.ru
map 'http://blogs.mysite.com/' do
run App::SimpleBlog
end
map 'http://us.mysite.com/' do
run App::UrlShortener
end
15/51
21. CRUD example
CRUDの例
Need these friends:
必須の友
shotgun
haml
sequel
20/51
22. migration
マイグレーション
# in 1281789589_create_blogs.rb
# touch `date +%s`_create_blogs.rb
Sequel.migration do
up do
create_table(:blogs) do
primary_key :id
end
end
end
21/51
23. run migration
マイグレーションの実行
sequel -m migrations -E $DATABASE_URL
22/51
24. rollback
ロールバック
# "-M 0" means to rollback all
# "-M 0" は全部をロールバックという意味
sequel -m migrations -M 0 -E $DATABASE_URL
23/51
25. app.rb
require 'rubygems'
require 'sinatra'
require 'blog'
get '/blogs/:id' do |id|
@blog = Blog[:id => id]
haml :article
end
post '/blogs/?' do
@blog = Blog.create(params[:blog])
redirect "/#{@blog.id}"
end
24/51
27. run config.ru
config.ruの実行
~/workspace/sinatra$ shotgun -s thin
== Shotgun/Thin on http://127.0.0.1:9393/
>> Thin web server (v1.2.7 codename No Hup)
>> Maximum connections set to 1024
>> Listening on 127.0.0.1:9393, CTRL+C to stop
26/51
28. Unit Test
Need these friends:
必須の友
Rack::MockRequest
Rspec | Test::Unit | Shoulda
27/51
29. Rack::MockRequest
# Mock of a Rack App
# Rack アプリケーションをモックする
req = Rack::MockRequest.new(Sinatra::Application)
# invoke HTTP methods(as in Sinatra)
# HTTPメソッドを呼び出す(Sinatraと同じ)
resp = req.get('/')
# And you get one Rack::MockResponse
# Rack::MockResponseを得る
resp.should.be.kind_of Rack::MockResponse
resp.should.be.ok
resp.body.should.match(%r{ <title>My Title</title> })
28/51
30. Authentication
認証
Need these friends:
必須の友
Warden
Rack::OpenID
29/51
31. Strategies
認証方式
No presetted strategies.
あらかじめセットされた認証方式はない
Adding new strategy is very
easy.
新しい方式を加えるが容易だ
30/51
32. Add new Strategy
# Add to strategies // 認証方式を加える
Warden::Strategies.add(:password) do
def authenticate!
u = User.auth(params['username'], params['password'])
u.nil? ? fail!("Could not log in") : success!(u)
end
end
31/51
33. Use Warden
# must enable session when use warden
# セッションの使用が必須
enable :session
# just as use a rack middleware
# Rackミドルワェアの使用と同じ
use Warden::Manager do |manager|
# assign the stategies
# 認証方式を指定する
manager.default_strategies :password
# Which Rack App to handle failure?
# 失敗したらどのRack Appをとうして処理するか
manager.failure_app = Failed
end
32/51
37. No respond_to
# in a rails controller
# different routes "/:id.:format", "/:id?format=:format"
# shares the same code
# パスが異なっていても同じコードを共有している
def show
respond_to do |format|
format.json do
# deal with json
end
format.html do
# deal with html
end
end
end
36/51
38. Only case ... when ...
# use regexp routes
# 正規表現のルーツを使う
get %r{^/blogs/:id(.json|.xml)?$} do
if params[:captures]
params[:format] = params[:captures][0][1..-1]
end
case params[:format]
when 'json'
# deal with json
else
# deal with html
end
end
37/51