Class: DNSer::StreamBuilder

Inherits:
Builder
  • Object
show all
Defined in:
lib/dnser/builders/stream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ StreamBuilder

Returns a new instance of StreamBuilder.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dnser/builders/stream.rb', line 5

def initialize stream
  case stream
    when String
      @stream = File.new stream, 'w'
    when ::IO,::File,::StringIO
      @stream = stream
    else
      raise "Unknown stream type #{stream.class}"
  end

  @buffer = []
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



4
5
6
# File 'lib/dnser/builders/stream.rb', line 4

def stream
  @stream
end

Instance Method Details

#origin(name) ⇒ Object



18
19
20
# File 'lib/dnser/builders/stream.rb', line 18

def origin name
  @stream << ('$ORIGIN ' + name.to_s + "\n")
end

#syncObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dnser/builders/stream.rb', line 42

def sync

  max_host_length = 0
  max_type_length = 0
  max_value_length = 0

  @buffer.each do |r|
    max_host_length = max_host_length < r[:host].size ? r[:host].length : max_host_length
    max_type_length = max_type_length < r[:type].size ? r[:type].length : max_type_length
    max_value_length = max_value_length < r[:value].size ? r[:value].length : max_value_length
  end

  @buffer.each do |r|
    @stream << r[:host].ljust(max_host_length + 2)
    @stream << 'IN ' + r[:type].ljust(max_type_length)
    @stream << "\t" +  r[:value].ljust(max_value_length)
    @stream << "\t##{r[:comment]}" unless r[:comment].empty?
    @stream << "\n"
  end

  @buffer.clear

end

#ttl(value) ⇒ Object



22
23
24
# File 'lib/dnser/builders/stream.rb', line 22

def ttl value
  @stream << ('$TTL ' + value.to_s + "\n")
end

#write(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dnser/builders/stream.rb', line 26

def write *args
  if (args.count == 1)
    record = args.first
    write record.host, record.type, record.value, record.comment
  else

    value = args[2]
    if value.is_a? DNSer::Record
      value = value.full_host
    else
      value = value.to_s
    end
    @buffer << {host: args[0].to_s, type: args[1].to_s.upcase ,value: value, comment: args[3] }
  end
end