Class: Rack::GoldenFrill

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/golden_frill.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, output_path) ⇒ GoldenFrill

Returns a new instance of GoldenFrill.

Raises:

  • (ArgumentError)


3
4
5
6
7
# File 'lib/rack/golden_frill.rb', line 3

def initialize app, output_path
  @app = app
  @root = ::File.expand_path(output_path)
  raise ArgumentError, "#{output_path} is not a directory" unless ::File.directory?(@root)
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/golden_frill.rb', line 9

def call env
  unless env["PATH_INFO"] =~ /images\/frill_/
    return @app.call(env)
  end

  command = env["PATH_INFO"].match(/\/frill_(.*)\.png/i)[1]
  request = ::Rack::Request.new(env)
  image_path = ::File.join(@root,"frill_#{command}.png")
  if ::File.exist?(image_path)
    return ::Rack::File.new(@root).call(env)
  end
  
  opts = { :output_path => image_path }
  opts[:base_color], opts[:width], opts[:height], opts[:frill_color] = command.split('.')
  ::GoldenFrill.run!(opts)
  # Redirect to this URL since it will now be served.
  return [301, {'Location' => request.url}, ['Redirecting to created image.']]
end