Sha256: 4e1036345ac867c4286129ec5cd16a278d80a4df3d1d89e929dce20b14a00460

Contents?: true

Size: 968 Bytes

Versions: 1

Compression:

Stored size: 968 Bytes

Contents

require 'nokogiri'
require 'addressable'
require 'rack'

module Enjoy::Goto
  class Middleware

    def initialize(app, options = {})
      @app = app
      @excluded_hosts = options.delete(:excluded_hosts) || []
      self
    end

    def call(env)
      status, headers, body = @app.call(env)

      if headers['Content-Type'].to_s.include?('text/html')
        doc = Nokogiri::HTML.parse(body.body)
        doc.css("a[href]").each do |a|
          _href = a['href']
          if _href =~ /^(https?:)?\/\//i
            begin
              _host = Addressable::URI.parse(_href).host
              puts _href
              puts _host
              unless @excluded_hosts.include?(_host)
                a['href'] = Rails.application.routes.url_helpers.enjoy_goto_path(url: _href)
              end
            rescue
            end
          end
        end
        return [status, headers, [doc.to_html]]
      end

      [status, headers, body]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enjoy_cms_goto-0.3.5 lib/enjoy/goto/middleware.rb