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
30
31
32
33
# 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)

  %w(source_extension content_type).each do |field|
    raise "Missing required option :#{field}" unless options.has_key?(field.to_sym)
  end

  @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



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rack/asset_compiler.rb', line 50

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]

    # Swap in the source file extension
    request_base[-1].sub!(/\.\w+$/, '.' + source_extension)

    source_file = F.join(source_dir, request_base)

    if F.exists?(source_file)
      last_modified_time = F.mtime(source_file)

      if env['HTTP_IF_MODIFIED_SINCE']
        cached_time = Time.parse(env['HTTP_IF_MODIFIED_SINCE'])
        if last_modified_time <= cached_time
          return [304, {}, ["Not modified\r\n"]]
        end
      end

      body = compile(source_file)

      headers = {
        'Content-Type' => @content_type,
        'Content-Length' => body.length.to_s,
        'Last-Modified' => last_modified_time.httpdate
      }

      if @cache_ttl
        headers['Expires'] = (Time.now + @cache_ttl).strftime('%a, %d %b %Y %H:%M:%S GMT')
        headers['Cache-Control'] = "public,max-age=#{@cache_ttl}"
      end

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

  @app.call(env)
end

#compile(source_file) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/rack/asset_compiler.rb', line 42

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

#response(status, body) ⇒ Object



35
36
37
38
39
40
# File 'lib/rack/asset_compiler.rb', line 35

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