Class: DNSer::SrvRecord

Inherits:
BaseRecord show all
Defined in:
lib/dnser/records/srv.rb

Instance Attribute Summary

Attributes inherited from BaseRecord

#name, #ttl_val

Attributes inherited from Record

#domain

Instance Method Summary collapse

Methods inherited from BaseRecord

#ttl, #type

Methods inherited from Record

#comment, #full_host, #priority, #ttl, #type

Constructor Details

#initialize(domain, *args, &block) ⇒ SrvRecord

Returns a new instance of SrvRecord.

Raises:



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
# File 'lib/dnser/records/srv.rb', line 5

def initialize domain, *args, &block

  params = {}
  params = args.pop if args.last.is_a? Hash

  host = domain.host
  value = nil

  [:port, :weight, :protocol, :service].each do |m|
    instance_variable_set("@#{m}".to_sym, nil)
    self.class.send :define_method, m, proc { |*args|
      instance_variable_set("@#{m}", args.first) unless args.empty?
      instance_variable_get("@#{m}")
    }
  end

  params = {weight: 0, priority: 0, port: 0}.merge(params)

  params.each do |key, value|
    self.send key, value if  self.respond_to? key
  end

  super domain, :SRV, *args, &block

  raise DNSer::Record::EmptyValue.new(self), 'Service must be defined' unless @service
  raise DNSer::Record::EmptyValue.new(self), 'Protocol must be defined' unless @protocol
  raise DNSer::Record::EmptyValue.new(self), 'Port must be defined' if @port == 0
end

Instance Method Details

#hostObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dnser/records/srv.rb', line 34

def host
  first_part = [@service, @protocol].map(&:to_s).map {|i| i.start_with?('_') ? i : ('_' + i)} .join('.')

  short_host = collapse_domain @host
  if(short_host == '@')
    [first_part, domain.name ]
  else
    [first_part, short_host, domain.name]
  end .join('.')

end

#valueObject



46
47
48
49
50
51
52
53
54
# File 'lib/dnser/records/srv.rb', line 46

def value
  res = super.split(' ')
  case res.size
    when 1
      ['0', @weight, @port, res.first ]
    when 2
      [res.first, @weight, @port, res.last ]
  end .map(&:to_s).join(' ')
end