Class: HttpUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/http_url.rb

Constant Summary collapse

HTTP =
'http'
HTTPS =
'https'

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ HttpUrl

Returns a new instance of HttpUrl.



8
9
10
11
12
13
14
15
16
17
# File 'lib/http_url.rb', line 8

def initialize(url)
  url = normalize(url)
  if url =~ /^#{URI::regexp}$/
    begin
      @uri = URI.parse(url)
    rescue 
    end
  end
  @uri ||= nil
end

Instance Method Details

#fragmentObject



57
58
59
# File 'lib/http_url.rb', line 57

def fragment
  @uri.fragment if valid?
end

#hostObject



35
36
37
38
39
40
# File 'lib/http_url.rb', line 35

def host
  if valid? && @uri.host.start_with?('www.')
    return @uri.host[4..-1]
  end
  @uri.host if valid?
end

#http?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/http_url.rb', line 23

def http?
  valid? && @uri.scheme && @uri.scheme.downcase == HTTP
end

#https?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/http_url.rb', line 27

def https?
  valid? && @uri.scheme && @uri.scheme.downcase == HTTPS
end

#pathObject



49
50
51
# File 'lib/http_url.rb', line 49

def path
  @uri.path if valid?
end

#portObject



42
43
44
45
46
47
# File 'lib/http_url.rb', line 42

def port
  if valid? && @uri.port.nil?
    return 80
  end
  @uri.port.to_i if valid?
end

#queryObject



53
54
55
# File 'lib/http_url.rb', line 53

def query
  @uri.query if valid?
end

#schemeObject



31
32
33
# File 'lib/http_url.rb', line 31

def scheme
  @uri.scheme if valid?
end

#to_sObject



61
62
63
# File 'lib/http_url.rb', line 61

def to_s
  @uri.to_s if valid?
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/http_url.rb', line 19

def valid?
  @uri && !@uri.to_s.include?(' ') && @uri.host.include?('.')
end