Class: FuckingFavicons::Middleware

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fucking_favicons/middleware.rb', line 3

def initialize(app, options = {})
  @app = app
  @favicon_path = options[:favicon_path] || options[:path]

  unless @favicon_path
    GLOBS.each do |glob|
      first = Dir.glob(glob).first

      if first
        @favicon_path = first

        if STDERR.tty?
          #warn "FuckingFavicon: #{ @favicon_path }"
        end

        break
      end
    end
  end

  begin
    MiniMagick::Image.open(@favicon_path)
  rescue
    warn "FuckingFavicon: #{ @favicon_path }"
    raise
  end

  CONFIG.each do |c|
    c[:regex] = %r'\A(?:/assets/|/)?#{ Regexp.escape( c[:path] )}'
  end
end

Instance Method Details

#call(env) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/fucking_favicons/middleware.rb', line 35

def call(env)
  config = CONFIG.detect{|c| env['PATH_INFO'] =~ c[:regex]}

  return @app.call(env) unless config

  favicon = FuckingFavicons.favicon_for(@favicon_path, config)

  [200, favicon[:headers], [favicon[:body]]]
end