Module: NaughtyOrNice

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

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
VERSION =
"2.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Ruby idiom that allows ‘include` to create class methods



53
54
55
# File 'lib/naughty_or_nice.rb', line 53

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



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

def domain
  return @domain if defined? @domain
  
  @domain = begin
    PublicSuffix.parse(domain_text)
  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)


91
92
93
# File 'lib/naughty_or_nice.rb', line 91

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

#initialize(domain) ⇒ Object



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

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



100
101
102
# File 'lib/naughty_or_nice.rb', line 100

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

#to_sObject

Return the parsed domain as a string



96
97
98
# File 'lib/naughty_or_nice.rb', line 96

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)


84
85
86
# File 'lib/naughty_or_nice.rb', line 84

def valid?
  !!(domain && domain.valid?)
end