Class: Rack::Hard::Save

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/rack/hard/save.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 = {}) ⇒ Save

Returns a new instance of Save.



6
7
8
9
# File 'lib/rack/hard/save.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
# File 'lib/rack/hard/save.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)

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

  if (@timeout === false || expired?(@timeout, path)) && !ignored?(@ignores, path) && status == 200
    begin
      make_dir(::File.dirname(path))
      create(path, response.first)
      headers['X-Rack-Hard-Save'] = 'true' if @headers
      logger.info "Rack::Hard::Save creating: #{path}" rescue nil
    rescue => e
      headers['X-Rack-Hard-Save'] = 'error' if @headers
      logger.error "Rack::Hard::Save error creating: #{path}\n#{e}\n#{e.backtrace.join("\n")}" rescue nil
    end
  end

  headers['X-Rack-Hard-Save'] ||= 'false' if @headers

  return Rack::Response.new(response, status, headers)
end