class Rack::VarnishEsiProcessor

Public Class Methods

new(locations) click to toggle source
# File lib/rack/varnish_esi_processor.rb, line 5
def initialize(locations)
  @locations = locations
end

Public Instance Methods

process(body) click to toggle source
# File lib/rack/varnish_esi_processor.rb, line 9
def process(body)
  output = []

  body.each do |part|
    part = process_remove(part)
    part = process_comment(part)
    output << process_include(part)
  end

  output
end

Protected Instance Methods

fetch(src) click to toggle source
# File lib/rack/varnish_esi_processor.rb, line 37
def fetch(src)
  location = src.gsub(/https?:\/\/[^\/]+/, '')
  location = "/#{location}" unless location.start_with?('/')

  @locations.each do |pattern, host|
    if (pattern.is_a?(String) && location == pattern) || (pattern.is_a?(Regexp) && location =~ pattern)
      return get("#{host}#{location}")
    end
  end
end
get(uri) click to toggle source
# File lib/rack/varnish_esi_processor.rb, line 48
def get(uri)
  Net::HTTP.get(URI(uri))
end
process_comment(part) click to toggle source
# File lib/rack/varnish_esi_processor.rb, line 27
def process_comment(part)
  part.gsub(/<!--esi(.*)-->/m, '\1')
end
process_include(part) click to toggle source
# File lib/rack/varnish_esi_processor.rb, line 31
def process_include(part)
  part.gsub(/<esi:include src="([^"]+)"(?: +alt="([^"]+)")? *\/?>/) do
    fetch($1).force_encoding(part.encoding)
  end
end
process_remove(part) click to toggle source
# File lib/rack/varnish_esi_processor.rb, line 23
def process_remove(part)
  part.gsub(/<esi:remove>.*<\/esi:remove>/m, '')
end