Class: Rack::LessCss

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

Instance Method Summary collapse

Constructor Details

#initialize(app, opts) ⇒ LessCss

Returns a new instance of LessCss.



6
7
8
9
10
11
12
# File 'lib/rack-lesscss.rb', line 6

def initialize(app, opts)
  @app = app
  @less_path = opts[:less_path] or raise ArgumentError, "You must specify :less_path option (path to directory containing .less files)"
  css_route = opts[:css_route] || "/stylesheets"
  css_route = css_route[0..-2] if css_route[-1] == "/"
  @css_route_regexp = /#{Regexp.escape(css_route)}\/(.+)\.css/
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rack-lesscss.rb', line 14

def call(env)
  if env['PATH_INFO'] =~ @css_route_regexp
    begin
      headers = { 'Content-Type' => 'text/css', 'Cache-Control' => 'private' }
      body = "/* Generated from #{$1}.less by Rack::LessCss middleware */\n\n"
      body << Less::Engine.new(get_source($1)).to_css
      headers["Content-Length"] = body.size.to_s
      return [200, headers, [body]]
    rescue SyntaxError, StandardError => e
      puts "Rack::LessCss Error: #{e.message}"
    end
  end
  @app.call(env)
end