!Sinatra!
2015.1.18'at'
!@ryudoawaru
a.k.a$Ryudo$or$
• 10$years$experience$in$Computer$Game$
Industry
• Ruby/Rails/Sinatra$Programmer(from$
2007)
• RubyConf$Taiwan$CoEOrganizer
• CoEFounder$of$5xRuby.tw
• Orgainzer$of$Ruby$Taiwan$Community
• Organizer$of$Railsgirls$Taiwan$/$Taipei$
event
AGENDA
1. Scrath)From)Rack
2. Basic)Features
3. Advanced)Features
4. )web)app
1. Mac,' 'rvm
2. 'Windows' 'rvm' 'Mac,' 'Nitrous.io
3. 'slack' h:p:/
/goo.gl/forms/Qld4rqdH8x
h"ps:/
/www.nitrous.io
1.
2.
3.
Interface)between)your)applica2on)
and)Web)Applica2on)Server
• Ruby&Applica-ons
• Web&Applica-on&Server
Thin&/&Passenger&/&Unicorn
• Frontend&Server
Apache&/&Nginx
A"protocol"for"building"and"
composing"web"applica5ons
!Rack!
Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack'd']] }
call method*
*Rack*
• STATUS&CODE
• Response&Header&Hash
• Response&body&in&an&array
a"collec'on"of"middleware"u'li'es
rake middleware
use Rack::MiniProfiler
use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fe3f1d97420>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
run PentaRuby::Application.routes
Middleware)
1. End&Point
&response&body&
2. Middleware
&response&body
Rack%Compitable%Ruby%Web%Framework%From%
Scratch
1. # #DSL#route
get('/hello'){return '<html>...'}
2. # #Query#String#
#h3p:/
/localhost/?id=1
3. # #Render#ERB#View
Step%by%Step%checkout%and%commit
• "commit
•
• Repo h,p:/
/goo.gl/8pn638
Rackup'File
1. #config.ru
2. rackup
Basic&Class
Rack%env%hash
Describing*the*incoming*request
{"SERVER_SOFTWARE"=>"thin 1.6.1 codename Death Proof"...}
!env!hash!
1. SERVER_NAME
2. REQUEST_METHOD
3. REQUEST_PATH
4. PATH_INFO
5. REQUEST_URI
6. QUERY_STRING
7. HTTP_REFERER
Simple'Rou+ng'with'block
Rou$ng'with'Regexp
Process'Request'Object'&&'Params'
Hash
Request'Object
• Instance)of)Rack::Request
• Ini2al)from)env
ruby
def call(env)
request = Rack::Request.new(env)
end
^)logger) )request)object
Params&Hash
• "env" "QUERY_STRING"
• "request"
ruby
params = request.params
Render&View
ERB
Embeded&Ruby
Embebe%Ruby%code%in%text
<%=expression%> # expression
<%expression%> # expresion
ERB$engine$by$Tilt
1. h$ps:/
/github.com/rtomayko/5lt
Tilt::ERBTemplate.new(layout_file_path).render(evaluation_scope, locals_hash)
• evalua'on_scope .ERB. .variable.scope
• locals_hash .ERB.
View%with%layout Like%Rails
Pass$a$block$to$render
#layout
...
<%=yield%>
...
#view
# controller
Tilt::ERBTemplate.new(layout_file_path).render(evaluation_scope, locals_hash) do
content_to_render_in_yield
end
Render&
1. render'ac*on'view
2. render'layout'view,' 'ac*on'view' 'layout'view' 'block
Sinatra'Introduc-on
Sinatra
• Lightweight)Web)Framework
• Pure)Ruby
• )View) )Controller)
gem$install$sinatra
Bootstrap(into( Hello(Word!
rou$ng
HTTP$Verbs
• GET
• POST
• DELETE
• PUT+/+PATCH
Route&with&named&params
get '/hello/:name' do
params[:name]
end
• GET HTTP'Verb
• '/hello/:name' PATH
• do' 'block controller'code
HTTP$RESTFUL
HTTP$RESTFUL$
1. Noun
Resource,name books,/,cars,...,etc ,,
2. Verb
GET,/,POST,/,DELETE...
3. Content,Types
HTML,/,JSON,/,XML,/,ContentFType
!Image!Resource!App
!view
• layout
• view,file
Route&
Splat&Param&Route
get '/books/*.*' do
# matches /books/ruby-guide.html
params["splat"]# => ["ruby-guide", "html"]
end
Regular(Expression
get %r{/posts/name-([w]+)} do
# => get /posts/name-abc, params[:captures][0] = 'abc'
"Hello, #{params[:captures].first}!"
end
Filter
Filters
• before
• a(er
Session
The$diffrent$between
• Cookie'based'session
Client' 'cookie' 'session'id,' 'session'
id' 'session'data
• Cookie'stored'session
Client' 'cookie' 'session'id' 'data
logging&in&Sinatra
Mime%type%and%format
configure
• environment
• logging
• method_override
• public_folder
• raise_errors
• root
• sessions
Environment
• develop
• test
• environment
Error$handling
Sinatra'Extensions
h"p:/
/www.sinatrarb.com/contrib/
• Sinatra::JSON
• Sinatra::Namespace
• Sinatra::RespondWith
Sinatra'Ac*verecord
h"ps:/
/github.com/janko4m/sinatra4
ac6verecord
!Blog!
• Markdown*PO
• *Login
•
Resources
• h#p:/
/www.bootstrapcdn.com
• h#p:/
/kevo.io/pagedown8bootstrap/
• h#ps:/
/rubygems.org/gems/github8markdown
• h#ps:/
/github.com/janko8m/sinatra8ac<verecord

20150118 學個 Sinatra 好過年