class SasscRack

Rack plugin that intercepts requests to css files and calls sassc on the corresponding scss file if there is one.

Public Class Methods

new(app, options = {}) click to toggle source

@param [Hash] options Configuration options @option options [String] :static_path Path from where static files

are served

@option options [Array] :loadpaths Additional paths for sassc to

look in

@option options [Boolean] :write_file Compile and write file to disk

instead of capturing sassc output and serving this directly
# File lib/sassc_rack.rb, line 11
def initialize(app, options = {})
  detect_sassc

  @app = app

  @options = {
    static_path: "public",
    loadpaths: [],
    write_file: true,
  }.update(options)

  sass_loadpaths = (Sass.load_paths | @options[:loadpaths]).join(":")
  @sass_command = 'sassc -I "' + sass_loadpaths + '" '
end

Public Instance Methods

call(env) click to toggle source
# File lib/sassc_rack.rb, line 31
def call(env)
  if env["PATH_INFO"].end_with? ".css"
    writepath = @options[:static_path] + env["PATH_INFO"]
    scssfile = writepath[0..-4] + "scss"

    if File.exist? scssfile
      if @options[:write_file]
        compile_write(env, scssfile, writepath)
      else
        compile_serve(env, scssfile)
      end
    else
      @app.call(env)
    end
  else
    @app.call(env)
  end
end
compile_serve(env, filepath) click to toggle source
# File lib/sassc_rack.rb, line 50
def compile_serve(env, filepath)
  _, headers, _ = @app.call(env)

  exec = @sass_command + filepath
  response_body = `#{exec}`

  headers["Content-Length"] = response_body.length.to_s

  [200, headers, [response_body]]
end
compile_write(env, filepath, writepath) click to toggle source
# File lib/sassc_rack.rb, line 61
def compile_write(env, filepath, writepath)
  system(@sass_command + filepath + " " + writepath)
  @app.call(env)
end
detect_sassc() click to toggle source
# File lib/sassc_rack.rb, line 26
def detect_sassc
  detect = system("sassc -v")
  fail "SasscRack could not find sassc." unless detect
end