sinatra introduction
DESCRIPTION
Ruby Tuesday 6/30 xdite's talkabout Sinatra / Rack / Rails MetalTRANSCRIPT
![Page 1: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/1.jpg)
![Page 2: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/2.jpg)
Agenda
• Sinatra Introduction
• Real Example
• Rack
• Rails Intergation
![Page 3: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/3.jpg)
自我介紹
• http://twitter.com/xdite
• http://blog.xdite.net
• Rails Developer
![Page 4: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/4.jpg)
Sinatra is ...
• ruby web framework library
• not MVC ( Model-View-Controller )
• DSL for defining RESTful HTTP actions
![Page 5: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/5.jpg)
RAILS LINGOORM , MVC , Testing , Mail ,
Generator, RJS, I18n, Template , Engine .....
![Page 6: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/6.jpg)
SINATRA LINGOGET / POST / PUT / DELETE , request
paramenters, response, views , helpers , configure , rack
![Page 7: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/7.jpg)
Single file web-app
![Page 8: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/8.jpg)
Single file web-app
![Page 9: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/9.jpg)
Hello World!
![Page 10: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/10.jpg)
HTTP actions
![Page 11: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/11.jpg)
Routes
![Page 12: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/12.jpg)
Views
![Page 13: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/13.jpg)
Helpers
![Page 14: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/14.jpg)
Filter
![Page 15: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/15.jpg)
Configure
![Page 16: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/16.jpg)
適用場合
![Page 17: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/17.jpg)
• Web Service / API
• Tiny Web Application
•Rails Metal
![Page 18: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/18.jpg)
Web Service / API
![Page 19: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/19.jpg)
Requests per second: 81.09 [#/sec] (mean)Time per request: 12.332 [ms] (mean)
Requests per second: 450.56 [#/sec] (mean)Time per request: 2.219 [ms] (mean)
Rails action
Sinatra
(Ubuntu 8.04, Intel Atom N270 @1.60GHz)
![Page 20: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/20.jpg)
Tiny Web Application
![Page 21: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/21.jpg)
• simple
• cherry pick
• ActiveRecord / Datamapper
• Prototype / jQuery
![Page 22: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/22.jpg)
![Page 23: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/23.jpg)
追蹤 twitter 上痛恨 IE6 的人
![Page 24: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/24.jpg)
![Page 25: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/25.jpg)
• CodeIgniter - PHP framework
• 使用 Twitter API 追蹤特定關鍵字並展示
• LOC : 1000+
• NOT support CJK
![Page 26: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/26.jpg)
require 'rubygems'require 'sinatra'require 'twitter_search'get '/' do @client = TwitterSearch::Client.new 'politweets' @results = @client.query :q => '@MrIE6' erb :homeend
10 行內收工..
![Page 27: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/27.jpg)
• ORM
• Paginate
![Page 28: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/28.jpg)
多寫了 40 行左右
![Page 29: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/29.jpg)
![Page 30: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/30.jpg)
Rails Metal with Sinatra
![Page 31: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/31.jpg)
Core Code Middleware Mongrel
rack 是一種 middleware
Rack provides a minimal interface between webservers supporting Ruby and Ruby frameworks
![Page 32: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/32.jpg)
![Page 33: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/33.jpg)
![Page 34: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/34.jpg)
http ProfilingMiddleware
SecurityMiddleware
CachingMiddleware
Handlerfor Rack
Core Code
![Page 35: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/35.jpg)
$ rake middleware
![Page 36: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/36.jpg)
Scaling Rails - Metal
• 使用 Rack 製作一個超級快的 action
• 將 Sinatra 當作一個 Metal app 使用
![Page 37: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/37.jpg)
Performance
![Page 38: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/38.jpg)
class Poller < Rails::Rack::Metal def call(env) if env["PATH_INFO"] =~ /^\/poller/ [[200], {"Content-Type" => "text/html"}, "Hello, World!"] else [[404], {"Content-Type" => "text/html"}, "Not Found"] end endend
class OldPollerController < ApplicationController def poller render :text => "Hello World!" endend
後者比前者快了 25 倍
![Page 39: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/39.jpg)
Requests per second: 81.09 [#/sec] (mean)Time per request: 12.332 [x`x] (mean)
Requests per second: 163.75 [#/sec] (mean)Time per request: 6.107 [ms] (mean)
Rails action
Metal + Sinatra
(Ubuntu 8.04, Intel Atom N270 @1.60GHz)
一般狀況
![Page 40: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/40.jpg)
Q & A
![Page 41: Sinatra Introduction](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7b8c74a7959c9688b4764/html5/thumbnails/41.jpg)
Resources
• Scaling Rails : Episode #14 Rack
• RailsCast: Episode #251 Rack Middleware
• Pragprog.com : Classy Web Development with Sinata
• Github / xdite : tweesage-message-wall