Class: Aitch::Location

Inherits:
Object show all
Defined in:
lib/aitch/location.rb

Constant Summary collapse

MATCHER =
%r{\A/}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redirect_stack, current_url) ⇒ Location

Returns a new instance of Location.



9
10
11
12
# File 'lib/aitch/location.rb', line 9

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

Instance Attribute Details

#current_urlObject (readonly)

Returns the value of attribute current_url.



7
8
9
# File 'lib/aitch/location.rb', line 7

def current_url
  @current_url
end

#redirect_stackObject (readonly)

Returns the value of attribute redirect_stack.



7
8
9
# File 'lib/aitch/location.rb', line 7

def redirect_stack
  @redirect_stack
end

Instance Method Details

#find_uri_with_hostObject



24
25
26
27
28
# File 'lib/aitch/location.rb', line 24

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

#locationObject



14
15
16
17
18
19
20
21
22
# File 'lib/aitch/location.rb', line 14

def location
  return current_url unless current_url.match?(MATCHER)

  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