Sha256: df9f6021f1326538b2e6e29908391efada7910ec4cdcb85e4ff80f5247e9ec1b

Contents?: true

Size: 769 Bytes

Versions: 5

Compression:

Stored size: 769 Bytes

Contents

require 'rack'

module Rack

  module Rewritten

    class Html

      def initialize(app)
        @app = app
      end

      def call(env)
        puts "-> Rack::Rewritten::Html"
        req = Rack::Request.new(env)
        status, headers, response = @app.call(env)

        new_response = []

        response.each do |line|
          links = line.scan(/href="([^"]+)"/).uniq
          res = line
          links.each do |link|
            t = get_translation(link)
            res.gsub!(/href="#{link}"/, %q|href="#{t}"|) if t
          end
          new_response << res
        end

        [status, headers, new_response]
      end

      private

      def get_translation(url)
        ::Rewritten.list_range("to:#{url}", -1, 1)
      end

    end

  end
end


Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rewritten-0.2.1 lib/rack/html.rb
rewritten-0.2.0 lib/rack/html.rb
rewritten-0.1.0 lib/rack/html.rb
rewritten-0.0.4 lib/rack/html.rb
rewritten-0.0.3 lib/rack/html.rb