Module: Zonify::Mappings

Extended by:
Mappings
Included in:
Mappings
Defined in:
lib/zonify.rb

Instance Method Summary collapse

Instance Method Details

#apply(name, mappings) ⇒ Object

Apply mappings to the name in order. (A hash can be used for mappings but then one will not be able to predict the order.) If no mappings apply, the empty list is returned.



584
585
586
587
588
589
590
591
# File 'lib/zonify.rb', line 584

def apply(name, mappings)
  name_ = Zonify.dot_(name)
  mappings.map do |k, v|
    _k_ = Zonify.dot_(Zonify._dot(k))
    before = Zonify::Mappings.unsuffix(name_, _k_)
    v.map{|s| Zonify.dot_(before + Zonify._dot(s)) } if before
  end.compact.flatten
end

#names(name, mappings) ⇒ Object

Get the names that result from the mappings, or the original name if none apply. The first name in the list is taken to be the canonical name, the one used for groups of servers in SRV records.



595
596
597
598
# File 'lib/zonify.rb', line 595

def names(name, mappings)
  mapped = Zonify::Mappings.apply(name, mappings)
  mapped.empty? ? [name] : mapped
end

#parse(s) ⇒ Object



577
578
579
580
# File 'lib/zonify.rb', line 577

def parse(s)
  k, *v = s.split(':')
  [k, v] if k and v and not v.empty?
end

#rewrite(tree, mappings) ⇒ Object



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/zonify.rb', line 603

def rewrite(tree, mappings)
  tree.inject({}) do |acc, pair|
    name, info = pair
    names = Zonify::Mappings.names(name, mappings)
    names.each do |name|
      acc[name] ||= {}
      info.inject(acc[name]) do |acc_, pair_|
        type, data = pair_
        acc_[type] ||= {}
        prefix_ = Zonify.dot_(Zonify::Resolve::SRV_PREFIX)
        rrs = if type == 'SRV' and name.start_with? prefix_ and data[:value]
                data[:value].map do |rr|
                  if /^(.+) ([^ ]+)$/.match(rr)
                    "#{$1} #{Zonify::Mappings.names($2, mappings).first}"
                  else
                    rr
                  end
                end
              end
        normed = (rrs + (acc_[type][:value] or [])).sort.uniq if rrs
        addenda = normed ? { :value => normed } : {}
        acc_[type] = data.merge(addenda)
        acc_
      end
    end
    acc
  end
end

#unsuffix(s, suffix) ⇒ Object



599
600
601
602
# File 'lib/zonify.rb', line 599

def unsuffix(s, suffix)
  before, _, after = s.rpartition(suffix)
  before if after.empty?
end