Module: NaughtyOrNice

Defined in:
lib/naughty_or_nice.rb,
lib/naughty_or_nice/version.rb

Overview

Primary module to be mixed into the child class

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

EMAIL_REGEX =

Source: bit.ly/1n2X9iv

%r{
  ^
  (
    [\w!\#$%&'*+\-/=?\^`\{|\}~]+
    \.
  )
  *
  [\w!\#$%&'*+\-/=?\^`\{|\}~]+
  @
  (
    (
      (
        (
          (
            [a-z0-9]{1}
            [a-z0-9\-]{0,62}
            [a-z0-9]{1}
          )
          |
          [a-z]
        )
        \.
      )+
      [a-z]{2,6}
    )
    |
    (
      \d{1,3}
      \.
    ){3}
    \d{1,3}
    (
      :\d{1,5}
    )?
  )
  $
}xi.freeze
VERSION =
'2.1.2'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



55
56
57
# File 'lib/naughty_or_nice.rb', line 55

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#domainObject

Return the public suffix domain object

Supports all domain strings (URLs, emails)

Returns the domain object or nil, but no errors, never an error



73
74
75
76
77
78
79
80
81
# File 'lib/naughty_or_nice.rb', line 73

def domain
  return @domain if defined? @domain

  @domain = begin
    PublicSuffix.parse(normalized_domain, default_rule: nil)
  rescue PublicSuffix::DomainInvalid, PublicSuffix::DomainNotAllowed
    nil
  end
end

#email?Boolean

Is the input text in the form of a valid email address?

Returns true if email, otherwise false

Returns:

  • (Boolean)


93
94
95
# File 'lib/naughty_or_nice.rb', line 93

def email?
  !(@text =~ EMAIL_REGEX).nil?
end

#initialize(domain) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/naughty_or_nice.rb', line 59

def initialize(domain)
  if domain.is_a?(PublicSuffix::Domain)
    @domain = domain
    @text   = domain.to_s
  else
    @text = domain.to_s.downcase.strip
  end
end

#inspectObject



102
103
104
# File 'lib/naughty_or_nice.rb', line 102

def inspect
  "#<#{self.class} domain=\"#{domain}\" valid=#{valid?}>"
end

#to_sObject

Return the parsed domain as a string



98
99
100
# File 'lib/naughty_or_nice.rb', line 98

def to_s
  @to_s ||= domain.to_s if domain
end

#valid?Boolean

Checks if the input string represents a valid domain

Returns boolean true if a valid domain, otherwise false

Returns:

  • (Boolean)


86
87
88
# File 'lib/naughty_or_nice.rb', line 86

def valid?
  !domain.nil?
end