Class: PublicSuffix::Domain
- Inherits:
-
Object
- Object
- PublicSuffix::Domain
- Defined in:
- lib/public_suffix/domain.rb
Class Method Summary collapse
-
.domain_to_labels(domain) ⇒ Array<String>
Splits a string into its possible labels as a domain in reverse order from the input string.
Instance Method Summary collapse
-
#domain ⇒ String
Returns a domain-like representation of this object if the object is a #domain?,
nil
otherwise. -
#domain? ⇒ Boolean
Checks whether
self
looks like a domain. -
#initialize(*args) {|self| ... } ⇒ Domain
constructor
Creates and returns a new Domain instance.
-
#is_a_domain? ⇒ Boolean
Checks whether
self
is exclusively a domain, and not a subdomain. -
#is_a_subdomain? ⇒ Boolean
Checks whether
self
is exclusively a subdomain. -
#name ⇒ String
Returns the full domain name.
-
#rule ⇒ PublicSuffix::Rule::Base?
Returns the rule matching this domain in the default List.
-
#sld ⇒ String?
Returns the Second Level Domain part, aka the domain part.
-
#subdomain ⇒ String
Returns a domain-like representation of this object if the object is a #subdomain?,
nil
otherwise. -
#subdomain? ⇒ Boolean
Checks whether
self
looks like a subdomain. -
#tld ⇒ String?
Returns the Top Level Domain part, aka the extension.
-
#to_a ⇒ Array<String, nil>
Returns an array containing the domain parts.
-
#to_s ⇒ String
Returns a string representation of this object.
-
#trd ⇒ String?
Returns the Third Level Domain part, aka the subdomain part.
-
#valid? ⇒ Boolean
Checks whether
self
is assigned and allowed according to default List. -
#valid_domain? ⇒ Boolean
Checks whether
self
looks like a domain and validates according to default List. -
#valid_subdomain? ⇒ Boolean
Checks whether
self
looks like a subdomain and validates according to default List.
Constructor Details
#initialize(tld) ⇒ Domain #initialize(tld, sld) ⇒ Domain #initialize(tld, sld, trd) ⇒ Domain
Creates and returns a new PublicSuffix::Domain instance.
66 67 68 69 |
# File 'lib/public_suffix/domain.rb', line 66 def initialize(*args, &block) @tld, @sld, @trd = args yield(self) if block_given? end |
Class Method Details
.domain_to_labels(domain) ⇒ Array<String>
Splits a string into its possible labels as a domain in reverse order from the input string.
The input is not validated, but it is assumed to be a valid domain.
32 33 34 |
# File 'lib/public_suffix/domain.rb', line 32 def self.domain_to_labels(domain) domain.to_s.split(".").reverse end |
Instance Method Details
#domain ⇒ String
Returns a domain-like representation of this object if the object is a #domain?, nil
otherwise.
PublicSuffix::Domain.new("com").domain
# => nil
PublicSuffix::Domain.new("com", "google").domain
# => "google.com"
PublicSuffix::Domain.new("com", "google", "www").domain
# => "www.google.com"
This method doesn’t validate the input. It handles the domain as a valid domain name and simply applies the necessary transformations.
# This is an invalid domain
PublicSuffix::Domain.new("zip", "google").domain
# => "google.zip"
This method returns a FQD, not just the domain part. To get the domain part, use #sld
(aka second level domain).
PublicSuffix::Domain.new("com", "google", "www").domain
# => "google.com"
PublicSuffix::Domain.new("com", "google", "www").sld
# => "google"
166 167 168 169 |
# File 'lib/public_suffix/domain.rb', line 166 def domain return unless domain? [sld, tld].join(".") end |
#domain? ⇒ Boolean
Checks whether self
looks like a domain.
This method doesn’t actually validate the domain. It only checks whether the instance contains a value for the #tld and #sld attributes. If you also want to validate the domain, use #valid_domain? instead.
248 249 250 |
# File 'lib/public_suffix/domain.rb', line 248 def domain? !(tld.nil? || sld.nil?) end |
#is_a_domain? ⇒ Boolean
Checks whether self
is exclusively a domain, and not a subdomain.
288 289 290 |
# File 'lib/public_suffix/domain.rb', line 288 def is_a_domain? domain? && !subdomain? end |
#is_a_subdomain? ⇒ Boolean
Checks whether self
is exclusively a subdomain.
295 296 297 |
# File 'lib/public_suffix/domain.rb', line 295 def is_a_subdomain? subdomain? end |
#name ⇒ String
Returns the full domain name.
129 130 131 |
# File 'lib/public_suffix/domain.rb', line 129 def name [trd, sld, tld].reject { |part| part.nil? }.join(".") end |
#rule ⇒ PublicSuffix::Rule::Base?
Returns the rule matching this domain in the default List.
215 216 217 |
# File 'lib/public_suffix/domain.rb', line 215 def rule List.default.find(name) end |
#sld ⇒ String?
Returns the Second Level Domain part, aka the domain part.
105 106 107 |
# File 'lib/public_suffix/domain.rb', line 105 def sld @sld end |
#subdomain ⇒ String
Returns a domain-like representation of this object if the object is a #subdomain?, nil
otherwise.
PublicSuffix::Domain.new("com").subdomain
# => nil
PublicSuffix::Domain.new("com", "google").subdomain
# => nil
PublicSuffix::Domain.new("com", "google", "www").subdomain
# => "www.google.com"
This method doesn’t validate the input. It handles the domain as a valid domain name and simply applies the necessary transformations.
# This is an invalid domain
PublicSuffix::Domain.new("zip", "google", "www").subdomain
# => "www.google.zip"
This method returns a FQD, not just the domain part. To get the domain part, use #tld
(aka third level domain).
PublicSuffix::Domain.new("com", "google", "www").subdomain
# => "www.google.com"
PublicSuffix::Domain.new("com", "google", "www").trd
# => "www"
204 205 206 207 |
# File 'lib/public_suffix/domain.rb', line 204 def subdomain return unless subdomain? [trd, sld, tld].join(".") end |
#subdomain? ⇒ Boolean
Checks whether self
looks like a subdomain.
This method doesn’t actually validate the subdomain. It only checks whether the instance contains a value for the #tld, #sld and #trd attributes. If you also want to validate the domain, use #valid_subdomain? instead.
280 281 282 |
# File 'lib/public_suffix/domain.rb', line 280 def subdomain? !(tld.nil? || sld.nil? || trd.nil?) end |
#tld ⇒ String?
Returns the Top Level Domain part, aka the extension.
98 99 100 |
# File 'lib/public_suffix/domain.rb', line 98 def tld @tld end |
#to_a ⇒ Array<String, nil>
Returns an array containing the domain parts.
90 91 92 |
# File 'lib/public_suffix/domain.rb', line 90 def to_a [trd, sld, tld] end |
#to_s ⇒ String
Returns a string representation of this object.
74 75 76 |
# File 'lib/public_suffix/domain.rb', line 74 def to_s name end |
#trd ⇒ String?
Returns the Third Level Domain part, aka the subdomain part.
112 113 114 |
# File 'lib/public_suffix/domain.rb', line 112 def trd @trd end |
#valid? ⇒ Boolean
325 326 327 328 |
# File 'lib/public_suffix/domain.rb', line 325 def valid? r = rule !r.nil? && r.allow?(name) end |
#valid_domain? ⇒ Boolean
Checks whether self
looks like a domain and validates according to default List.
354 355 356 |
# File 'lib/public_suffix/domain.rb', line 354 def valid_domain? domain? && valid? end |
#valid_subdomain? ⇒ Boolean
Checks whether self
looks like a subdomain and validates according to default List.
381 382 383 |
# File 'lib/public_suffix/domain.rb', line 381 def valid_subdomain? subdomain? && valid? end |