class Dps::DNS

Public Class Methods

decode_txt_record(value) click to toggle source
# File lib/dps/dns.rb, line 13
def decode_txt_record(value)
  parts = value.gsub('"', '').split
  
  head, tail = head_and_tail(parts)
  
  case head
  when "dps:endpoint"  
    Endpoint.new(tail)
  else
    InvalidRecord.new("Unexpected action '#{head}'")
  end
end
get_endpoint(domain) click to toggle source
# File lib/dps/dns.rb, line 26
def get_endpoint(domain)
  records = get_records(domain).select { |r| r.is_a?(Endpoint) }
  
  if records.length > 1
    TooManyRecords.new("Too many valid endpoint TXT records returned.")
  elsif records.length == 0
    NoRecords.new("No valid endpoint TXT records found.")
  else
    records[0]
  end
end
get_records(domain) click to toggle source
# File lib/dps/dns.rb, line 7
def get_records(domain)
  get_txt_records(domain).
  collect { |value| decode_txt_record(value) }.
  select(&:valid?)
end

Private Class Methods

dnsruby_config() click to toggle source
# File lib/dps/dns.rb, line 46
def dnsruby_config
  {
    nameserver: Dps.dns_nameservers
  }
end
get_txt_records(domain) click to toggle source
# File lib/dps/dns.rb, line 52
def get_txt_records(domain)
  result = []
  
  Dnsruby::DNS.open(dnsruby_config) do |dns|
    begin
      record = dns.getresource(domain, Dnsruby::Types::TXT)
      result << record.data
    rescue Dnsruby::NXDomain => e
      raise StandardError.new("Invalid domain '#{domain}'.")
    end
  end
  
  result
end
head_and_tail(array) click to toggle source
# File lib/dps/dns.rb, line 40
def head_and_tail(array)
  head = array[0]
  tail = array[1, array.length - 1]
  [head, tail]
end