Class: Aitch::Location

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redirect_stack, current_url) ⇒ Location

Returns a new instance of Location.


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

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


5
6
7
# File 'lib/aitch/location.rb', line 5

def current_url
  @current_url
end

#redirect_stackObject (readonly)

Returns the value of attribute redirect_stack


5
6
7
# File 'lib/aitch/location.rb', line 5

def redirect_stack
  @redirect_stack
end

Instance Method Details

#find_uri_with_hostObject


22
23
24
25
26
# File 'lib/aitch/location.rb', line 22

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

#locationObject


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

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