Sha256: c52d928119eaa8086e6f3fcfc5dc768d1287cdb9974dd57b27ef0239efb86b0a

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

module RecordStore
  class Record
    FQDN_REGEX  = /\A(\*\.)?([a-z0-9_]+(-[a-z0-9]+)*\._?)+[a-z]{2,}\.\Z/i
    CNAME_REGEX =  /\A(\*\.)?([a-z0-9]+((-|--)?[a-z0-9]+)*\._?)+[a-z]{2,}\.\Z/i

    include ActiveModel::Validations

    attr_accessor :fqdn, :ttl, :id

    validates :ttl, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 2147483647 }
    validates :fqdn, format: { with: Record::FQDN_REGEX }, length: { maximum: 254 }
    validate :validate_label_length

    def initialize(record)
      @fqdn = Record.ensure_ends_with_dot(record.fetch(:fqdn))
      @ttl  = record.fetch(:ttl)
      @id   = record.fetch(:record_id, nil)
    end

    def self.build_from_yaml_definition(yaml_definition)
      record_type = yaml_definition.fetch(:type)
      Record.const_get(record_type).new(yaml_definition)
    end

    def log!(logger=STDOUT)
      logger.puts to_s
    end

    def to_hash
      {
        type: type,
        fqdn: fqdn,
        ttl: ttl
      }.merge(rdata)
    end

    def type
      self.class.name.split('::').last
    end

    def ==(other)
      other.class == self.class && other.to_hash == self.to_hash
    end

    alias_method :eql?, :==

    def hash
      to_hash.hash
    end

    def to_json
      { ttl: ttl, rdata: rdata }
    end

    def key
      "#{type},#{fqdn}"
    end

    def rdata
      raise NotImplementedError
    end

    def rdata_txt
      raise NotImplementedError
    end

    def to_s
      rr_type = self.class.name.demodulize
      "[#{rr_type}Record] #{fqdn} #{ttl} IN #{rr_type} #{rdata_txt}"
    end

    protected

    def validate_label_length
      unless fqdn.split('.').all? { |label| label.length <= 63 }
        errors.add(:fqdn, "A label should be at most 63 characters")
      end
    end

    def self.ensure_ends_with_dot(fqdn)
      fqdn.end_with?(".") ? fqdn : "#{fqdn}."
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
record_store-5.0.4 lib/record_store/record.rb
record_store-5.0.3 lib/record_store/record.rb
record_store-5.0.2 lib/record_store/record.rb
record_store-5.0.1 lib/record_store/record.rb
record_store-5.0.0 lib/record_store/record.rb