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.



549
550
551
552
553
554
555
556
# File 'lib/zonify.rb', line 549

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.



560
561
562
563
# File 'lib/zonify.rb', line 560

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

#parse(s) ⇒ Object



542
543
544
545
# File 'lib/zonify.rb', line 542

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

#rewrite(tree, mappings) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/zonify.rb', line 568

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



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

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