File: mx.rb

package info (click to toggle)
ruby-net-dns 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 496 kB
  • sloc: ruby: 4,157; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 2,121 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module Net # :nodoc:
  module DNS
    class RR
      #
      # = Mail Exchange Record (MX)
      #
      # Class for DNS MX resource records.
      #
      # A MX record specifies the name and relative preference of mail servers
      # (mail exchangers in the DNS jargon) for the zone.
      # The MX RR is used by SMTP (Mail) Agents to route mail for the domain.
      #
      class MX < RR
        # Gets the preference value.
        #
        # Returns an Integer.
        attr_reader :preference

        # Gets the exchange value.
        #
        # Returns a String.
        attr_reader :exchange

        # Gets the standardized value for this record,
        # represented by the value of <tt>preference</tt> and <tt>exchange</tt>.
        #
        # Returns a String.
        def value
          "#{preference} #{exchange}"
        end

        private

        def subclass_new_from_hash(options)
          if options.key?(:preference) && options.key?(:exchange)
            @preference = options[:preference].to_i
            @exchange = options[:exchange]
          else
            raise ArgumentError, ":preference and :exchange fields are mandatory"
          end
        end

        def subclass_new_from_string(str)
          @preference, @exchange = check_mx(str)
        end

        def subclass_new_from_binary(data, offset)
          @preference = data.unpack("@#{offset} n")[0]
          offset += 2
          @exchange, offset = dn_expand(data, offset)
          offset
        end

        def set_type
          @type = Net::DNS::RR::Types.new("MX")
        end

        def get_inspect
          value
        end

        def check_mx(input)
          str = input.to_s
          unless str.strip =~ /^(\d+)\s+(\S+)$/
            raise ArgumentError, "Invalid MX section `#{str}'"
          end

          [Regexp.last_match(1).to_i, Regexp.last_match(2)]
        end

        def build_pack
          @mx_pack = [@preference].pack("n") + pack_name(@exchange)
          @rdlength = @mx_pack.size
        end

        def get_data
          @mx_pack
        end
      end
    end
  end
end