Sha256: 7d8c01aad91b2e06d84c520f2a682b5b7d06833949cb5013394b645a303acf65

Contents?: true

Size: 1.71 KB

Versions: 30

Compression:

Stored size: 1.71 KB

Contents

require 'pact_broker/ui/controllers/relationships'
require 'pact_broker/ui/controllers/groups'
require 'pact_broker/doc/controllers/app'

module PactBroker
  module UI
    class App

      def initialize
        @app = ::Rack::Builder.new {

          use HtmlFilter

          map "/ui/relationships" do
            run PactBroker::UI::Controllers::Relationships
          end

          map "/groups" do
            run PactBroker::UI::Controllers::Groups
          end

          map "/doc" do
            run PactBroker::Doc::Controllers::App
          end

          map "/" do
            run RedirectRootToRelationships
          end
        }
      end

      def call env
        @app.call(env)
      end

      class RedirectRootToRelationships

        def self.call env
          # A request for the root path in the browser (not the json index) should
          # redirect to ui/relationships
          if (env['PATH_INFO'].chomp("/") == "")
            [303, {'Location' =>  "#{env['SCRIPT_NAME']}/ui/relationships"},[]]
          else
            [404, {},[]]
          end
        end

      end

      class HtmlFilter

        def initialize app
          @app = app
        end

        def call env
          if accepts_html_and_not_json_or_csv env
            @app.call(env)
          else
            [404, {},[]]
          end
        end

        def accepts_html_and_not_json_or_csv env
          accept = env['HTTP_ACCEPT'] || ''
          accepts_html(accept) && !accepts_json_or_csv(accept)
        end

        def accepts_html(accept)
          accept.include?("html")
        end

        def accepts_json_or_csv accept
          accept.include?("json") || accept.include?("csv")
        end

      end

    end
  end

end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
pact_broker-2.0.3 lib/pact_broker/ui/app.rb
pact_broker-2.0.2 lib/pact_broker/ui/app.rb
pact_broker-2.0.1 lib/pact_broker/ui/app.rb
pact_broker-2.0.0 lib/pact_broker/ui/app.rb
pact_broker-2.0.0.beta.8 lib/pact_broker/ui/app.rb
pact_broker-2.0.0.beta.7 lib/pact_broker/ui/app.rb
pact_broker-2.0.0.beta.6 lib/pact_broker/ui/app.rb
pact_broker-1.18.0 lib/pact_broker/ui/app.rb
pact_broker-2.0.0.beta.5 lib/pact_broker/ui/app.rb
pact_broker-1.18.0.beta.1 lib/pact_broker/ui/app.rb
pact_broker-1.17.2 lib/pact_broker/ui/app.rb
pact_broker-1.17.1 lib/pact_broker/ui/app.rb
pact_broker-2.0.0.beta.4 lib/pact_broker/ui/app.rb
pact_broker-2.0.0.beta.3 lib/pact_broker/ui/app.rb
pact_broker-2.0.0.beta.2 lib/pact_broker/ui/app.rb
pact_broker-2.0.0.beta.1 lib/pact_broker/ui/app.rb
pact_broker-1.17.0 lib/pact_broker/ui/app.rb
pact_broker-1.16.0 lib/pact_broker/ui/app.rb
pact_broker-1.15.0 lib/pact_broker/ui/app.rb
pact_broker-1.14.0 lib/pact_broker/ui/app.rb