Slide 1

Slide 1 text

!Sinatra! 2015.1.18'at'

Slide 2

Slide 2 text

!@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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

AGENDA 1. Scrath)From)Rack 2. Basic)Features 3. Advanced)Features 4. )web)app

Slide 5

Slide 5 text

1. Mac,' 'rvm 2. 'Windows''rvm''Mac,' 'Nitrous.io 3. 'slack' h:p:/ /goo.gl/forms/Qld4rqdH8x

Slide 6

Slide 6 text

h"ps:/ /www.nitrous.io 1. 2. 3.

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Interface)between)your)applica2on) and)Web)Applica2on)Server

Slide 9

Slide 9 text

• Ruby&Applica-ons • Web&Applica-on&Server Thin&/&Passenger&/&Unicorn • Frontend&Server Apache&/&Nginx

Slide 10

Slide 10 text

A"protocol"for"building"and" composing"web"applica5ons

Slide 11

Slide 11 text

!Rack! Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['get rack\'d']] }

Slide 12

Slide 12 text

callmethod* *Rack*

Slide 13

Slide 13 text

• STATUS&CODE • Response&Header&Hash • Response&body&in&an&array

Slide 14

Slide 14 text

a"collec'on"of"middleware"u'li'es

Slide 15

Slide 15 text

rake middleware use Rack::MiniProfiler use Rack::Sendfile use ActionDispatch::Static use Rack::Lock use # 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

Slide 16

Slide 16 text

Middleware) 1. End&Point &response&body& 2. Middleware &response&body

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Rack%Compitable%Ruby%Web%Framework%From% Scratch 1. ##DSL#route get('/hello'){return '...'} 2. ##Query#String# #h3p:/ /localhost/?id=1 3. ##Render#ERB#View

Slide 19

Slide 19 text

Step%by%Step%checkout%and%commit • "commit • • Repo h,p:/ /goo.gl/8pn638

Slide 20

Slide 20 text

Rackup'File 1. #config.ru 2. rackup

Slide 21

Slide 21 text

Basic&Class

Slide 22

Slide 22 text

Rack%env%hash Describing*the*incoming*request {"SERVER_SOFTWARE"=>"thin 1.6.1 codename Death Proof"...}

Slide 23

Slide 23 text

!env!hash! 1. SERVER_NAME 2. REQUEST_METHOD 3. REQUEST_PATH 4. PATH_INFO 5. REQUEST_URI 6. QUERY_STRING 7. HTTP_REFERER

Slide 24

Slide 24 text

Simple'Rou+ng'with'block

Slide 25

Slide 25 text

Rou$ng'with'Regexp

Slide 26

Slide 26 text

Process'Request'Object'&&'Params' Hash

Slide 27

Slide 27 text

Request'Object • Instance)of)Rack::Request • Ini2al)from)env ruby def call(env) request = Rack::Request.new(env) end ^)logger))request)object

Slide 28

Slide 28 text

Params&Hash • "env""QUERY_STRING" • "request" ruby params = request.params

Slide 29

Slide 29 text

Render&View

Slide 30

Slide 30 text

ERB Embeded&Ruby Embebe%Ruby%code%in%text <%=expression%> # expression <%expression%> # expresion

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

View%with%layoutLike%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

Slide 33

Slide 33 text

Render& 1. render'ac*on'view 2. render'layout'view,''ac*on'view''layout'view''block

Slide 34

Slide 34 text

Sinatra'Introduc-on

Slide 35

Slide 35 text

Sinatra • Lightweight)Web)Framework • Pure)Ruby • )View))Controller)

Slide 36

Slide 36 text

gem$install$sinatra

Slide 37

Slide 37 text

Bootstrap(into(Hello(Word!

Slide 38

Slide 38 text

rou$ng

Slide 39

Slide 39 text

HTTP$Verbs • GET • POST • DELETE • PUT+/+PATCH

Slide 40

Slide 40 text

Route&with&named&params get '/hello/:name' do params[:name] end • GETHTTP'Verb • '/hello/:name'PATH • do''blockcontroller'code

Slide 41

Slide 41 text

HTTP$RESTFUL

Slide 42

Slide 42 text

HTTP$RESTFUL$ 1. Noun Resource,namebooks,/,cars,...,etc,, 2. Verb GET,/,POST,/,DELETE... 3. Content,Types HTML,/,JSON,/,XML,/,ContentFType

Slide 43

Slide 43 text

!Image!Resource!App

Slide 44

Slide 44 text

!view • layout • view,file

Slide 45

Slide 45 text

Route&

Slide 46

Slide 46 text

Splat&Param&Route get '/books/*.*' do # matches /books/ruby-guide.html params["splat"]# => ["ruby-guide", "html"] end

Slide 47

Slide 47 text

Regular(Expression get %r{/posts/name-([\w]+)} do # => get /posts/name-abc, params[:captures][0] = 'abc' "Hello, #{params[:captures].first}!" end

Slide 48

Slide 48 text

Filter

Slide 49

Slide 49 text

Filters • before • a(er

Slide 50

Slide 50 text

Session The$diffrent$between • Cookie'based'session Client''cookie''session'id,' 'session' id''session'data • Cookie'stored'session Client''cookie''session'id''data

Slide 51

Slide 51 text

logging&in&Sinatra

Slide 52

Slide 52 text

Mime%type%and%format

Slide 53

Slide 53 text

configure • environment • logging • method_override • public_folder • raise_errors • root • sessions

Slide 54

Slide 54 text

Environment • develop • test • environment

Slide 55

Slide 55 text

Error$handling

Slide 56

Slide 56 text

Sinatra'Extensions h"p:/ /www.sinatrarb.com/contrib/ • Sinatra::JSON • Sinatra::Namespace • Sinatra::RespondWith

Slide 57

Slide 57 text

Sinatra'Ac*verecord h"ps:/ /github.com/janko4m/sinatra4 ac6verecord

Slide 58

Slide 58 text

!Blog! • Markdown*PO • *Login •

Slide 59

Slide 59 text

Resources • h#p:/ /www.bootstrapcdn.com • h#p:/ /kevo.io/pagedown8bootstrap/ • h#ps:/ /rubygems.org/gems/github8markdown • h#ps:/ /github.com/janko8m/sinatra8ac