Class: Rack::Tumblargh

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/tumblargh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Tumblargh

Returns a new instance of Tumblargh.



4
5
6
7
8
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/rack/tumblargh.rb', line 10

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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