Sha256: 41bca4feafeb091d71ae716e715b956bef9fed1fc97e4e09a1441029c6d293b8

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

module RecordStore
  class Record::CAA < Record
    attr_accessor :flags, :tag, :value

    LABEL_REGEX = '[a-z0-9](?:-*[a-z0-9])*'
    DOMAIN_REGEX = /\A#{LABEL_REGEX}(?:\.#{LABEL_REGEX})\z/i

    validates :flags, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 255 }, presence: true
    validates :tag, inclusion: { in: %w(issue issuewild iodef) }, presence: true
    validate :validate_uri_value, if: :iodef?
    validates :value, format: { with: DOMAIN_REGEX, message: 'is not a fully qualified domain name'}, unless: :iodef?

    def initialize(record)
      super
      @flags = Integer(record.fetch(:flags))
      @tag = record.fetch(:tag)
      @value = record.fetch(:value)
    end

    def rdata
      {
        flags: flags,
        tag: tag,
        value: value,
      }
    end

    def rdata_txt
      "#{flags} #{tag} \"#{value}\""
    end

    protected

    def iodef?
      tag == 'iodef'
    end

    def validate_uri_value
      uri = URI(value)
      return if uri.kind_of?(URI::MailTo) || uri.kind_of?(URI::HTTP)
      errors.add(:value, "URL scheme should be mailto, http, or https")
    rescue URI::Error
      errors.add(:value, "Value should be a valid URI")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
record_store-5.5.3 lib/record_store/record/caa.rb
record_store-5.4.3 lib/record_store/record/caa.rb
record_store-5.4.2 lib/record_store/record/caa.rb
record_store-5.4.1 lib/record_store/record/caa.rb