Sinatra and friends

3,539 views

Published on

My presentation on Ruby Kaigi 2010, with title "Rails to Siantra: What's ready"

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,539
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
30
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Sinatra and friends

  1. 1. Rails to Sinatra What's ready Jiang Wu
  2. 2. Is Mr. Tsuyoshikawa Here? 吉川さんは着たの か。 1/51
  3. 3. 『Sinatra 1.0 の世界 にようこそ』 2/51
  4. 4. Minimal Effort 最小限の労力 require 'rubygems' require 'sinatra' get '/' do "Hello, world!" end 3/51
  5. 5. It's unfair 不公平 4/51
  6. 6. over one 5/51
  7. 7. The answer is friends 「友達」がカギ 6/51
  8. 8. Sinatra is designed to need friends Sinatraは友達を前提に設計されている No Presets 設定不要 Pure Ruby ピュアRuby Rack-lover Rack好き 7/51
  9. 9. Fight with our friends! 友と闘え! 8/51
  10. 10. Rack is the best friend Rackは最高の友達 9/51
  11. 11. Middlewares in Sinatra Sinatraでのミドルウェア Rack::Session::Cookie Rack::CommonLogger Rack::MethodOverride 10/51
  12. 12. rack-contrib Rack::Profiler Rack::JSON-P Rack::Recaptcha Rack::MailExceptions (and more) 11/51
  13. 13. Rack::Cache 12/51
  14. 14. Rack::URLMap 13/51
  15. 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. 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
  17. 17. Directory Structure(1) ディレクトリ構成(1) - multi/ - apps/ | simple_blog_app.rb | url_shortener_app.rb - views/ + simple_blog/ + url_shortener/ | config.ru 16/51
  18. 18. Directory Structure(2) ディレクトリ構成(2) - multi/ - simple_blog/ | app.rb | config.ru + views/ - url_shortener/ | app.rb | config.ru + views/ | config.ru 17/51
  19. 19. Rack is awesome! Rack最高! 18/51
  20. 20. Some common tasks 基本タスク CRUD Unit Test Authentication Ajax 19/51
  21. 21. CRUD example CRUDの例 Need these friends: 必須の友 shotgun haml sequel 20/51
  22. 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. 23. run migration マイグレーションの実行 sequel -m migrations -E $DATABASE_URL 22/51
  24. 24. rollback ロールバック # "-M 0" means to rollback all # "-M 0" は全部をロールバックという意味 sequel -m migrations -M 0 -E $DATABASE_URL 23/51
  25. 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
  26. 26. config.ru require 'app' require 'sequel' DB = Sequel.connect ENV['DATABASE_URL'] class Blog < Sequel::Model end run Sinatra::Application 25/51
  27. 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. 28. Unit Test Need these friends: 必須の友 Rack::MockRequest Rspec | Test::Unit | Shoulda 27/51
  29. 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. 30. Authentication 認証 Need these friends: 必須の友 Warden Rack::OpenID 29/51
  31. 31. Strategies 認証方式 No presetted strategies. あらかじめセットされた認証方式はない Adding new strategy is very easy. 新しい方式を加えるが容易だ 30/51
  32. 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. 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
  34. 34. OpenID and OAuth warden-openid provides warden-oauth provides 33/51
  35. 35. Ajax Need these friends: 必須の友 jQuery 34/51
  36. 36. Client-side code $.post('/blogs', { title: 'new title', content: '.. some content' }); // with jquery-form $('#form').ajaxSubmit(); 35/51
  37. 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. 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
  39. 39. Web services may help 便利なサービス 38/51
  40. 40. Heroku Minimal effort to deploy app 最小限の労力でアプリをデプロ する 39/51
  41. 41. just a "push" "push"するだけ wujiang@wujiang-laptop:~/jiangwu.net$ git push heroku master Counting objects: 7, done. Delta compression using up to 2 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 1.03 KiB, done. Total 4 (delta 2), reused 0 (delta 0) -----> Heroku receiving push -----> Rack app detected Compiled slug size is 340K -----> Launching...... done http://www.jiangwu.net deployed to Heroku 40/51
  42. 42. no need to setup system by yourself セットアップ不要 41/51
  43. 43. Disqus Minimal effort of comment system 最小限の労力でコメントシステ ムを作る 42/51
  44. 44. add commenting system in one minute 瞬時にコメント機能を追加 <section class="comments"> ... <script type="text/javascript" src="http://disqus.com/forums/wujiang/embed.js"> </script> ... </section> 43/51
  45. 45. Loading 読み込み中 44/51
  46. 46. Loading Complete 読み込み完了 45/51
  47. 47. Light but powerful 軽くてパワフル 46/51
  48. 48. Focus 専念 47/51
  49. 49. Real Business 真の用事 48/51
  50. 50. Make friends actively 友達をたくさん作ろう! 49/51
  51. 51. Questions? 質問どうぞ 50/51
  52. 52. Thank you! Especially thanks Masayoki TakaHashi and Eito Katagiri for translations! http://jiangwu.net @mastewujiang 51/51

×