Class: UrlParser::Model
- Inherits:
-
Object
- Object
- UrlParser::Model
- Defined in:
- lib/url_parser/model.rb
Instance Attribute Summary collapse
-
#parsed_domain ⇒ Object
readonly
Returns the value of attribute parsed_domain.
-
#parsed_uri ⇒ Object
readonly
Returns the value of attribute parsed_uri.
Instance Method Summary collapse
-
#authority ⇒ Object
Userinfo and host.
-
#directory ⇒ Object
Any directories following the site within the URI.
-
#domain ⇒ Object
The domain name with the tld.
-
#filename ⇒ Object
Segment if a file extension is present.
-
#fragment ⇒ Object
Fragment identifier.
-
#host ⇒ Object
Hostname and port.
-
#hostname ⇒ Object
Fully qualified domain name or IP address.
-
#initialize(uri, domain = nil) ⇒ Model
constructor
A new instance of Model.
-
#location ⇒ Object
Directory and resource - everything after the site.
-
#naked_hostname ⇒ Object
Fully qualified domain name or IP address without ww? prefix.
-
#naked_trd ⇒ Object
(also: #naked_subdomain)
Any non-ww? subdomains.
-
#origin ⇒ Object
Scheme and host.
-
#password ⇒ Object
Password portion of the userinfo.
-
#path ⇒ Object
Directory and segment.
-
#port ⇒ Object
Port number.
-
#query ⇒ Object
Params and values as a string.
-
#query_values ⇒ Object
A hash of params and values.
-
#resource ⇒ Object
Path, query, and fragment.
-
#scheme ⇒ Object
Top level URI naming structure / protocol.
-
#segment ⇒ Object
Last portion of the path.
-
#site ⇒ Object
Scheme, userinfo, and host.
-
#sld ⇒ Object
(also: #second_level_domain, #domain_name)
Returns the second level domain portion, aka the domain part.
-
#subdomain ⇒ Object
All subdomains, include ww?.
-
#suffix ⇒ Object
The file extension of the filename.
-
#tld ⇒ Object
(also: #top_level_domain, #extension)
Returns the top level domain portion, aka the extension.
-
#trd ⇒ Object
(also: #third_level_domain, #subdomains)
Returns the third level domain portion, aka the subdomain part.
-
#userinfo ⇒ Object
URI username and password for authentication.
-
#username ⇒ Object
(also: #user)
Username portion of the userinfo.
-
#www ⇒ Object
The ww? portion of the subdomain.
Constructor Details
#initialize(uri, domain = nil) ⇒ Model
Returns a new instance of Model.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/url_parser/model.rb', line 6 def initialize(uri, domain = nil) unless uri.is_a?(Addressable::URI) raise RequiresAddressableURI, "#{uri} must be an Addressable::URI" end unless domain.is_a?(UrlParser::Domain) raise RequiresUrlParserDomain, "#{domain} must be a UrlParser::Domain" end if domain @parsed_uri = uri @parsed_domain = domain || UrlParser::Domain.new(uri.hostname) end |
Instance Attribute Details
#parsed_domain ⇒ Object (readonly)
Returns the value of attribute parsed_domain.
4 5 6 |
# File 'lib/url_parser/model.rb', line 4 def parsed_domain @parsed_domain end |
#parsed_uri ⇒ Object (readonly)
Returns the value of attribute parsed_uri.
4 5 6 |
# File 'lib/url_parser/model.rb', line 4 def parsed_uri @parsed_uri end |
Instance Method Details
#authority ⇒ Object
Userinfo and host.
133 134 135 |
# File 'lib/url_parser/model.rb', line 133 def parsed_uri. end |
#directory ⇒ Object
Any directories following the site within the URI.
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/url_parser/model.rb', line 157 def directory unless path.nil? || path.empty? parts = path.split('/') if parts.empty? '/' else parts.pop unless segment.to_s.empty? parts.unshift('') unless parts.first.to_s.empty? parts.compact.join('/') end end end |
#domain ⇒ Object
The domain name with the tld.
114 115 116 |
# File 'lib/url_parser/model.rb', line 114 def domain parsed_domain.domain end |
#filename ⇒ Object
Segment if a file extension is present.
172 173 174 |
# File 'lib/url_parser/model.rb', line 172 def filename segment.to_s[/.+\..+/] end |
#fragment ⇒ Object
Fragment identifier.
200 201 202 |
# File 'lib/url_parser/model.rb', line 200 def fragment parsed_uri.fragment end |
#host ⇒ Object
Hostname and port.
70 71 72 73 |
# File 'lib/url_parser/model.rb', line 70 def host result = [ hostname, port ].compact.join(':') result.empty? ? nil : result end |
#hostname ⇒ Object
Fully qualified domain name or IP address.
48 49 50 |
# File 'lib/url_parser/model.rb', line 48 def hostname parsed_uri.host end |
#location ⇒ Object
Directory and resource - everything after the site.
213 214 215 216 217 218 219 220 |
# File 'lib/url_parser/model.rb', line 213 def location if directory == '/' directory + resource.to_s else result = [ directory, resource ].compact.join('/') result.empty? ? nil : result end end |
#naked_hostname ⇒ Object
Fully qualified domain name or IP address without ww? prefix.
54 55 56 57 58 59 60 |
# File 'lib/url_parser/model.rb', line 54 def naked_hostname if www hostname.sub(/\A#{www}./, '') else hostname end end |
#naked_trd ⇒ Object Also known as: naked_subdomain
Any non-ww? subdomains.
107 108 109 |
# File 'lib/url_parser/model.rb', line 107 def naked_trd (trd && www) ? trd[/(?<=^#{www}\.).+/] : trd end |
#origin ⇒ Object
Scheme and host.
126 127 128 129 |
# File 'lib/url_parser/model.rb', line 126 def origin original_origin = parsed_uri.origin original_origin == "null" ? nil : original_origin end |
#password ⇒ Object
Password portion of the userinfo.
36 37 38 |
# File 'lib/url_parser/model.rb', line 36 def password parsed_uri.password end |
#path ⇒ Object
Directory and segment.
145 146 147 |
# File 'lib/url_parser/model.rb', line 145 def path parsed_uri.path end |
#port ⇒ Object
Port number.
64 65 66 |
# File 'lib/url_parser/model.rb', line 64 def port parsed_uri.port end |
#query ⇒ Object
Params and values as a string.
188 189 190 |
# File 'lib/url_parser/model.rb', line 188 def query parsed_uri.query end |
#query_values ⇒ Object
A hash of params and values.
194 195 196 |
# File 'lib/url_parser/model.rb', line 194 def query_values parsed_uri.query_values.to_h end |
#resource ⇒ Object
Path, query, and fragment.
206 207 208 209 |
# File 'lib/url_parser/model.rb', line 206 def resource name = [ segment, query_string, fragment_string ].compact.join name.empty? ? nil : name end |
#scheme ⇒ Object
Top level URI naming structure / protocol.
23 24 25 |
# File 'lib/url_parser/model.rb', line 23 def scheme parsed_uri.scheme end |
#segment ⇒ Object
Last portion of the path.
151 152 153 |
# File 'lib/url_parser/model.rb', line 151 def segment (path =~ /\/\z/ ? nil : path.split('/').last) if path end |
#site ⇒ Object
Scheme, userinfo, and host.
139 140 141 |
# File 'lib/url_parser/model.rb', line 139 def site parsed_uri.site end |
#sld ⇒ Object Also known as: second_level_domain, domain_name
Returns the second level domain portion, aka the domain part.
91 92 93 |
# File 'lib/url_parser/model.rb', line 91 def sld parsed_domain.sld end |
#subdomain ⇒ Object
All subdomains, include ww?.
120 121 122 |
# File 'lib/url_parser/model.rb', line 120 def subdomain parsed_domain.subdomain end |
#suffix ⇒ Object
The file extension of the filename.
178 179 180 181 182 183 184 |
# File 'lib/url_parser/model.rb', line 178 def suffix if path ext = File.extname(path) ext[0] = '' if ext[0] == '.' ext.empty? ? nil : ext end end |
#tld ⇒ Object Also known as: top_level_domain, extension
Returns the top level domain portion, aka the extension.
83 84 85 |
# File 'lib/url_parser/model.rb', line 83 def tld parsed_domain.tld end |
#trd ⇒ Object Also known as: third_level_domain, subdomains
Returns the third level domain portion, aka the subdomain part.
99 100 101 |
# File 'lib/url_parser/model.rb', line 99 def trd parsed_domain.trd end |
#userinfo ⇒ Object
URI username and password for authentication.
42 43 44 |
# File 'lib/url_parser/model.rb', line 42 def userinfo parsed_uri.userinfo end |
#username ⇒ Object Also known as: user
Username portion of the userinfo.
29 30 31 |
# File 'lib/url_parser/model.rb', line 29 def username parsed_uri.user end |
#www ⇒ Object
The ww? portion of the subdomain.
77 78 79 |
# File 'lib/url_parser/model.rb', line 77 def www trd.split('.').first.to_s[/www?\d*/] if trd end |