More Related Content Similar to Sinatra事始め (20) More from Tomohiro Nishimura More from Tomohiro Nishimura (20) Sinatra事始め5. SINATRA ?
• Web DSL
• Rack Rails
•
7. DSL
> ruby app.rb
== Sinatra/0.9.2 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.2.2 codename I Find Your Lack of Sauce Disturbing)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop
8. RACK RAILS
• Rails
• Rack
9. RACK RAILS
• Rails
• Rack
Sinatra
10. • View /
• Haml, Erb, Builder, Sass
• Model / OR
• Sequel, DataMapper, ActiveRecord, etc
12. ROUTES
•get(‘/’) { ... ... }
•post(‘/’) { .., ... }
•put(‘/’) { ... ... }
•delete(‘/’) { ... ... }
13. ROUTES
• ‘/hi‘ Route
• URL
•
• HTTP (GET, POST, PUT, DLETE)
• get(‘/hi’) { ... } != put(‘/hi’) { ... }
14. ROUTES
• ‘/show/:id’
• /show/5 #=> params[:id] == 5
• %r|/hello/(w{3})|
• /hello/tomohiro #=> params[:captures][0] == ‘tom’
•
• get '/foo', :agent => /Songbird (d.d)[d/]*?/
15. TEMPLATE
•
• haml, erb, builder, sass
• ./views
• set :views, File.dirname(__FILE__) + '/templates'
• ./views/layout.[type]
• e.g.) ./views/layout.haml
• haml :index, :layout => false #=>
16. CONFIGURATION
• set
• set :max_length, 15 #=> options.max_length == 15
• enable, disable
• enable :sessions #=> options.sessions == true
• (development, product, test)
• configure(:production) { enable :log }
17. ERROR HANDLING
• (404 Not Found)
• not_found { ‘404 ’}
• error
• error {‘ ’}
• error(MyError) { ‘MyError : } # raise MyError
• production
18. HELPERS
• helpers
helpers do
def mr(name)
“Mr. #{name}”
end
end
•
get ‘/hello/:name’ do |name|
“hello, #{mr(name)}”
end
20. • Web
• http://www.sinatrarb.com/
• Repository
• http://github.com/sinatra/sinatra/tree/master
• Issue tracker
• https://sinatra.lighthouseapp.com/projects/9779-sinatra
• Mailing List
• http://groups.google.com/group/sinatrarb