25. > rake middleware
use Rack::Lock
use ActionDispatch::ShowExceptions, [true]
use ActionDispatch::Callbacks, [true]
use ActionDispatch::Rescue, [#<Method: ...>]
use ActionDispatch::Session::CookieStore, [{...}]
use ActionDispatch::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
run ActionController::Dispatcher.new
Saturday, July 25, 2009
26. > rake middleware
use Rack::Lock
use ActionDispatch::ShowExceptions, [true]
use ActionDispatch::Callbacks, [true]
use ActionDispatch::Rescue, [#<Method: ...>]
use ActionDispatch::Session::CookieStore, [{...}]
use ActionDispatch::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
run ActionController::Dispatcher.new
Saturday, July 25, 2009
27. app = Rack::Builder.new {
use Rails::Rack::LogTailer unless options[:detach]
use Rails::Rack::Debugger if options[:debugger]
map "/" do
use Rails::Rack::Static
run ActionController::Dispatcher.new
end
}.to_app
Saturday, July 25, 2009
28. Rails::Initializer.run do |config|
config.middleware.insert_before Rack::Head, Ben::Asplode
config.middleware.swap Rack::Lock, Ben::Lock
config.middleware.use Rack::MailExceptions
end
Saturday, July 25, 2009
29. # in config/initializers/middlewares.rb
Rails::Initializer.run do |config|
config.middleware.insert_before Rack::Head, Ben::Asplode
ActionController::Dispatcher.middleware.insert_before 'Rack::Head', Appender
config.middleware.swap Rack::Lock, Ben::Lock
ActionController::Dispatcher.middleware.delete 'Rack::Lock'
config.middleware.use Rack::MailExceptions
ActionController::Dispatcher.middleware.use Prepender
end
Saturday, July 25, 2009
50. class Personalizer
def self.call(env)
if env["PATH_INFO"] =~ /^/personalize/
[
200,
{"Content-Type" => "application/javascript"},
env['rack.session'][:personalize].to_json
]
else
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
end
end
Saturday, July 25, 2009
61. class Embiggener < Test::Unit::TestCase
def test_embiggener_should_embiggen_known_url
body = ["Hello! I'm at http://bit.ly/31IqMl"]
assert_equal ["Hello! I'm at http://cnn.com"],
Rack:: Embiggener.embiggen_urls(body)
end
def test_embiggener_should_embiggen_multiple_urls
body = [
"Hello! I'm at http://bit.ly/31IqMl",
"And I'm at http://bit.ly/31IqMl"
]
assert_equal [
"Hello! I'm at http://cnn.com",
"And I'm at http://cnn.com"
], Rack::Embiggener.embiggen_urls(body)
end
end
Saturday, July 25, 2009
62. module Rack
class Embiggener
def self.embiggen_urls(original_body, login, key)
new_body = []
original_body.each { |line|
bits = line.scan(/(http://bit.ly/(.{6}))/)
new_body << bits.uniq.inject(line) do |body, bit|
original_bit, hash = *bit
new_url = embiggened_url(hash, login, key)
body.gsub(original_bit, new_url) if new_url
end
}
new_body
end
# ...
end
end
Saturday, July 25, 2009
63. module Rack
class Embiggener
# ...
def self.embiggened_url(hash, login, key)
url = [
"http://api.bit.ly/expand?version=2.0.1",
"shortUrl=http://bit.ly/#{hash}", "login=#{login}", "apiKey=#{key}"
].join('&')
response = JSON.parse(Net::HTTP.get_response(URI.parse(url)))
if response['statusCode'] = 'OK'
embiggened_url = response['results'][hash]['longUrl']
end
end
# ...
end
end
Saturday, July 25, 2009
64. module Rack
class Embiggener
# ...
attr_accessor :api_login, :api_key
def initialize(app, api_login, api_key)
@app = app
@api_login = api_login
@api_key = api_key
end
def call(env)
status, headers, body = @app.call(env)
headers.delete('Content-Length')
response = Rack::Response.new(
Rack::Embiggener.embiggen_urls(body, api_login, api_hash),
status,
headers
)
response.finish
return response.to_a
end
end
end
Saturday, July 25, 2009
65. module Rack
class Embiggener
def self.embiggen_urls(original_body, login, key)
new_body = []
original_body.each { |line|
bits = line.scan(/bit:(w+?)/)
new_body << bits.uniq.inject(line) do |body, bit|
hash = bit.first
original_bit = "bit:#{hash}"
new_url = embiggened_url(hash, login, key)
body.gsub(original_bit, new_url) if new_url
end
}
new_body
end
# ...
end
end
Saturday, July 25, 2009
66. module Rack
class Embiggener
# ...
def self.embiggened_url(hash, login, key)
url = [
"http://api.bit.ly/expand?version=2.0.1",
"hash=#{hash}", "login=#{login}", "apiKey=#{key}"
].join('&')
response = JSON.parse(Net::HTTP.get_response(URI.parse(url)))
if response['statusCode'] = 'OK'
embiggened_url = response['results'][hash]['longUrl']
end
end
# ...
end
end
Saturday, July 25, 2009
67. WARNING exception handling
Saturday, July 25, 2009
68. formerly:
WARNING exception handling
Saturday, July 25, 2009
69. Rack::Hoptoad*
* unofficial; thoughtbot is not responsible for this middleware; do not taunt rack::hoptoad; pregnant women should consult their doctors before using rack::hoptoad
Saturday, July 25, 2009
71. > rake middleware
use Rack::Lock
use ActionDispatch::ShowExceptions, [true]
use ActionDispatch::Callbacks, [true]
use ActionDispatch::Rescue, [#<Method: ...>]
use ActionDispatch::Session::CookieStore, [{...}]
use ActionDispatch::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
run ActionController::Dispatcher.new
Saturday, July 25, 2009