Class: Speedup::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/speedup/middleware.rb

Defined Under Namespace

Classes: SpeedupBody

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
8
# File 'lib/speedup/middleware.rb', line 5

def initialize(app)
  @app = app
  @redirects = []
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/speedup/middleware.rb', line 10

def call(env)
  return @app.call(env) if !Speedup.enabled?

  Speedup.setup_request(env['action_dispatch.request_id'])
  status, headers, body = @app.call(env)
  Speedup.request.save

  if Speedup.show_bar? && headers['Content-Type'] =~ /text\/html/
    case status.to_i
    when 200..299, 400..500
      body = SpeedupBody.new(body, @redirects)
      headers['Content-Length'] = (headers['Content-Length'].to_i + body.bar_html.length).to_s if headers['Content-Length'] && body.render_bar?
      @redirects = []
    when 300..400
      @redirects.push(Speedup.request.id)
    end
  end

  [status, headers, body]
rescue Exception => exception
   Speedup.request && Speedup.request.save
  raise exception
end

#speedup_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/speedup/middleware.rb', line 34

def speedup_request?(env)
  env['REQUEST_PATH'].starts_with?('/speedup_rails')
end