Skip to content

Instantly share code, notes, and snippets.

@lennyburdette
Created July 12, 2013 20:00
Show Gist options
  • Save lennyburdette/5987318 to your computer and use it in GitHub Desktop.
Save lennyburdette/5987318 to your computer and use it in GitHub Desktop.
Learn Rails Inside and Out: Rack example code
require 'rack'
your_app = Proc.new { [200, {}, ["Hello World!"]] }
Rack::Server.start app: your_app, Port: 9292
#####################################################################
your_app = proc { [200, {}, ["Hello World!"]] }
run your_app
#####################################################################
run proc { [200, {}, ["<h1>Hello World!</h1>"]] }
#####################################################################
run proc { [200, { "Content-Type" => "text/html" }, ["<h1>Hello World!</h1>"]] }
#####################################################################
# encoding: utf-8
run proc { [200, { "Content-Type" => "text/html; charset=utf-8" }, ["<h1>Hello World! 👍</h1>"]] }
#####################################################################
run proc { |env| [200, {}, ["env is a #{env.class}"]] }
#####################################################################
run proc { |env|
list = env.map do |key, value|
"<dt>#{key}</dt>" +
"<dd>#{value.inspect.gsub('<', '&lt;')}</dd>"
end
[200, { "Content-Type" => "text/html" }, ["<dl>", *list, "</dl>"]]
}
#####################################################################
run proc { |env|
params = Rack::Utils.parse_query(env['QUERY_STRING'])
name = params["name"]
[200, { "Content-Type" => "text/html" }, ["Your name is #{name}"]]
}
#####################################################################
run proc { |env|
request = Rack::Request.new(env)
name = request.params["name"]
[200, { "Content-Type" => "text/html" }, ["Your name is #{name}"]]
}
#####################################################################
run proc { |env|
request = Rack::Request.new(env)
methods = request.public_methods(false).sort.map { |m| "<li>#{m}</li>" }
[200, { "Content-Type" => "text/html" }, ['<ul>', *methods, '</ul>']]
}
#####################################################################
class DumbLogger
def initialize(app)
@app = app
end
def call(env)
start = Time.now
status, headers, body = @app.call(env)
finish = Time.now
[status, headers, body + ["\n<!-- Request took #{finish - start} seconds -->"]]
end
end
use DumbLogger
run proc { |env| sleep(1); [200, {}, ["hello"]] }
#####################################################################
class CoreValueHeader
VALUES = [
"1. Deliver WOW Through Service",
"2. Embrace and Drive Change",
"3. Create Fun and A Little Weirdness",
"4. Be Adventurous, Creative, and Open-Minded",
"5. Pursue Growth and Learning",
"6. Build Open and Honest Relationships With Communication",
"7. Build a Positive Team and Family Spirit",
"8. Do More With Less",
"9. Be Passionate and Determined",
"10. Be Humble"
]
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
[status, headers.merge({ "X-Core-Value" => VALUES.sample(1).first }), body]
end
end
use CoreValueHeader
run proc { [200, { "Content-Type" => "text/html" }, ["Hello World"]] }
#####################################################################
#####################################################################
class Router
def call(env)
# controller.action(action).call(env)
[200, {}, ["router called"]]
end
end
run Router.new
#####################
# /foo/bar
class FooController
def bar
"hi from foo#bar"
end
end
#####################
class Router
def call(env)
# "/foo/bar" => ["/", "foo", "bar"]
url_parts = env['REQUEST_PATH'].split("/")
controller_name = url_parts[1]
action = url_parts[2].to_sym
controller_name = "#{controller_name.capitalize}Controller"
controller = Kernel.const_get(controller_name)
controller.action(action).call(env)
end
end
#####################
class BaseController
def self.action(action_name)
lambda { |env|
[200, {}, ["BaseController::action called"]]
}
end
end
class FooController < BaseController
end
#####################
lambda { |env|
[200, {}, [self.new.send(action_name)]]
}
#####################
def call(env)
rescue NoMethodError
[404, {}, ["Not Found (action missing)"]]
end
#####################
def baz
"FOO! BAZ!"
end
#####################
def baz
@things = %w{one two three}
template = File.read("foo/baz.html.erb")
ERB.new(template).result(binding).to_s
end
#####################################################################
require 'action_controller'
class FooController < ActionController::Metal
include ActionController::Rendering
append_view_path File.dirname(__FILE__)
def bar
self.response_body = "hi from foo#bar"
end
def baz
@things = %w{one two three}
render "foo/baz"
end
end
#####################################################################
require 'sinatra'
class MyApp < Sinatra::Base
get '/foo/bar' do
"hi from foo#bar"
end
get '/foo/baz' do
@things = %w{one two three}
erb :baz, views: File.dirname(__FILE__) + "/foo"
end
end
run MyApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment