merb

25
Merb An Introduction

Upload: tohchye

Post on 18-May-2015

1.017 views

Category:

Technology


0 download

DESCRIPTION

An introduction to Merb

TRANSCRIPT

Page 1: Merb

MerbAn Introduction

Page 2: Merb

Who am I?

Page 3: Merb

History

Page 4: Merb

Key Focus

Page 5: Merb

Fast(No code is faster than no code)

Page 6: Merb

Memory Efficient

Page 7: Merb

Modular(All you need none you don’t)

Page 8: Merb

As Simple As Possible

Page 9: Merb

Features

Page 10: Merb

Plugins as Gems

Page 11: Merb

Rack Integration

Page 12: Merb

uploader = Proc.new do |env| request = Merb::Request.new(env) if request.path =~ /\/images\/upload/ params = request.params FileUtils.mv params[:file][:tempfile].path, Merb.root / 'public' / 'uploads' / params[:file][:filename]

headers = {"Content-Type"=>"text/html", "Location" => "/images"} [302, headers, "You are being redirected"] else [404, {}, "No Upload here, move along"] end end

merb = Merb::Rack::Application.new

run Rack::Cascade.new([uploader, merb])

File upload example using rack middleware

Page 13: Merb

Router

Page 14: Merb

Optional Path SegmentsMatching match("/articles(/:year(/:month(/:day)))/:title"). to(:controller => "articles", :action => "show"). name(:article) Generating /articles/Hello => { :title => “Hello” } /articles/2008/07/Hello => { :year => “2008”, :month => “07”, :title => “Hello” }

Page 15: Merb

Access to Request & Params

match("/secret").defer_to do |request, params| if request.session.authenticated? params end end

http://www.slideshare.net/carllerche/merb-pluming-the-router-presentation

Page 16: Merb

Controllers

Page 17: Merb

class Posts < Application provides :xml def show @post = Post.get(params[:id]) display @post end end

Page 18: Merb

Background Processing

Page 19: Merb

render_then_call dosend_notification

end

run_later do send_notificationend

Page 20: Merb

Exceptions

Page 21: Merb

class Exceptions < Merb::Controller # handle NotFound exceptions (404) def not_found render :format => :html end

# handle NotAcceptable exceptions (406) def not_acceptable render :format => :html end

end

Page 22: Merb

Merb Slice

Page 23: Merb

Application Server

Page 24: Merb

merb -e produciton -d -c 4

kill HUP ‘master process id’

Processes• master• spawner• worker

Hot Deploy

Page 25: Merb

Questions?