Class: Nous::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/nous/primitives/url.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Url

Returns a new instance of Url.



7
8
9
10
11
12
# File 'lib/nous/primitives/url.rb', line 7

def initialize(raw)
  @uri = Addressable::URI.parse(raw.to_s.strip).normalize
  raise ArgumentError, "invalid URL: #{raw}" if uri.to_s.empty?
rescue Addressable::URI::InvalidURIError => e
  raise ArgumentError, "invalid URL: #{e.message}"
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



32
33
34
# File 'lib/nous/primitives/url.rb', line 32

def ==(other)
  other.is_a?(Url) && to_s == other.to_s
end

#hash ⇒ Object



37
38
39
# File 'lib/nous/primitives/url.rb', line 37

def hash
  to_s.hash
end

#host ⇒ Object



14
15
16
# File 'lib/nous/primitives/url.rb', line 14

def host
  uri.host
end

#http? ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/nous/primitives/url.rb', line 24

def http?
  %w[http https].include?(uri.scheme)
end

#path ⇒ Object



18
19
20
21
22
# File 'lib/nous/primitives/url.rb', line 18

def path
  return "/" if uri.path.empty?

  uri.path
end

#to_s ⇒ Object



28
29
30
# File 'lib/nous/primitives/url.rb', line 28

def to_s
  uri.to_s
end