Sha256: 1889575a0228debe0ed29d66b08c0821b863e7dd65d363620c9384b002bebb3c

Contents?: true

Size: 1.76 KB

Versions: 12

Compression:

Stored size: 1.76 KB

Contents

module MoreCoreExtensions
  module StringFormats
    # From: http://www.regular-expressions.info/email.html
    RE_EMAIL =  %r{\A[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z}i

    def email?
      !!(self =~ RE_EMAIL)
    end

    # From: Regular Expression Cookbook: 7.15 Validating Domain Names
    RE_DOMAINNAME = %r{^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$}i
    def domain_name?
      !!(self =~ RE_DOMAINNAME)
    end

    # Based on RFC 1034
    RE_HOSTNAME = %r{^((?![0-9]*$)+(?![0-9]*\.))(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])$}i
    def hostname?
      !!(self =~ RE_HOSTNAME)
    end

    # From: Regular Expression Cookbook: 7.16 Matching IPv4 Addresses
    RE_IPV4 = %r{^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$}
    def ipv4?
      !!(self =~ RE_IPV4)
    end

    # From: Regular Expression Cookbook: 7.17 Matching IPv6 Addresses
    RE_IPV6 = %r{^(?:(?:(?:[A-F0-9]{1,4}:){6}|(?=(?:[A-F0-9]{0,4}:){0,6}(?:[0-9]{1,3}\.){3}[0-9]{1,3}$)(([0-9A-F]{1,4}:){0,5}|:)((:[0-9A-F]{1,4}){1,5}:|:))(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}|(?=(?:[A-F0-9]{0,4}:){0,7}[A-F0-9]{0,4}$)(([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}|:))$}i
    def ipv6?
      !!(self =~ RE_IPV6)
    end

    def ipaddress?
      ipv4? || ipv6?
    end

    RE_INTEGER = %r{^-?[0-9]+$}
    def integer?
      !!(self =~ RE_INTEGER)
    end

    RE_GUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
    def guid?
      !!(self =~ RE_GUID)
    end
  end
end

String.send(:include, MoreCoreExtensions::StringFormats)

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
more_core_extensions-4.5.1 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-4.5.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-4.4.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-4.3.1 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-4.3.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-4.2.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-4.1.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-4.0.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-3.8.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-3.7.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-3.6.0 lib/more_core_extensions/core_ext/string/formats.rb
more_core_extensions-3.5.0 lib/more_core_extensions/core_ext/string/formats.rb