Sinatra Ryan Farnell (@criscokid)
SinatraRyan Farnell (@criscokid)
What is Sinatra?
Micro Web Framework
• Built on top of Rack
• Uses RESTful thinking
• URL’s are king
Features
• Routing to code blocks
• Static Files
• Views/Layouts
• Filters
• Helpers
Routing To Blocks
• Routes made up of 3 basic parts
• HTTP Verb
• Match Expression
• Code block
HTTP Verbs
POST
DELETEPUT
GET
Demo
Serving Public Files
• If no matching URL was found, Sinatra checks the public folder
• Public folder can be changed by setting :public
set :public,'path/to/file/static'
Views• Views are mixes of code and markup
• Sinatra works with ERB, HAML, and Builder by default
• Views are kept in a folder at the root of the site called “views”
• View folder can be changed by setting :views
set :views,,'path/to/file/static'
Layout
• Layouts are like templates for all your pages.
• By default looks for a file called “layout” in views with the same extension as the view you are rendering.
• Default name can be overridden.
Demo
Filters
• Filters run before all routes.
• They can modify the Request and Response object.
• Any instance variable created in the before filter is available in the route.
Helpers
• Small methods that you find useful.
• Available within the route and within the views.
Demo
Example Apps
Git-Wikihttp://atonie.org/2008/02/git-wiki
Rif-Grafhttp://rifgraf.heroku.com/
Github-Serviceshttp://github.com/pjhyett/github-services/tree/master
Deployment
• Any server compatible with Rack
• Ruby Servers
• Mongrel
• Thin
• Webrick
• Apache running mod_rack (Phusion Passenger)
• FastCGI Server
• Apache
• Lighttpd
http://www.sinatrarb.com/
Q&A