Class: Rack::Relativize
- Inherits:
-
Object
- Object
- Rack::Relativize
- Defined in:
- lib/rack/relativize/version.rb,
lib/rack/relativize.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Relativize
constructor
A new instance of Relativize.
Constructor Details
#initialize(app) ⇒ Relativize
Returns a new instance of Relativize.
6 7 8 |
# File 'lib/rack/relativize.rb', line 6 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rack/relativize.rb', line 10 def call(env) status, headers, enumerable_body = original_response = @app.call(env.dup) if headers["Content-Type"].to_s.match(/(ht|x)ml/) # FIXME: Use another pattern type = :html elsif headers["Content-Type"].to_s.match(/css/) type = :css else return original_response end path = env['PATH_INFO'] raise "PATH_INFO is Empty" if path == "" content = join_body(enumerable_body) case type when :html processed_body = content.gsub(/(<[^>]+\s+(src|href))=(['"]?)(\/.*?)\3([ >])/) do $1 + '=' + $3 + relative_path_to(path, $4) + $3 + $5 end when :css processed_body = content.gsub(/url\((['"]?)(\/.*?)\1\)/) do 'url(' + $1 + relative_path_to(path, $2) + $1 + ')' end else raise RuntimeError.new( "The relativize_paths needs to know the type of content to " + "process. Pass :type => :html for HTML or :type => :css for CSS." ) end processed_headers = headers.merge({ "Content-Length" => processed_body.size.to_s }) [status, processed_headers, [processed_body]] end |