Sha256: 24f8289c380216ff27d113c79431f7e3fe1385b5a13a62bcb4a627ba85d1dd3b

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require 'rack'
require 'rack/response'

# All(?) Rack code is namespaced within this module.
module Rack
  # Module includes our middleware components for managing service API versions.
  module ServiceApiVersioning
    # Build redirect URI from original request URI and API Version SBU.
    class BuildRedirectLocationUri
      def self.call(api_version_base_uri:, request_uri:)
        new(api_version_base_uri, request_uri).call
      end

      def call
        update_path
        update_query
        uri_for_redirect
      end

      protected

      def initialize(api_version_base_uri, request_uri)
        @new_base_uri_parts = api_version_base_uri.to_hash
        @request_uri = request_uri
        self
      end

      private

      attr_reader :new_base_uri_parts, :request_uri

      def combined_path_str
        new_base_uri_parts[:path] + request_uri.path
      end

      def path_str
        combined_path_str.sub('//', '/')
      end

      def update_path
        new_base_uri_parts[:path] = path_str
      end

      def update_query
        new_base_uri_parts[:query] = request_uri.query
      end

      def uri_for_redirect
        Addressable::URI.new(new_base_uri_parts)
      end
    end # class Rack::ServiceApiVersioning::BuildRedirectLocationUri
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-service_api_versioning-0.1.1 lib/rack/service_api_versioning/build_redirect_location_uri.rb
rack-service_api_versioning-0.1.0 lib/rack/service_api_versioning/build_redirect_location_uri.rb