Sha256: e4c662a0833df4a627a2ab9c0d7117bfa57df6a86ee239cc273ffa0ac8ca7c58

Contents?: true

Size: 1.25 KB

Versions: 59

Compression:

Stored size: 1.25 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'
      end

      def call(env)
        request = Rack::Request.new(env)
        if match?(request)
          return [303, {'Location' => hal_browser_url_from_request(request)}, []]
        end
        Rack::Static.new(@app, :urls => ['/hal-browser'], :root => ::File.expand_path('../../../../vendor', __FILE__)).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

59 entries across 59 versions & 1 rubygems

Version Path
pact_broker-1.18.0 lib/rack/hal_browser/redirect.rb
pact_broker-2.0.0.beta.5 lib/rack/hal_browser/redirect.rb
pact_broker-1.18.0.beta.1 lib/rack/hal_browser/redirect.rb
pact_broker-1.17.2 lib/rack/hal_browser/redirect.rb
pact_broker-1.17.1 lib/rack/hal_browser/redirect.rb
pact_broker-2.0.0.beta.4 lib/rack/hal_browser/redirect.rb
pact_broker-2.0.0.beta.3 lib/rack/hal_browser/redirect.rb
pact_broker-2.0.0.beta.2 lib/rack/hal_browser/redirect.rb
pact_broker-2.0.0.beta.1 lib/rack/hal_browser/redirect.rb
pact_broker-1.17.0 lib/rack/hal_browser/redirect.rb
pact_broker-1.16.0 lib/rack/hal_browser/redirect.rb
pact_broker-1.15.0 lib/rack/hal_browser/redirect.rb
pact_broker-1.14.0 lib/rack/hal_browser/redirect.rb
pact_broker-1.13.0 lib/rack/hal_browser/redirect.rb
pact_broker-1.12.0 lib/rack/hal_browser/redirect.rb
pact_broker-1.11.2 lib/rack/hal_browser/redirect.rb
pact_broker-1.11.1 lib/rack/hal_browser/redirect.rb
pact_broker-1.11.0 lib/rack/hal_browser/redirect.rb
pact_broker-1.10.0 lib/rack/hal_browser/redirect.rb
pact_broker-1.9.3 lib/rack/hal_browser/redirect.rb