class Rack::FaviconAll

Constants

FAVICON
VERSION

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/favicon_all.rb, line 30
def initialize(app, options = {})
  @app = app
  @favicon_path = options[:favicon_path]
  if !@favicon_path.nil? && ::File.exist?(@favicon_path)
    @image = Magick::Image.read(@favicon_path).first
  end
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/favicon_all.rb, line 38
def call(env)
  return @app.call(env) if @image.nil?

  favicon_info = FAVICON.find { |f| env['PATH_INFO'] =~ f[:path] }
  return @app.call(env) if favicon_info.nil?

  body = genarate_favicon(favicon_info)
  headers = {
    "Content-Length" => body.bytesize.to_s,
    "Content-Type"   => favicon_info[:mime_type] || "imgae/png",
    "Last-Modified"  => Time.now.httpdate
  }

  [200, headers, [body]]
end

Private Instance Methods

genarate_favicon(info) click to toggle source
# File lib/rack/favicon_all.rb, line 56
def genarate_favicon(info)
  @image.scale!(*info[:size]).to_blob do
    self.format = info[:format] unless info[:format].nil?
  end
end