module Varkon

Constants

VERSION

Public Instance Methods

compress_ipv6(value) click to toggle source
# File lib/varkon.rb, line 56
def compress_ipv6(value)
  IPAddr.new(value).to_s
rescue
  value
end
digit?(value) click to toggle source
# File lib/varkon.rb, line 112
def digit?(value)
  value.to_s =~ /^\d+$/
end
email?(value) click to toggle source
# File lib/varkon.rb, line 6
def email?(value)
  value =~ /\A                        # The local-part of addr-spec is specific in section 3.4.1 of RFC 5322.
    [a-z\d!#\$\%&'*+\-\/=?^_`{|}~]+ # It is composed of ASCII alphanumerics plus some puctionation characters
    (?:                             # It mau contain a period, but not as the first character, not as a the last
    \.                            # characters, and two periods must not be next to each other.
    [a-z\d!#\$\%&'*+\-\/=?^_`{|}~]+
    )*
    @
    (?:                             # Hostnames must start with an alphanumeric character, can contain
    [a-z\d]                       # alphanumeric or hyphens, must end with alphanumeric, and max
    (?:                           # label length is 63 chars. RFCs 1122, 1035, 952.
     [a-z\d\-]{0,61}
  [a-z\d]
    )?
    (?:
     \.(?!\z)|\z                 # Label must be separated by periods or must be the end of the string.
    )
    ){2,}                           # Must have at least two labels to be qualified.
  \z/ix
end
escape_sql_wildcards(value) click to toggle source
# File lib/varkon.rb, line 104
def escape_sql_wildcards(value)
  value.gsub(/[\\%_]/) { |x| '\\' + x }
end
guid?(value) click to toggle source
# File lib/varkon.rb, line 27
def guid?(value)
  value =~ /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
end
hostname?(value) click to toggle source
# File lib/varkon.rb, line 31
def hostname?(value)
  value =~ /\A
  (?:                 # Hostnames must start with an alphanumeric character, can contain
  [a-z\d]           # alphanumeric or hyphens, must end with alphanumeric, and min label
  (?:               # length is 1 and max length is 63 chars. RFCs 1122, 1035, 952.
   [a-z\d\-]{0,61}
  [a-z\d]
  )?
  (?:
   \.(?!\z)|\z     # Label must be separated by periods or must be the end of the string.
  )
  ){2,}               # Must have at least two labels to be qualified.
  \z/ix
end
ipv4?(value) click to toggle source
# File lib/varkon.rb, line 46
def ipv4?(value)
  value =~ /\A((?:(?:25[0-5]|2[0-4]\d|1?\d\d?)(?:\.(?!\z)|\z)){4})\z/
end
ipv6?(value) click to toggle source
# File lib/varkon.rb, line 50
def ipv6?(value)
  IPAddr.new(value).ipv6?
rescue
  false
end
mac_address?(value) click to toggle source
# File lib/varkon.rb, line 108
def mac_address?(value)
  value =~ /\A([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]){2}\z/
end
md5?(value) click to toggle source
# File lib/varkon.rb, line 66
def md5?(value)
  value =~ /\A\h{32}\z/
end
partial_url?(value) click to toggle source
# File lib/varkon.rb, line 82
def partial_url?(value)
  value =~ /\A            # URL with no scheme.
    (?:                 # Hostnames must start with an alphanumeric character, can contain
    [a-z\d]           # alphanumeric or hyphens, must end with alphanumeric, and min label
    (?:               # length is 1 and max length is 63 chars. RFCs 1122, 1035, 952.
     [a-z\d\-]{0,61}
  [a-z\d]
    )?
    (?:
     \.(?!:|\/)|(?=:|\/)  # Label must be separated by periods or must be followed by a port or path.
    )
    ){2,}               # Must have at least two labels to be qualified.
  (?:
   :\d+\/.*          # Followed by a port number and a path.
   |
   \/.*              # Or followed by a path.
     |
   :\d+              # Or followed by a port number.
  )
    \z/ix and !value.empty? and url? "http://#{value}"
end
sha1?(value) click to toggle source
# File lib/varkon.rb, line 70
def sha1?(value)
  value =~ /\A\h{40}\z/
end
sha256?(value) click to toggle source
# File lib/varkon.rb, line 74
def sha256?(value)
  value =~ /\A\h{64}\z/
end
url?(value) click to toggle source
# File lib/varkon.rb, line 78
def url?(value)
  ['http', 'https'].include?(URI.parse(value).scheme) rescue false
end
valid_ip?(address) click to toggle source
# File lib/varkon.rb, line 62
def valid_ip?(address)
  IpList::IPV4_RE.match(address)
end