SlideShare a Scribd company logo
Beginner’s Sinatra
                                Minimal Framework in Ruby




2012/09/22 - kanazawa.rb #2
Web System

   Web server



Application server



Database server
Web System
                     Client
   Web server



Application server



Database server
example
Web System

  Apache



Tomcat (java)



 PostgreSQL
example
Web System

    nginx



 Rails (Ruby)



   MySQL
example
Web System

     thin



Sinatra (Ruby)



   MySQL
example
Web System

     thin



Sinatra (Ruby)



   MySQL
Sinatra
Sinatra

http://www.sinatrarb.com
Sinatra

http://www.sinatrarb.com



        Sinatra is a DSL for
          quickly creating
     web applications in Ruby
Sinatra

http://www.sinatrarb.com



        Sinatra is a DSL for
         quickly creating
     web applications in Ruby
Hello world!


$ gem install sinatra

$ vi myapp.rb

$ ruby myapp.rb
Hello world!


$ gem install sinatra

$ vi myapp.rb

$ ruby myapp.rb
Hello world!

require 'sinatra'


get '/' do
 'Hello world!'
end
Hello world!

require 'sinatra'


get '/' do
 'Hello world!'
end
                    that's all
Hello world!

require 'sinatra'


get '/' do
 'Hello world!'
end
Hello world!

          require 'sinatra'
Request

          get '/' do
           'Hello world!'
          end
Hello world!

          require 'sinatra'
Request

          get '/' do
           'Hello world!'
          end          Handling & Response
Hello world!


$ gem install sinatra

$ vi myapp.rb

$ ruby myapp.rb
Hello world!
Hello world!
MVC



Model
View
Controller
MVC



Model
View
Controller
MVC



Model
View           Sinatra
Controller
MVC



Model
View
Controller
MVC

               ActiveRecord
               DataMapper
                  Sequel
Model
View
Controller
MVC

               ActiveRecord
               DataMapper
                  Sequel
Model
View
                     erb
Controller          haml
                   Builder
MVC

                ActiveRecord
                DataMapper
                   Sequel
Model
View
                     erb
Controller          haml
                   Builder


        As you like :)
Sinatra

Minimal “Routing” Framework in Ruby



            handling     response

request     handling     response

            handling     response
Routing
How to routing


1.HTTP method + URL path
2.Conditions
3.Filters
4.Passing
5.Halt & Error
1. HTTP method + URL path


HTTP method
  get
  post
  put
  delete
1. HTTP method + URL path

get '/' do ...

post '/' do ...

put '/' do ...

delete '/' do ...
                     first match
1. HTTP method + URL path



URL path
  pattern
  regular expressions
1. HTTP method + URL path

get '/path/' do ... # => '/path/'

get '/path/:dir/:file' do ... # => '/path/hoge/fuga'

get '/path/*.*' do ... # => '/path/hoge.xml'

get %r{/path/[w]+} do ... # => '/path/hoge'
                                           first match
2. Conditions


user agent
host name
mime type (≒HTTP Accept)
custom conditions
2. Conditions


get '/', :agent => /MSIE/ do ...

get '/', :host_name => /^admin./ do ...

get '/', :provides => :rss do ...

                                           first match
2. Conditions


set(:random) { |val| condition { rand <= val } }


get '/', :random => 0.2 do ... # => 20%
get '/' do ... # => 80%

                                          first match
3. Filters



Before
After
3. Filters


before '/usr/*' do ...
before '/' do ...
get '/usr/*' do ...
get '*' do ...
after '/usr/*' do ...
after do ... # => after '*' do ...
3. Filters

                                 /usr/hoge

before '/usr/*' do ...
before '/' do ...
get '/usr/*' do ...
get '*' do ...
after '/usr/*' do ...
after do ... # => after '*' do ...
3. Filters

                                 /usr/hoge

before '/usr/*' do ...               1
before '/' do ...
get '/usr/*' do ...                  2
get '*' do ...
after '/usr/*' do ...                3
after do ... # => after '*' do ...
3. Filters

                                 /usr/hoge   /

