class Rack::Tumblargh

Attributes

options[R]

Public Class Methods

new(app, options={}) click to toggle source
# File lib/rack/tumblargh.rb, line 4
def initialize(app, options={})
  @app = app
  @options = options
  @options[:blog] = 'staff.tumblr.com' if @options[:blog].nil?
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/tumblargh.rb, line 12
def call(env)
  request = Rack::Request.new(env)

  ['/tweets.js', %r{/api.*}].each do |route|
    if request.path.match route
      url = "http://#{@options[:blog]}#{request.path}?#{request.query_string}"
      return [301, { "Location" => url }, []]
    end
  end

  status, headers, response = @app.call(env)

  if should_parse?(status, headers)
    content = response.respond_to?(:body) ? response.body : response
    render_opts = { :permalink => permalink?(env['PATH_INFO']) }

    headers.delete('Content-Length')
    response = Rack::Response.new(
      render(content, render_opts),
      status,
      headers
    )
    response.finish
    response.to_a
  else
    [status, headers, response]
  end
end

Private Instance Methods

render(content, opts) click to toggle source
# File lib/rack/tumblargh.rb, line 53
def render(content, opts)
  ::Tumblargh::render_html(content.first, options[:blog], opts)
end
should_parse?(status, headers) click to toggle source
# File lib/rack/tumblargh.rb, line 47
def should_parse?(status, headers)
  status == 200 && 
  headers["Content-Type"] && 
  headers["Content-Type"].include?("text/html")
end