Class: Rack::Hard::Load

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/rack/hard/load.rb

Instance Method Summary collapse

Methods included from Util

#expired?, #generate_path_from, #http_headers, #ignored?, #make_dir, #setup_variables

Constructor Details

#initialize(app, opts = {}) ⇒ Load

Returns a new instance of Load.



6
7
8
9
# File 'lib/rack/hard/load.rb', line 6

def initialize(app, opts={})
  @app     = app
  setup_variables(opts)
end

Instance Method Details

#call(env) ⇒ Object



11
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
# File 'lib/rack/hard/load.rb', line 11

def call(env)
  return @app.call(env) if     ignored?(@ignores, env["PATH_INFO"].to_s)
  return @app.call(env) unless env["REQUEST_METHOD"] == "GET"

  logger    = env['rack.logger']||nil
  path      = generate_path_from(@store, env['PATH_INFO'].to_s)

  if ::File.exists?(path) && !ignored?(@ignores, path) && (@timeout === false || !expired?(@timeout, path))
    logger.info "Rack::Hard::Load loading: #{path}" rescue nil
    begin
      status = 200
      headers = http_headers(env)
      headers['X-Rack-Hard-Load'] = 'true' if @headers
      response = [ ::File.read(path) ]
    rescue => e
      logger.error "Rack::Hard::Load error creating: #{path}\n#{e}\n#{e.backtrace.join("\n")}" rescue nil
      status, headers, response = @app.call(env)
      headers['X-Rack-Hard-Load'] = "error" if @headers
    end
  else
    logger.info "Rack::Hard::Load passing" rescue nil
    status, headers, response = @app.call(env)
    headers['X-Rack-Hard-Load'] = "false" if @headers
  end
  return Rack::Response.new(response, status, headers)
end