Class: PublicSuffix::Domain
- Inherits:
-
Object
- Object
- PublicSuffix::Domain
- Defined in:
- lib/public_suffix/domain.rb
Overview
Domain represents a domain name, composed by a TLD, SLD and TRD.
Instance Attribute Summary collapse
-
#sld ⇒ Object
readonly
Returns the value of attribute sld.
-
#tld ⇒ Object
readonly
Returns the value of attribute tld.
-
#trd ⇒ Object
readonly
Returns the value of attribute trd.
Class Method Summary collapse
-
.name_to_labels(name) ⇒ Array<String>
Splits a string into the labels, that is the dot-separated parts.
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.
-
#name ⇒ String
Returns the full domain name.
-
#subdomain ⇒ String
Returns a subdomain-like representation of this object if the object is a #subdomain?,
nil
otherwise. -
#subdomain? ⇒ Boolean
Checks whether
self
looks like a subdomain. -
#to_a ⇒ Array<String, nil>
Returns an array containing the domain parts.
-
#to_s ⇒ String
Returns a string representation of this object.
Constructor Details
#initialize(tld) ⇒ Domain #initialize(tld, sld) ⇒ Domain #initialize(tld, sld, trd) ⇒ Domain
Creates and returns a new PublicSuffix::Domain instance.
65 66 67 68 |
# File 'lib/public_suffix/domain.rb', line 65 def initialize(*args) @tld, @sld, @trd = args yield(self) if block_given? end |
Instance Attribute Details
#sld ⇒ Object (readonly)
Returns the value of attribute sld.
33 34 35 |
# File 'lib/public_suffix/domain.rb', line 33 def sld @sld end |
#tld ⇒ Object (readonly)
Returns the value of attribute tld.
33 34 35 |
# File 'lib/public_suffix/domain.rb', line 33 def tld @tld end |
#trd ⇒ Object (readonly)
Returns the value of attribute trd.
33 34 35 |
# File 'lib/public_suffix/domain.rb', line 33 def trd @trd end |
Class Method Details
.name_to_labels(name) ⇒ Array<String>
Splits a string into the labels, that is the dot-separated parts.
The input is not validated, but it is assumed to be a valid domain name.
28 29 30 |
# File 'lib/public_suffix/domain.rb', line 28 def self.name_to_labels(name) name.to_s.split(DOT) 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 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"
137 138 139 |
# File 'lib/public_suffix/domain.rb', line 137 def domain [@sld, @tld].join(DOT) if domain? end |
#domain? ⇒ Boolean
198 199 200 |
# File 'lib/public_suffix/domain.rb', line 198 def domain? !(@tld.nil? || @sld.nil?) end |
#name ⇒ String
Returns the full domain name.
105 106 107 |
# File 'lib/public_suffix/domain.rb', line 105 def name [@trd, @sld, @tld].compact.join(DOT) end |
#subdomain ⇒ String
Returns a subdomain-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 method returns a FQD, not just the subdomain part. To get the subdomain part, use #trd
(aka third level domain).
PublicSuffix::Domain.new("com", "google", "www").subdomain
# => "www.google.com"
PublicSuffix::Domain.new("com", "google", "www").trd
# => "www"
169 170 171 |
# File 'lib/public_suffix/domain.rb', line 169 def subdomain [@trd, @sld, @tld].join(DOT) if subdomain? end |
#subdomain? ⇒ Boolean
229 230 231 |
# File 'lib/public_suffix/domain.rb', line 229 def subdomain? !(@tld.nil? || @sld.nil? || @trd.nil?) end |
#to_a ⇒ Array<String, nil>
Returns an array containing the domain parts.
89 90 91 |
# File 'lib/public_suffix/domain.rb', line 89 def to_a [@trd, @sld, @tld] end |
#to_s ⇒ String
Returns a string representation of this object.
73 74 75 |
# File 'lib/public_suffix/domain.rb', line 73 def to_s name end |