Class: Frank::Middleware::Imager

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Imager.



11
12
13
# File 'lib/frank/middleware/imager.rb', line 11

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

Instance Method Details

#call(env) ⇒ Object

catch a request for _img/0x0, get an image, resize it to given dims



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/frank/middleware/imager.rb', line 25

def call(env)
  path = env['PATH_INFO']
  image_path = File.join(LIBDIR, 'frank/templates/imager/')

  if defined?(MiniMagick) && path.include?('_img')
    dims = '!' + path.split('/').last.match(/\d+x\d+/i).to_s
    filename = image_filename(dims, env['QUERY_STRING'])

    image = MiniMagick::Image.from_file(image_path + filename)
    image.resize dims
    return [ 200, { 'Content-Type' => 'image/jpg' }, image.to_blob ] 
  end
  @app.call(env)
end

#image_filename(dims, query) ⇒ Object

choose a random image if random is in the query



16
17
18
19
20
21
22
# File 'lib/frank/middleware/imager.rb', line 16

def image_filename(dims, query)      
  if query.include?('random')
    "frank#{rand(10)}.jpg"
  else
    "frank#{dims.hash.to_s[-1..-1]}.jpg"
  end  
end