Class: Rack::DevFavicon

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-devfavicon.rb,
lib/rack-devfavicon/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DevFavicon.



6
7
8
# File 'lib/rack-devfavicon.rb', line 6

def initialize(app, options = {})
  @app = app
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
# File 'lib/rack-devfavicon.rb', line 10

def call(env)
  unless env['PATH_INFO'] =~ %r{\A /favicon\.(?:png|ico) \z}xm
    return @app.call(env)
  end

  env.delete('HTTP_IF_MODIFIED_SINCE') # do not cache the icon file

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

  data = ''
  response.each do |buf|
    data << buf
  end

  img = Magick::Image.from_blob(data) do |info|
    info.format = headers['Content-Type'] =~ /png/ ? 'PNG' : 'ICO'
  end[0]
  img = img.quantize(4, Magick::GRAYColorspace)

  [status, headers, [img.to_blob]]
end