Class: Rack::AssetCompiler

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

Direct Known Subclasses

CoffeeCompiler, SassCompiler

Constant Summary collapse

F =
::File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &block) ⇒ AssetCompiler

Returns a new instance of AssetCompiler.



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

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

  options = {
    :compiler => block,
    :source_dir => Dir.pwd,
    :url => '/',
    :cache => ENV['RACK_ENV'] == 'production'
  }.merge(options)

  @compiler = options[:compiler]
  @source_dir = options[:source_dir]
  @url = options[:url]
  @source_extension = options[:source_extension]
  @content_type = options[:content_type]

  @cache_ttl = options[:cache]

  # Default cache duration is 1 week
  if(@cache_ttl && !@cache_ttl.kind_of?(Integer))
    @cache_ttl = 60 * 60 * 24 * 7
  end
end

Instance Attribute Details

#source_dirObject

Returns the value of attribute source_dir.



3
4
5
# File 'lib/rack/asset_compiler.rb', line 3

def source_dir
  @source_dir
end

#source_extensionObject

Returns the value of attribute source_extension.



3
4
5
# File 'lib/rack/asset_compiler.rb', line 3

def source_extension
  @source_extension
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/rack/asset_compiler.rb', line 3

def url
  @url
end

Instance Method Details

#call(env) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rack/asset_compiler.rb', line 46

def call(env)
  request_path = Utils.unescape(env["PATH_INFO"])
  return response( 403, 'Forbidden') if request_path.include? ".."

  match_parts = url.split('/').select{ |str| str.length > 0 }
  request_parts = request_path.split('/').select{ |str| str.length > 0 }

  if(request_parts.take(match_parts.length) == match_parts)
    request_base = request_parts[match_parts.length..-1]

    # Directory listsings not supported
    return response( 404, 'Not found') if F.directory? F.join(source_dir, request_base)

    if source_extension
      # Swap in the source file extension if given
      request_base[-1].sub!(/\.\w+$/, '.' + source_extension)
    end
    source_file = F.join(source_dir, request_base)

    if F.exists?(source_file)
      body = compile(source_file)

      @content_type ||= Rack::Mime.mime_type(F.extname(source_file))

      headers = {
        'Content-Type' => @content_type
      }

      return [200, headers, [body]]
    end
  end

  @app.call(env)
end

#compile(source_file) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rack/asset_compiler.rb', line 38

def compile(source_file)
  if @compiler
    @compiler.call(source_file)
  else
    raise "Missing compiler"
  end
end

#response(status, body) ⇒ Object



31
32
33
34
35
36
# File 'lib/rack/asset_compiler.rb', line 31

def response(status, body)
  body += "\r\n"
  [status, {"Content-Type" => 'text/plain',
       "Content-Length" => body.size.to_s},
       [body]]
end