before '/usr/*' do ...               1
before '/' do ...
get '/usr/*' do ...                  2
get '*' do ...
after '/usr/*' do ...                3
after do ... # => after '*' do ...
3. Filters

                                 /usr/hoge   /

before '/usr/*' do ...               1
before '/' do ...                            1
get '/usr/*' do ...                  2
get '*' do ...                               2
after '/usr/*' do ...                3
after do ... # => after '*' do ...           3
3. Filters

                                 /usr/hoge   /   /fuga

before '/usr/*' do ...               1
before '/' do ...                            1
get '/usr/*' do ...                  2
get '*' do ...                               2
after '/usr/*' do ...                3
after do ... # => after '*' do ...           3
3. Filters

                                 /usr/hoge   /   /fuga

before '/usr/*' do ...               1
before '/' do ...                            1
get '/usr/*' do ...                  2
get '*' do ...                               2    1
after '/usr/*' do ...                3
after do ... # => after '*' do ...           3    2
4. Passing

get '*' do
  pass if rand <= 0.2;
  # xxx
end
get '*' do
  # xxx
end
                                 first match
4. Passing

get '*' do
  pass if rand <= 0.2;
  # xxx
end
get '*' do
  # xxx
end
                                 first match
5. Halt & Error


error 403 do
  'Access Forbidden'
end
before '/secret/*' do
  halt 403 unless authorized
end
5. Halt & Error


error 403 do
  'Access Forbidden'
end
before '/secret/*' do
  halt 403 unless authorized
end
5. Halt & Error


error 404 do
  'File Not Found'
end
before '/devel/*' do
  halt 'xxx'
end
Request
URL pattern (:xxx)



get '/path/:dir/:file' do # => '/path/hoge/fuga'
  params[:dir] # => "hoge"
  params[:file] # => "fuga"
end
URL pattern (*)

get '/path/*.*' do # => '/path/hoge/fuga.xml'
  params[:splat] # => [ "hoge/fuga", "xml" ]
end
get '/path/*.*' do |path, ext| # => '/path/hoge/fuga.xml'
  path # => "hoge/fuga"
  ext # => "xml"
end
URL regular expression


get %r{/path/([w]+)} do # => '/path/hoge'
  params[:capture] # => [ "hoge" ]
end
get %r{/path/([w]+)} do |cap| # => '/path/hoge'
  cap # => [ "hoge" ]
end
HTTP Get query


"/?abc=hoge&def=fuga"


get '*' do
  params[:abc] # => "hoge"
  params[:def] # => "fuga"
end
HTTP Post data

<input name="abc" value="hoge">
<input name="def" value="fuga">


post '*' do
  params[:abc] # => "hoge"
  params[:def] # => "fuga"
end
request object


get '*' do
  request.cookies # => cookie hash
  request.xhr? # => is ajax request (boolean)
  request.methods # => any more!
end
Response
Response type



1.Objects
2.Template
1. Object


String
Fixnum (as HTTP Status code)
Array
   [status (Fixnum), response body (responds to #each)]
   [status (Fixnum), headers (Hash), response body (responds to #each)]
2. Template

haml
erb
builder
2. Template

haml
erb
builder
sass
coffee-script
2. Template



get '/' do
 erb :index # => ./views/index.erb
end
2. Template



get '/' do
 erb :index # => ./views/index.erb
end
2. Template



get '/' do
 erb :index # => ./views/index.erb
end
                    config OK
2. Template

get '/' do
  @attr1 = "val1"
  @attr2 = "val2"
  erb :index
end


<%= @attr1 %><%= @attr2 %>
2. Template


get '/' do
  erb :index, :locals => { :attr1 => "val1", :attr2 => "val2" }
end




<%= attr1 %><%= attr2 %>
Conclusion
Sinatra


Minimal   routing framework

for   quickly creating web applications in Ruby

can using   any templates
Thank you




Tomokazu Kiyohara
http://facebook.com/tomokazu.kiyohara
http://twitter.com/kiyohara

More Related Content

Beginner's Sinatra

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n