And the Greatest of These Is ... Space
Upcoming SlideShare
Loading in...5
×
 

And the Greatest of These Is ... Space

on

  • 2,735 views

Presentation given at Rails Underground; revision of earlier version given at Railsconf 2009.

Presentation given at Rails Underground; revision of earlier version given at Railsconf 2009.

Statistics

Views

Total Views
2,735
Views on SlideShare
2,657
Embed Views
78

Actions

Likes
4
Downloads
26
Comments
0

5 Embeds 78

http://skillsmatter.com 73
http://www.slideshare.net 2
http://www.apurva.com 1
http://192.168.56.101 1
http://smash 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    And the Greatest of These Is ... Space And the Greatest of These Is ... Space Presentation Transcript

    • And the Greatest of These Is... Rack Support Ben Scofield – Viget Labs Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Application Templates Saturday, July 25, 2009
    • Nested Attribute Assignment flickr: angelrays Saturday, July 25, 2009
    • ActiveRecord::Base#touch flickr: jjjohn Saturday, July 25, 2009
    • DB Seeding flickr: richardthomas78 Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Rack Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • def call(env) [ status, # 200 headers, # {"Content-Type" => "text/html"} body # ["<html>...</html>"] ] end Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • request application response Saturday, July 25, 2009
    • request middleware application middleware response Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Rack::Profiler flickr: oberazzi Saturday, July 25, 2009
    • Rack::MailExceptions flickr: warmnfuzzy Saturday, July 25, 2009
    • Rack::Cache flickr: timpatterson Saturday, July 25, 2009
    • rack::cascade Rack::Cascade flickr: _at Saturday, July 25, 2009
    • Rack in Rails Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • > 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
    • > 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
    • 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
    • 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
    • # 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
    • Metal flickr: lrargerich Saturday, July 25, 2009
    • http://www.kiwibean.com/ Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • PLEASE testing metalBY EXPAND: STAND THIS IS ONLY A TEST Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • METAL Sinatra Saturday, July 25, 2009
    • Cool Tricks Saturday, July 25, 2009
    • Rack::Bug flickr: catdancing Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • requires </body> Saturday, July 25, 2009
    • Progressive Caching Saturday, July 25, 2009
    • Progressive Caching Saturday, July 25, 2009
    • Progressive Caching Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • class SessionsController < ApplicationController def create # ... session[:personalize] = { :logged_in => true, :quicklist => current_user.quicklist.episode_ids, :subscriptions => current_user.subscription_ids } end end Saturday, July 25, 2009
    • class ChannelsController < ApplicationController caches_page :show def show @channel = Channel.find(params[:id]) end end Saturday, July 25, 2009
    • $(document).ready(function() { $.getJSON('/personalize', function(data) { // identify quicklisted episodes $.each(data['quicklist'], function() { $('#episode_'+this).addClass('listed'); }); // switch navigation if (data['logged_in']) { $('#logged_in_nav').show(); } // etc. }); }); Saturday, July 25, 2009
    • 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
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • class PullList def self.call(env) if env["PATH_INFO"] =~ /^/pulls/ [ 200, {"Content-Type" => "application/javascript"}, [Pull.by_user(user).for_date(date).map {|i| i.item_id}.to_json]] else [404, {"Content-Type" => "text/html"}, ["Not Found"]] end end end standard 0.617 progressive 0.039 0.096 Saturday, July 25, 2009
    • class PullList def self.call(env) if env["PATH_INFO"] =~ /^/pulls/ [ 200, {"Content-Type" => "application/javascript"}, [env['rack.session'][:pulls].to_json] ] else [404, {"Content-Type" => "text/html"}, ["Not Found"]] end end end standard 0.617 progressive 0.039 0.096 progressive v2 0.043 0.023 Saturday, July 25, 2009
    • DIY Saturday, July 25, 2009
    • request middleware application middleware response Saturday, July 25, 2009
    • Rack::Embiggener Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • WARNING exception handling Saturday, July 25, 2009
    • formerly: WARNING exception handling Saturday, July 25, 2009
    • 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
    • Saturday, July 25, 2009
    • > 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
    • ActionController::Dispatcher.middleware.delete ActionDispatch::ShowExceptions ActionController::Dispatcher.middleware.delete ActionDispatch::Rescue ActionController::Dispatcher.middleware.use 'Rack::Hoptoad', 'my-api-key' ActionController::Dispatcher.middleware.use 'Rack::ShowExceptions' Saturday, July 25, 2009
    • module Rack class Hoptoad def initialize(app, api_key) @app = app @api_key = api_key end def call(env) @app.call(env) rescue => exception notify_hoptoad(exception, env) raise exception end # ... end end Saturday, July 25, 2009
    • module Rack class Hoptoad def notify_hoptoad(exception, env) headers = { 'Content-type' => 'application/x-yaml', 'Accept' => 'text/xml, application/xml' } url = URI.parse("http://hoptoadapp.com/notices") http = Net::HTTP.new(url.host, url.port) data = { :api_key => @api_key, :error_message => "#{exception.class}: #{exception}", :backtrace => exception.backtrace, :request => {}, :session => env['rack.session'], :environment => env } response = http.post( url.path, stringify_keys(:notice => data).to_yaml, headers ) if response != Net::HTTPSuccess # Hoptoad post failed end end end end Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Intangibles Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Saturday, July 25, 2009
    • Thank You ben scofield - @bscofield - http://www.viget.com/extend - http://www.speakerrate.com/bscofield Saturday, July 25, 2009