Upload
nitesh-kumar
View
1.659
Download
1
Embed Size (px)
DESCRIPTION
Introduction to Sinatra
Citation preview
SINATRANitesh Kumar
Member of technical staff11.12.12
ROADMAP
What is sinatra?
Why sinatra?
Flexibility with sinatra
Singing with sinatra
File hierarchy and naming convention
Features
Sinatra with ActiveRecord
WHAT IS SINATRA?
A micro-framework for web applications
A ruby domain specific language
A ruby “gem”
WHY SINATRA?
Lightweight
Nicely pluggable within other application
Good tool for creating REST-like interface
Support many different template syntaxes.
SINATRA ON RAILS
#in routes.rbmatch “/blog” => MySinatraBlogApp, :anchor =>
false
#Sinatra appClass MySinatraBlogApp < Sinatra::Base
#this now will match /blog/archivesget “/archives” do
“My old posts”end
end
FLEXIBILITY WITH SINATRA :
Template engine ERB, HAML, Builder..
ORM ActiveRecord, DataMapper, Sequel
JS library jQuery, Prototype, YUI, Dojo
Testing tool Test::unit, Rspec, Bacon
LET’S START THE SHOW
gem install sinatra
ruby hello-world.rb
SINGING WITH “SINATRA”
#sample.rb
require ‘rubygems’require ‘sinatra’
get ‘ / ’ do“Why this kolavari kolavari kolavari
di..”end
SO
Every url you wants to respond to goes in your .rb file
Pages themselves goes in /views as .erb files Wrapping layout goes in /views/layout.erb Static files goes in public
|-- beta.rb|--public| `--stylesheets| `--shared.css`-- views |--index.erb |--layout.erb
FEATURES: [ ROUTING ]
Routing to code block
get ‘ / ‘ do“Hello world!”
end“ block ”
Whatever is returned from the block is sent to the browser
FEATURES: [ VIEWS ]
#beta.rb
require ‘rubygems’
require ‘sinatra’
get ‘ / ’ do
erb: index
end
#views/index.erb
<h3> Welcome the my website </h3>
<p>I’m being developed . Thanks for stopping by.</p>
FEATURES: [ LAYOUTS ]
#views/layout.rb
<!DOCTYPE html>
<html>
<head>
<title>Beta Info</title>
</head>
<body>
<%= yield %>
</body>
</html>
#views/index.erb
<h3> Welcome the my website </h3>
<p>I’m being developed . Thanks for stopping by.</p>
FEATURES: [FILTERS]
beforebefore do
MyStore.connect unless MyStore.connected? end
get '/' do @list = MyStore.find(:all) erb :index
end
afterafter do
MyStore.disconnect end
FEATURES: [HELPERS]
helpers dodef bar(name)
"#{name}bar“end
end
get '/:name' dobar(params[:name])
end
DYNAMIC URLS?
#dynamic.rbrequire ‘rubygems’require ‘sinatra’
get ‘ / ’ do“This is homepage.”
end
get ‘/:name’ do‘Hello’ + params[:name] + ‘!’
end
SINATRA WITH ACTIVERECORD
require ‘rubygems’require ‘sinatra’require ‘active_record’
class Article < ActiveRecord::Baseend
get ‘ / ’ doArticle.establish_connection(
:adapter => “sqlite3”,:database => “hw.db”
)Article.first.title
end
REFERENCES
http://sinatra-book.gittr.com/
http://www.sinatrarb.com/
http://www.slideshare.net/Cannikin/classy-web-development-with-ruby-sinatra-and-heroku-1
THANKS!