Class: Middleware::MissingAvatars

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

Overview

In development mode, it is common to use a database from a production site for testing with their data. Unfortunately, you can end up with dozens of missing avatar requests due to the files not being present locally. This middleware, only enabled in development mode, will replace those with an appropriate image.

Instance Method Summary collapse

Constructor Details

#initialize(app, settings = {}) ⇒ MissingAvatars

Returns a new instance of MissingAvatars.



9
10
11
# File 'lib/middleware/missing_avatars.rb', line 9

def initialize(app, settings = {})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/middleware/missing_avatars.rb', line 13

def call(env)
  if (env["REQUEST_PATH"] =~ %r{\A/uploads/default/avatars})
    path = "#{Rails.root}/public#{env["REQUEST_PATH"]}"
    unless File.exist?(path)
      default_image = "#{Rails.root}/public/images/d-logo-sketch-small.png"
      return 200, { "Content-Type" => "image/png" }, [File.read(default_image)]
    end
  end

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