Sha256: 504ad271d6904bb72fa533148bd3fd09a3174fffe3bdbf0a1b4f055e141c1760

Contents?: true

Size: 1.28 KB

Versions: 108

Compression:

Stored size: 1.28 KB

Contents

require 'uri'
require 'rack/request'
require 'rack/static'

module Rack
  module HalBrowser
    class Redirect

      def initialize(app, options = {}, &block)
        @app = app
        @excluded_paths = Array(options[:exclude]) << '/hal-browser'
        @hal_browser = Rack::Static.new(@app, :urls => ['/hal-browser'], :root => ::File.expand_path('../../../../vendor', __FILE__))
      end

      def call(env)
        request = Rack::Request.new(env)
        if match?(request)
          return [303, {'Location' => hal_browser_url_from_request(request)}, []]
        end
        @hal_browser.call(env)
      end

      private

      def match?(request)
        request.get? && prefers_html?(request) && path_not_excluded?(request)
      end

      def prefers_html?(request)
        # TODO: actually follow real HTTP content negotiation rules
        request.env.fetch('HTTP_ACCEPT', '').start_with?('text/html') && request.env.fetch('HTTP_ACCEPT', '').include?('json')
      end

      def path_not_excluded?(request)
        !@excluded_paths.detect{|excluded_path| request.path.start_with?(excluded_path) }
      end

      def hal_browser_url_from_request(request)
        url = URI.parse('/hal-browser/browser.html')
        url.fragment = request.path_info
        url.to_s
      end
    end
  end
end

Version data entries

108 entries across 108 versions & 1 rubygems

Version Path
pact_broker-2.23.3 lib/rack/hal_browser/redirect.rb
pact_broker-2.23.2 lib/rack/hal_browser/redirect.rb
pact_broker-2.23.1 lib/rack/hal_browser/redirect.rb
pact_broker-2.23.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.22.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.21.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.20.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.19.2 lib/rack/hal_browser/redirect.rb
pact_broker-2.19.1 lib/rack/hal_browser/redirect.rb
pact_broker-2.19.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.18.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.17.1 lib/rack/hal_browser/redirect.rb
pact_broker-2.17.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.16.1 lib/rack/hal_browser/redirect.rb
pact_broker-2.16.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.15.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.14.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.13.1 lib/rack/hal_browser/redirect.rb
pact_broker-2.13.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.12.0 lib/rack/hal_browser/redirect.rb