Class: HttpUrl
- Inherits:
-
Object
- Object
- HttpUrl
- Defined in:
- lib/http_url.rb
Constant Summary collapse
- HTTP =
'http'
- HTTPS =
'https'
Instance Method Summary collapse
- #fragment ⇒ Object
- #host ⇒ Object
- #http? ⇒ Boolean
- #https? ⇒ Boolean
-
#initialize(url) ⇒ HttpUrl
constructor
A new instance of HttpUrl.
- #path ⇒ Object
- #port ⇒ Object
- #query ⇒ Object
- #scheme ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
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
#fragment ⇒ Object
57 58 59 |
# File 'lib/http_url.rb', line 57 def fragment @uri.fragment if valid? end |
#host ⇒ Object
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
23 24 25 |
# File 'lib/http_url.rb', line 23 def http? valid? && @uri.scheme && @uri.scheme.downcase == HTTP end |
#https? ⇒ Boolean
27 28 29 |
# File 'lib/http_url.rb', line 27 def https? valid? && @uri.scheme && @uri.scheme.downcase == HTTPS end |
#path ⇒ Object
49 50 51 |
# File 'lib/http_url.rb', line 49 def path @uri.path if valid? end |
#port ⇒ Object
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 |
#query ⇒ Object
53 54 55 |
# File 'lib/http_url.rb', line 53 def query @uri.query if valid? end |
#scheme ⇒ Object
31 32 33 |
# File 'lib/http_url.rb', line 31 def scheme @uri.scheme if valid? end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/http_url.rb', line 61 def to_s @uri.to_s if valid? end |
#valid? ⇒ Boolean
19 20 21 |
# File 'lib/http_url.rb', line 19 def valid? @uri && !@uri.to_s.include?(' ') && @uri.host.include?('.') end |