module Wpxf::Utility::Text

Provides helper methods for text based operations.

Public Class Methods

alpha_ranges(casing) click to toggle source

@param casing [Symbol] the casing to use for the alpha characters.

Possible values are :mixed, :upper and :lower.

@return [Array] a range of alpha characters in the matching casing.

# File lib/wpxf/utility/text.rb, line 41
def self.alpha_ranges(casing)
  if casing == :mixed
    [*'A'..'Z', *'a'..'z']
  elsif casing == :upper
    [*'A'..'Z']
  elsif casing == :lower
    [*'a'..'z']
  end
end
hexify_string(value) click to toggle source

Convert each byte of a string to its hexadecimal value and concantenate them together, to provide a hexadecimal string. @param value [String] the string to hexify. @return [String] the hexadecimal string.

# File lib/wpxf/utility/text.rb, line 76
def self.hexify_string(value)
  value.each_byte.map { |b| b.to_s(16) }.join
end
md5(value) click to toggle source

Generate an MD5 hash of a string. @param value [String] the value to hash. @return [String] the MD5 hash.

# File lib/wpxf/utility/text.rb, line 54
def self.md5(value)
  digest = Digest::MD5.new
  digest.update(value)
  digest.hexdigest
end
rand_alpha(length, casing = :mixed) click to toggle source

Generate a random alpha string. @param length [Integer] the number of characters to include. @param casing [Symbol] the casing to use for the alpha characters.

Possible values are :mixed, :upper and :lower.

@return [String] a random alpha string.

# File lib/wpxf/utility/text.rb, line 34
def self.rand_alpha(length, casing = :mixed)
  Array.new(length) { alpha_ranges(casing).sample }.join
end
rand_alphanumeric(length, casing = :mixed) click to toggle source

Generate a random alphanumeric string. @param length [Integer] the number of characters to include. @param casing [Symbol] the casing to use for the alpha characters.

Possible values are :mixed, :upper and :lower.

@return [String] a random alphanumeric string.

# File lib/wpxf/utility/text.rb, line 24
def self.rand_alphanumeric(length, casing = :mixed)
  range = [*'0'..'9'] + alpha_ranges(casing)
  Array.new(length) { range.sample }.join
end
rand_email() click to toggle source

Generate a random e-mail address. @return [String] the e-mail address.

# File lib/wpxf/utility/text.rb, line 62
def self.rand_email
  "#{rand_alpha(rand(5..10))}@#{rand_alpha(rand(5..10))}.com"
end
rand_month() click to toggle source

Generate a random month name. @return [String] the month name.

# File lib/wpxf/utility/text.rb, line 68
def self.rand_month
  %w[january february march april june july august september october november december].sample
end
rand_numeric(length, allow_leading_zero = false) click to toggle source

Generate a random numeric string. @param length [Integer] the number of characters to include. @param allow_leading_zero [Boolean] if set to true, will allow a number starting with zero. @return [String] a random numeric string.

# File lib/wpxf/utility/text.rb, line 13
def self.rand_numeric(length, allow_leading_zero = false)
  value = Array.new(length) { [*'0'..'9'].sample }.join
  value[0] = [*'1'..'9'].sample unless allow_leading_zero
  value
end