Module: RFC822

Defined in:
lib/rfc822.rb

Defined Under Namespace

Modules: Patterns Classes: MXRecord

Constant Summary collapse

EMAIL =
/\A#{Patterns::ADDRESS}\z/
@@host_command =
'/usr/bin/env host'

Class Method Summary collapse

Class Method Details

.host_commandObject



28
29
30
# File 'lib/rfc822.rb', line 28

def host_command
  @@host_command
end

.host_command=(value) ⇒ Object



32
33
34
# File 'lib/rfc822.rb', line 32

def host_command=(value)
  @@host_command = value
end

.host_mx(domain) ⇒ Object



53
54
55
# File 'lib/rfc822.rb', line 53

def host_mx(domain)
  `#{host_command} -t MX #{domain}`      
end

.mx_records(address) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rfc822.rb', line 36

def mx_records(address)
  address = address.to_s
  return [] unless address =~ EMAIL
  
  Timeout::timeout(2) do 
    raw_mx_records(address.split('@').last).map do |priority, host|
      MXRecord.new(priority.to_i, host)
    end
  end
rescue Timeout::Error
  []
end

.raw_mx_records(domain) ⇒ Object



49
50
51
# File 'lib/rfc822.rb', line 49

def raw_mx_records(domain)      
  host_mx(domain).scan(/#{Regexp.escape(domain)}[\w ]+?(\d+) (#{URI::REGEXP::PATTERN::HOSTNAME})\./)      
end