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.



570
571
572
573
574
575
576
577
# File 'lib/zonify.rb', line 570

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.



581
582
583
584
# File 'lib/zonify.rb', line 581

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

#parse(s) ⇒ Object



563
564
565
566
# File 'lib/zonify.rb', line 563

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

#rewrite(tree, mappings) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/zonify.rb', line 589

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



585
586
587
588
# File 'lib/zonify.rb', line 585

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