Class: MockDnsServer::MessageTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/mock_dns_server/message_transformer.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dns_message) ⇒ MessageTransformer

Initialize the transformer with a message.

Parameters:

  • dns_message

    can be either a Dnsruby::Message instance or binary wire data



10
11
12
# File 'lib/mock_dns_server/message_transformer.rb', line 10

def initialize(dns_message)
  self.message = dns_message
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



6
7
8
# File 'lib/mock_dns_server/message_transformer.rb', line 6

def message
  @message
end

Instance Method Details

#answer_count(answer_type) ⇒ Object



70
71
72
# 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_questionObject



60
61
62
63
64
65
66
67
# 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

#qclassObject

Returns the message’s qclass as a String.

Returns:

  • the message’s qclass as a String



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

def qclass
  question_attr(:qclass)
end

#qnameObject

Returns the message’s qname as a String.

Returns:

  • the message’s qname as a String



43
44
45
# File 'lib/mock_dns_server/message_transformer.rb', line 43

def qname
  question_attr(:qname)
end

#qtypeObject

Returns the message’s qtype as a String.

Returns:

  • the message’s qtype as a String



36
37
38
39
# 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) ⇒ Object



54
55
56
57
# 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) ⇒ Object

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



24
25
26
27
28
29
30
31
32
# 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