Beginner's Sinatra

3,884 views

Published on

kanazawa.rb #02 (2012/09/22) の発表資料。Sinatra 概要。

Published in: Technology
  • Be the first to comment

Beginner's Sinatra

  1. 1. Beginner’s Sinatra Minimal Framework in Ruby2012/09/22 - kanazawa.rb #2
  2. 2. Web System Web serverApplication serverDatabase server
  3. 3. Web System Client Web serverApplication serverDatabase server
  4. 4. exampleWeb System ApacheTomcat (java) PostgreSQL
  5. 5. exampleWeb System nginx Rails (Ruby) MySQL
  6. 6. exampleWeb System thinSinatra (Ruby) MySQL
  7. 7. exampleWeb System thinSinatra (Ruby) MySQL
  8. 8. Sinatra
  9. 9. Sinatrahttp://www.sinatrarb.com
  10. 10. Sinatrahttp://www.sinatrarb.com Sinatra is a DSL for quickly creating web applications in Ruby
  11. 11. Sinatrahttp://www.sinatrarb.com Sinatra is a DSL for quickly creating web applications in Ruby
  12. 12. Hello world!$ gem install sinatra$ vi myapp.rb$ ruby myapp.rb
  13. 13. Hello world!$ gem install sinatra$ vi myapp.rb$ ruby myapp.rb
  14. 14. Hello world!require sinatraget / do Hello world!end
  15. 15. Hello world!require sinatraget / do Hello world!end thats all
  16. 16. Hello world!require sinatraget / do Hello world!end
  17. 17. Hello world! require sinatraRequest get / do Hello world! end
  18. 18. Hello world! require sinatraRequest get / do Hello world! end Handling & Response
  19. 19. Hello world!$ gem install sinatra$ vi myapp.rb$ ruby myapp.rb
  20. 20. Hello world!
  21. 21. Hello world!
  22. 22. MVCModelViewController
  23. 23. MVCModelViewController
  24. 24. MVCModelView SinatraController
  25. 25. MVCModelViewController
  26. 26. MVC ActiveRecord DataMapper SequelModelViewController
  27. 27. MVC ActiveRecord DataMapper SequelModelView erbController haml Builder
  28. 28. MVC ActiveRecord DataMapper SequelModelView erbController haml Builder As you like :)
  29. 29. SinatraMinimal “Routing” Framework in Ruby handling responserequest handling response handling response
  30. 30. Routing
  31. 31. How to routing1.HTTP method + URL path2.Conditions3.Filters4.Passing5.Halt & Error
  32. 32. 1. HTTP method + URL pathHTTP method get post put delete
  33. 33. 1. HTTP method + URL pathget / do ...post / do ...put / do ...delete / do ... first match
  34. 34. 1. HTTP method + URL pathURL path pattern regular expressions
  35. 35. 1. HTTP method + URL pathget /path/ do ... # => /path/get /path/:dir/:file do ... # => /path/hoge/fugaget /path/*.* do ... # => /path/hoge.xmlget %r{/path/[w]+} do ... # => /path/hoge first match
  36. 36. 2. Conditionsuser agenthost namemime type (≒HTTP Accept)custom conditions
  37. 37. 2. Conditionsget /, :agent => /MSIE/ do ...get /, :host_name => /^admin./ do ...get /, :provides => :rss do ... first match
  38. 38. 2. Conditionsset(:random) { |val| condition { rand <= val } }get /, :random => 0.2 do ... # => 20%get / do ... # => 80% first match
  39. 39. 3. FiltersBeforeAfter
  40. 40. 3. Filtersbefore /usr/* do ...before / do ...get /usr/* do ...get * do ...after /usr/* do ...after do ... # => after * do ...
  41. 41. 3. Filters /usr/hogebefore /usr/* do ...before / do ...get /usr/* do ...get * do ...after /usr/* do ...after do ... # => after * do ...
  42. 42. 3. Filters /usr/hogebefore /usr/* do ... 1before / do ...get /usr/* do ... 2get * do ...after /usr/* do ... 3after do ... # => after * do ...
  43. 43. 3. Filters /usr/hoge /before /usr/* do ... 1before / do ...get /usr/* do ... 2get * do ...after /usr/* do ... 3after do ... # => after * do ...
  44. 44. 3. Filters /usr/hoge /before /usr/* do ... 1before / do ... 1get /usr/* do ... 2get * do ... 2after /usr/* do ... 3after do ... # => after * do ... 3
  45. 45. 3. Filters /usr/hoge / /fugabefore /usr/* do ... 1before / do ... 1get /usr/* do ... 2get * do ... 2after /usr/* do ... 3after do ... # => after * do ... 3
  46. 46. 3. Filters /usr/hoge / /fugabefore /usr/* do ... 1before / do ... 1get /usr/* do ... 2get * do ... 2 1after /usr/* do ... 3after do ... # => after * do ... 3 2
  47. 47. 4. Passingget * do pass if rand <= 0.2; # xxxendget * do # xxxend first match
  48. 48. 4. Passingget * do pass if rand <= 0.2; # xxxendget * do # xxxend first match
  49. 49. 5. Halt & Errorerror 403 do Access Forbiddenendbefore /secret/* do halt 403 unless authorizedend
  50. 50. 5. Halt & Errorerror 403 do Access Forbiddenendbefore /secret/* do halt 403 unless authorizedend
  51. 51. 5. Halt & Errorerror 404 do File Not Foundendbefore /devel/* do halt xxxend
  52. 52. Request
  53. 53. URL pattern (:xxx)get /path/:dir/:file do # => /path/hoge/fuga params[:dir] # => "hoge" params[:file] # => "fuga"end
  54. 54. URL pattern (*)get /path/*.* do # => /path/hoge/fuga.xml params[:splat] # => [ "hoge/fuga", "xml" ]endget /path/*.* do |path, ext| # => /path/hoge/fuga.xml path # => "hoge/fuga" ext # => "xml"end
  55. 55. URL regular expressionget %r{/path/([w]+)} do # => /path/hoge params[:capture] # => [ "hoge" ]endget %r{/path/([w]+)} do |cap| # => /path/hoge cap # => [ "hoge" ]end
  56. 56. HTTP Get query"/?abc=hoge&def=fuga"get * do params[:abc] # => "hoge" params[:def] # => "fuga"end
  57. 57. HTTP Post data<input name="abc" value="hoge"><input name="def" value="fuga">post * do params[:abc] # => "hoge" params[:def] # => "fuga"end
  58. 58. request objectget * do request.cookies # => cookie hash request.xhr? # => is ajax request (boolean) request.methods # => any more!end
  59. 59. Response
  60. 60. Response type1.Objects2.Template
  61. 61. 1. ObjectStringFixnum (as HTTP Status code)Array [status (Fixnum), response body (responds to #each)] [status (Fixnum), headers (Hash), response body (responds to #each)]
  62. 62. 2. Templatehamlerbbuilder
  63. 63. 2. Templatehamlerbbuildersasscoffee-script
  64. 64. 2. Templateget / do erb :index # => ./views/index.erbend
  65. 65. 2. Templateget / do erb :index # => ./views/index.erbend
  66. 66. 2. Templateget / do erb :index # => ./views/index.erbend config OK
  67. 67. 2. Templateget / do @attr1 = "val1" @attr2 = "val2" erb :indexend<%= @attr1 %><%= @attr2 %>
  68. 68. 2. Templateget / do erb :index, :locals => { :attr1 => "val1", :attr2 => "val2" }end<%= attr1 %><%= attr2 %>
  69. 69. Conclusion
  70. 70. SinatraMinimal routing frameworkfor quickly creating web applications in Rubycan using any templates
  71. 71. Thank youTomokazu Kiyoharahttp://facebook.com/tomokazu.kiyoharahttp://twitter.com/kiyohara

×