18
SINATRA Nitesh Kumar Member of technical staff 11.12.12

Sinatra

Embed Size (px)

DESCRIPTION

Introduction to Sinatra

Citation preview

Page 1: Sinatra

SINATRANitesh Kumar

Member of technical staff11.12.12

Page 2: Sinatra

ROADMAP

What is sinatra?

Why sinatra?

Flexibility with sinatra

Singing with sinatra

File hierarchy and naming convention

Features

Sinatra with ActiveRecord

Page 3: Sinatra

WHAT IS SINATRA?

A micro-framework for web applications

A ruby domain specific language

A ruby “gem”

Page 4: Sinatra

WHY SINATRA?

Lightweight

Nicely pluggable within other application

Good tool for creating REST-like interface

Support many different template syntaxes.

Page 5: Sinatra

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

Page 6: Sinatra

FLEXIBILITY WITH SINATRA :

Template engine ERB, HAML, Builder..

ORM ActiveRecord, DataMapper, Sequel

JS library jQuery, Prototype, YUI, Dojo

Testing tool Test::unit, Rspec, Bacon

Page 7: Sinatra

LET’S START THE SHOW

gem install sinatra

ruby hello-world.rb

Page 8: Sinatra

SINGING WITH “SINATRA”

#sample.rb

require ‘rubygems’require ‘sinatra’

get ‘ / ’ do“Why this kolavari kolavari kolavari

di..”end

Page 9: Sinatra

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

Page 10: Sinatra

FEATURES: [ ROUTING ]

Routing to code block

get ‘ / ‘ do“Hello world!”

end“ block ”

Whatever is returned from the block is sent to the browser

Page 11: Sinatra

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>

Page 12: Sinatra

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>

Page 13: Sinatra

FEATURES: [FILTERS]

beforebefore do

MyStore.connect unless MyStore.connected? end

get '/' do @list = MyStore.find(:all) erb :index

end

afterafter do

MyStore.disconnect end

Page 14: Sinatra

FEATURES: [HELPERS]

helpers dodef bar(name)

"#{name}bar“end

end

get '/:name' dobar(params[:name])

end

Page 15: Sinatra

DYNAMIC URLS?

#dynamic.rbrequire ‘rubygems’require ‘sinatra’

get ‘ / ’ do“This is homepage.”

end

get ‘/:name’ do‘Hello’ + params[:name] + ‘!’

end

Page 16: Sinatra

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

Page 18: Sinatra

THANKS!