class MockDnsServer::MessageTransformer

Lambdas that transform a message into something else, usually a message component such as domain or qtype.

Attributes

message[R]

Public Class Methods

new(dns_message) click to toggle source

Initialize the transformer with a message. @param dns_message can be either a Dnsruby::Message instance or binary wire data

# File lib/mock_dns_server/message_transformer.rb, line 10
def initialize(dns_message)
  self.message = dns_message
end

Public Instance Methods

answer_count(answer_type) click to toggle source
# File lib/mock_dns_server/message_transformer.rb, line 70
def answer_count(answer_type)
  message.answer.select { |a| a.rr_type.to_s == answer_type}.count
end
first_question() click to toggle source
# File lib/mock_dns_server/message_transformer.rb, line 60
def first_question
  has_question = message &&
      message.question &&
      message.question.first &&
      message.question.first.is_a?(Dnsruby::Question)

  has_question ? message.question.first : nil
end
message=(dns_message) click to toggle source
# File lib/mock_dns_server/message_transformer.rb, line 15
def message=(dns_message)
  @message = dns_message.is_a?(String) ? Dnsruby::Message.decode(dns_message) : dns_message
end
qclass() click to toggle source

@return the message's qclass as a String

# File lib/mock_dns_server/message_transformer.rb, line 49
def qclass
  question_attr(:qclass)
end
qname() click to toggle source

@return the message's qname as a String

# File lib/mock_dns_server/message_transformer.rb, line 43
def qname
  question_attr(:qname)
end
qtype() click to toggle source

@return the message's qtype as a String

# File lib/mock_dns_server/message_transformer.rb, line 36
def qtype
  dnsruby_type_instance = question_attr(:qtype)
  Dnsruby::Types.to_string(dnsruby_type_instance)
end
question_attr(symbol) click to toggle source
# File lib/mock_dns_server/message_transformer.rb, line 54
def question_attr(symbol)
  question = first_question
  question ? question.send(symbol).to_s : nil
end
serial(location = :answer) click to toggle source

A SOA record is usually in the answer section, but in the case of IXFR requests it will be in the authority section.

@location defaults to :answer, can override w/:authority

# File lib/mock_dns_server/message_transformer.rb, line 24
def serial(location = :answer)
  return nil if message.nil?

  target_section = message.send(location == :answer ? :answer : :authority)
  return nil if target_section.nil?

  soa_answer = target_section.detect { |record| record.is_a?(Dnsruby::RR::IN::SOA) }
  soa_answer ? soa_answer.serial : nil
end