Module: Mbrao::Validations::ClassMethods

Defined in:
lib/mbrao/parser.rb

Overview

Class methods.

Instance Method Summary collapse

Instance Method Details

#is_email?(text) ⇒ Boolean

Checks if the text is a valid email.

Parameters:

  • text (String)

    The text to check.

Returns:

  • (Boolean)

    true if the string is valid email, false otherwise.



136
137
138
139
# File 'lib/mbrao/parser.rb', line 136

def is_email?(text)
  regex = /^([a-z0-9_\.\-\+]+)@([\da-z\.\-]+)\.([a-z\.]{2,6})$/i
  text.ensure_string.strip =~ regex
end

#is_url?(text) ⇒ Boolean

Checks if the text is a valid URL.

Parameters:

  • text (String)

    The text to check.

Returns:

  • (Boolean)

    true if the string is valid URL, false otherwise.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/mbrao/parser.rb', line 145

def is_url?(text)
  regex = /
    ^(
      ([a-z0-9\-]+:\/\/) #PROTOCOL
      (([\w-]+\.)?) # LOWEST TLD
      ([\w-]+) # 2nd LEVEL TLD
      (\.[a-z]+) # TOP TLD
      ((:\d+)?) # PORT
      ([\S|\?]*) # PATH, QUERYSTRING AND FRAGMENT
    )$
  /ix

  text.ensure_string.strip =~ regex
end