Class: Zonesync::Zonefile

Inherits:
Object
  • Object
show all
Defined in:
lib/zonesync/zonefile.rb

Constant Summary collapse

DUMMY_SOA =
"@ 1 SOA example.com example.com ( 2000010101 1 1 1 1 )\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, origin:) ⇒ Zonefile

Returns a new instance of Zonefile.



37
38
39
40
# File 'lib/zonesync/zonefile.rb', line 37

def initialize(records, origin:)
  @records = records
  @origin = origin
end

Instance Attribute Details

#originObject (readonly)

Returns the value of attribute origin.



43
44
45
# File 'lib/zonesync/zonefile.rb', line 43

def origin
  @origin
end

#recordsObject (readonly)

Returns the value of attribute records.



42
43
44
# File 'lib/zonesync/zonefile.rb', line 42

def records
  @records
end

Class Method Details

.ensure_soa(zone_string) ⇒ Object

Inserts a dummy SOA record if needed for parsing. Returns [modified_content, insertion_offset] where insertion_offset is nil if no SOA was added, or the byte position and length of the inserted SOA.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zonesync/zonefile.rb', line 12

def self.ensure_soa(zone_string)
  if zone_string =~ /\sSOA\s/
    [zone_string, nil]
  else
    content = zone_string.dup
    match = content.match(/\n([^$])/)
    if match
      insertion_point = match.begin(0) + 1
      content.sub!(/\n([^$])/, "\n#{DUMMY_SOA}\\1")
      [content, { at: insertion_point, length: DUMMY_SOA.length }]
    else
      [content, nil]
    end
  end
end

.load(zone_string) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/zonesync/zonefile.rb', line 28

def self.load(zone_string)
  content, _ = ensure_soa(zone_string)
  zone = Parser.parse(content)
  records = zone.records.map do |dns_zonefile_record|
    Zonesync::Record.from_dns_zonefile_record(dns_zonefile_record)
  end
  new(records, origin: zone.origin)
end