Class: Rack::AssetCompiler
- Inherits:
-
Object
- Object
- Rack::AssetCompiler
- Defined in:
- lib/rack/asset_compiler.rb
Direct Known Subclasses
Constant Summary collapse
- F =
::File
Instance Attribute Summary collapse
-
#source_dir ⇒ Object
Returns the value of attribute source_dir.
-
#source_extension ⇒ Object
Returns the value of attribute source_extension.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #compile(source_file) ⇒ Object
-
#initialize(app, options = {}, &block) ⇒ AssetCompiler
constructor
A new instance of AssetCompiler.
- #response(status, body) ⇒ Object
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, ={}, &block) @app = app = { :compiler => block, :source_dir => Dir.pwd, :url => '/', :cache => ENV['RACK_ENV'] == 'production' }.merge() %w(source_extension content_type).each do |field| raise "Missing required option :#{field}" unless .has_key?(field.to_sym) end @compiler = [:compiler] @source_dir = [:source_dir] @url = [:url] @source_extension = [:source_extension] @content_type = [:content_type] @cache_ttl = [: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_dir ⇒ Object
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_extension ⇒ Object
Returns the value of attribute source_extension.
3 4 5 |
# File 'lib/rack/asset_compiler.rb', line 3 def source_extension @source_extension end |
#url ⇒ Object
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 94 95 |
# 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] # Directory listsings not supported return response( 403, 'Forbidden') if F.directory? F.join(source_dir, request_base) # 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 |