Sha256: 030226c4c9c0d0b3554e3b2609734329e84d4ebe2529aa747d03d800f80d64c5

Contents?: true

Size: 652 Bytes

Versions: 3

Compression:

Stored size: 652 Bytes

Contents

# frozen_string_literal: true
module Aitch
  class Location
    attr_reader :redirect_stack, :current_url

    def initialize(redirect_stack, current_url)
      @redirect_stack = redirect_stack
      @current_url = current_url
    end

    def location
      return current_url unless current_url.match(%r[\A/])

      uri = find_uri_with_host
      url = ["#{uri.scheme}://#{uri.hostname}"]
      url << ":#{uri.port}" unless [80, 443].include?(uri.port)
      url << current_url
      url.join
    end

    def find_uri_with_host
      redirect_stack.reverse
        .map {|url| ::URI.parse(url) }
        .find {|uri| uri.scheme }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aitch-1.0.2 lib/aitch/location.rb
aitch-1.0.1 lib/aitch/location.rb
aitch-1.0.0 lib/aitch/location.rb