Module: Mbrao::Validations::ClassMethods

Defined in:
lib/mbrao/parser.rb

Overview

Class methods.

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) is_email?(text)

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.



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

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

- (Boolean) is_url?(text)

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.



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

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

- (Array) sanitized_array(object, uniq = true, compact = false, sanitize_method = :ensure_string, &block)

Converts an object to a a flatten array with all values sanitized.

Parameters:

  • object (Object)

    The object to convert.

  • uniq (Boolean) (defaults to: true)

    If to remove duplicates from the array before sanitizing.

  • compact (Boolean) (defaults to: false)

    If to compact the array before sanitizing.

  • sanitize_method (Symbol|nil) (defaults to: :ensure_string)

    If not nil, the method to use to sanitize entries. Ignored if a block is present.

  • block (Proc)

    A block to sanitize entries. It must accept the value as unique argument.

Returns:

  • (Array)

    An flattened array whose all values are strings.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/mbrao/parser.rb', line 188

def sanitized_array(object, uniq = true, compact = false, sanitize_method = :ensure_string, &block)
  rv = object.ensure_array.flatten
  rv.uniq! if uniq
  rv.compact! if compact

  if block then
    rv = rv.collect(&block)
  elsif sanitize_method then
    rv = rv.collect(&sanitize_method)
  end

  rv.uniq! if uniq
  rv.compact! if compact
  rv
end

- (Object) sanitized_hash(object, sanitize_method = nil, &block)

Converts an object making sure that every Hash is converted to a HashWithIndifferentAccess.

Parameters:

  • object (Object)

    The object to convert. If the object is not an Hash or doesn’t respond to collect then the original object is returned.

  • sanitize_method (Symbol|nil) (defaults to: nil)

    If not nil, the method to use to sanitize entries. Ignored if a block is present.

  • block (Proc)

    A block to sanitize entries. It must accept the value as unique argument.

Returns:

  • (Object)

    The converted object.



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mbrao/parser.rb', line 167

def sanitized_hash(object, sanitize_method = nil, &block)
  if object.is_a?(Hash) || object.is_a?(HashWithIndifferentAccess) then
    object.inject(HashWithIndifferentAccess.new) do |hash, pair|
      hash[pair[0]] = Mbrao::Parser.sanitized_hash(pair[1], sanitize_method, &block)
      hash
    end
  elsif object.respond_to?(:collect) then
    object.collect {|item| Mbrao::Parser.sanitized_hash(item, sanitize_method, &block) }
  else
    sanitized_hash_entry(object, sanitize_method, &block)
  end
end