Class: RogerSassc::Middleware

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

Overview

Rack Middleware for Roger Sassc

This middleware transforms a given url scss -> css. It does this by checking if the .css can be resolved to .scss file, if so it compiles (with the help of libsass) the scss.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/roger_sassc/middleware.rb', line 15

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

  defaults = {
    load_paths: RogerSassc.load_paths,
    source_map: true,
    source_map_embed: true
  }

  @options = defaults.update(options)
end

Instance Attribute Details

#projectObject

Returns the value of attribute project.



13
14
15
# File 'lib/roger_sassc/middleware.rb', line 13

def project
  @project
end

#resolver=(value) ⇒ Object

Sets the attribute resolver

Parameters:

  • value

    the value to set the attribute resolver to.



12
13
14
# File 'lib/roger_sassc/middleware.rb', line 12

def resolver=(value)
  @resolver = value
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/roger_sassc/middleware.rb', line 27

def call(env)
  @project ||= env["roger.project"]
  @options[:roger_html_path] = @project.html_path

  url = ::Rack::Utils.unescape(env["PATH_INFO"].to_s).sub(%r{^/}, "")
  unless url.end_with?(".css") || url.end_with?(".css.map")
    return @app.call(env)
  end

  # Convert the url to an absolute path,
  # which is used to retrieve compile the scss
  scss_path = resolve_url(url)

  return @app.call(env) unless scss_path

  env["rack.errors"].puts "Invoking ruby-sassc for #{scss_path}"
  engine = create_scss_engine(scss_path)

  begin
    respond(engine, url.end_with?(".css.map"))
  rescue ::SassC::SyntaxError,
         ::SassC::NotRenderedError,
         ::SassC::InvalidStyleError => sassc_error
    respond_with_error(sassc_error)
  end
end