Sha256: fd75858c15845a5164fed7e6f51e136b93794458b72c2f001308723c1b249a17

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 KB

Contents

require "valid_email2"
require "resolv"
require "mail"

module ValidEmail2
  class Address
    attr_accessor :address

    def initialize(address)
      @parse_error = false
      @raw_address = address

      begin
        @address = Mail::Address.new(address)
      rescue Mail::Field::ParseError
        @parse_error = true
      end
    end

    def valid?
      return false if @parse_error

      if address.domain && address.address == @raw_address
        domain = address.domain
        # Valid address needs to have a dot in the domain
        !!domain.match(/\./) && !domain.match(/\.{2,}/)
      else
        false
      end
    end

    def disposable?
      valid? && domain_is_in?(ValidEmail2.disposable_emails)
    end

    def blacklisted?
      valid? && domain_is_in?(ValidEmail2.blacklist)
    end

    def valid_mx?
      return false unless valid?

      mx = []

      Resolv::DNS.open do |dns|
        mx.concat dns.getresources(address.domain, Resolv::DNS::Resource::IN::MX)
      end

      mx.any?
    end

    private

    def domain_is_in?(domain_list)
      domain_list.select { |domain|
        address.domain =~ (/^(.*\.)*#{domain}$/i)
      }.any?
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
valid_email2-1.2.9 lib/valid_email2/address.rb
valid_email2-1.2.8 lib/valid_email2/address.rb
valid_email2-1.2.7 lib/valid_email2/address.rb
valid_email2-1.2.6 lib/valid_email2/address.rb
valid_email2-1.2.5 lib/valid_email2/address.rb
valid_email2-1.2.4 lib/valid_email2/address.rb
valid_email2-1.2.3 lib/valid_email2/address.rb
valid_email2-1.2.2 lib/valid_email2/address.rb
valid_email2-1.2.1 lib/valid_email2/address.rb