Sha256: 3e737c2bbf7b0283820a0277ea1a3035f3206f99dd2ae510b1cc3f99ea8079ce

Contents?: true

Size: 1.07 KB

Versions: 13

Compression:

Stored size: 1.07 KB

Contents

require "email_assessor"
require "resolv"
require "mail"

module EmailAssessor
  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,}/) && domain.match(/[a-z]\Z/i)
      else
        false
      end
    end

    def disposable?
      valid? && EmailAssessor.domain_is_disposable?(address.domain)
    end

    def blacklisted?
      valid? && EmailAssessor.domain_is_blacklisted?(address.domain)
    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
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
email_assessor-0.3.9 lib/email_assessor/address.rb
email_assessor-0.3.8 lib/email_assessor/address.rb
email_assessor-0.3.7 lib/email_assessor/address.rb
email_assessor-0.3.6 lib/email_assessor/address.rb
email_assessor-0.3.5 lib/email_assessor/address.rb
email_assessor-0.3.4 lib/email_assessor/address.rb
email_assessor-0.3.3 lib/email_assessor/address.rb
email_assessor-0.3.2 lib/email_assessor/address.rb
email_assessor-0.3.1 lib/email_assessor/address.rb
email_assessor-0.3 lib/email_assessor/address.rb
email_assessor-0.2.1 lib/email_assessor/address.rb
email_assessor-0.2 lib/email_assessor/address.rb
email_assessor-0.1 lib/email_assessor/address.rb