class DMARC::Record

Attributes

p[R]

‘p` field.

@return [:none, :quarantine, :reject]

rua[R]

‘rua` field.

@return [Array<Uri>]

ruf[R]

‘rua` field.

@return [Array<Uri>]

sp[R]

‘sp` field.

@return [:none, :quarantine, :reject]

v[R]

‘v` field.

@return [:DMARC1]

Public Class Methods

from_txt(rec) click to toggle source

@deprecated use {parse} instead.

# File lib/dmarc/record.rb, line 282
def self.from_txt(rec)
  parse(rec)
end
new(attributes={}) click to toggle source

Initializes the record.

@param [Hash{Symbol => Object}] attributes

Attributes for the record.

@option attributes [:r, :s] :adkim (:r)

@option attributes [:r, :s] :aspf (:r)

@option attributes [Array<‘0’, ‘1’, ‘d’, ‘s’>] :fo (‘0’)

@option attributes [:none, :quarantine, :reject] :p

@option attributes [Integer] :pct (100)

@option attributes [:afrf, :iodef] :rf (:afrf)

@option attributes [Integer] :ri (86400)

@option attributes [Array<Uri>] :rua

@option attributes [Array<Uri>] :ruf

@option attributes [:none, :quarantine, :reject] :sp

@option attributes [:DMARC1] :v

# File lib/dmarc/record.rb, line 63
def initialize(attributes={})
  @v     = attributes.fetch(:v)
  @adkim = attributes[:adkim]
  @aspf  = attributes[:aspf]
  @fo    = attributes[:fo]
  @p     = attributes[:p]
  @pct   = attributes[:pct]
  @rf    = attributes[:rf]
  @ri    = attributes[:ri]
  @rua   = attributes[:rua]
  @ruf   = attributes[:ruf]
  @sp    = attributes[:sp]
end
parse(record) click to toggle source

Parses a DMARC record.

@param [String] record

The raw DMARC record.

@return [Record]

The parsed DMARC record.

@raise [InvalidRecord]

The DMARC record could not be parsed.

@since 0.3.0

@api public

# File lib/dmarc/record.rb, line 273
def self.parse(record)
  new(Parser.parse(record))
rescue Parslet::ParseFailed => error
  raise(InvalidRecord.new(error.message,error.cause))
end
query(domain,resolver=Resolv::DNS.new) click to toggle source

Queries and parses the DMARC record for a domain.

@param [String] domain

The domain to query DMARC for.

@param [Resolv::DNS] resolver

The resolver to use.

@return [Record, nil]

The parsed DMARC record. If no DMARC record was found, `nil` will be
returned.

@raise [InvalidRecord]

The DMARC record could not be parsed.

@since 0.3.0

@api public

# File lib/dmarc/record.rb, line 306
def self.query(domain,resolver=Resolv::DNS.new)
  if (dmarc = DMARC.query(domain,resolver))
    parse(dmarc)
  end
end

Public Instance Methods

adkim() click to toggle source

‘adkim=` field.

@return [:r, :s]

The value of the `adkim=` field, or `:r` if the field was omitted.
# File lib/dmarc/record.rb, line 115
def adkim
  @adkim || :r
end
adkim?() click to toggle source

Determines whether the ‘adkim=` field was specified.

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 105
def adkim?
  !@adkim.nil?
end
aspf() click to toggle source

‘aspf` field.

@return [:r, :s]

The value of the `aspf=` field, or `:r` if the field was omitted.
# File lib/dmarc/record.rb, line 136
def aspf
  @aspf || :r
end
aspf?() click to toggle source

Determines whether the ‘aspf=` field was specified.

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 126
def aspf?
  !@aspf.nil?
end
fo() click to toggle source

‘fo` field.

@return [Array<‘0’, ‘1’, ‘d’, ‘s’>]

The value of the `fo=` field, or `["0"]` if the field was omitted.
# File lib/dmarc/record.rb, line 157
def fo
  @fo || %w[0]
end
fo?() click to toggle source

Determines whether the ‘fo=` field was specified.

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 147
def fo?
  !@fo.nil?
end
p?() click to toggle source

Determines if the ‘p=` field was specified?

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 168
def p?
  !@p.nil?
end
pct() click to toggle source

‘pct` field.

@return [Integer]

The value of the `pct=` field, or `100` if the field was omitted.
# File lib/dmarc/record.rb, line 189
def pct
  @pct || 100
end
pct?() click to toggle source

Determines whether the ‘pct=` field was specified.

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 179
def pct?
  !@pct.nil?
end
rf() click to toggle source

‘rf` field.

@return [:afrf, :iodef]

The value of the `rf=` field, or `:afrf` if the field was omitted.
# File lib/dmarc/record.rb, line 210
def rf
  @rf || :afrf
end
rf?() click to toggle source

Determines whether the ‘rf=` field was specified.

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 200
def rf?
  !@rf.nil?
end
ri() click to toggle source

‘ri` field.

@return [Integer]

The value of the `ri=` field, or `86400` if the field was omitted.
# File lib/dmarc/record.rb, line 231
def ri
  @ri || 86400
end
ri?() click to toggle source

Determines whether the ‘ri=` field was specified.

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 221
def ri?
  !@ri.nil?
end
rua?() click to toggle source

Determines if the ‘rua=` field was specified?

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 242
def rua?
  !@rua.nil?
end
ruf?() click to toggle source

Determines if the ‘ruf=` field was specified?

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 253
def ruf?
  !@ruf.nil?
end
sp?() click to toggle source

Determines if the ‘sp=` field was specified?

@return [Boolean]

@since 0.4.0

# File lib/dmarc/record.rb, line 84
def sp?
  !@sp.nil?
end
to_h() click to toggle source

Converts the record to a Hash.

@return [Hash{Symbol => Object}]

@since 0.4.0

# File lib/dmarc/record.rb, line 319
def to_h
  hash = {}

  hash[:v]     = @v     if @v
  hash[:p]     = @p     if @p
  hash[:sp]    = @sp    if @sp
  hash[:rua]   = @rua   if @rua
  hash[:ruf]   = @ruf   if @ruf
  hash[:adkim] = @adkim if @adkim
  hash[:aspf]  = @aspf  if @aspf
  hash[:ri]    = @ri    if @ri
  hash[:fo]    = @fo    if @fo
  hash[:rf]    = @rf    if @rf
  hash[:pct]   = @pct   if @pct

  return hash
end
to_s() click to toggle source

Converts the record back to a DMARC String.

@return [String]

# File lib/dmarc/record.rb, line 342
def to_s
  tags = []

  tags << "v=#{@v}"               if @v
  tags << "p=#{@p}"               if @p
  tags << "sp=#{@sp}"             if @sp
  tags << "rua=#{@rua.join(',')}" if @rua
  tags << "ruf=#{@ruf.join(',')}" if @ruf
  tags << "adkim=#{@adkim}"       if @adkim
  tags << "aspf=#{@aspf}"         if @aspf
  tags << "ri=#{@ri}"             if @ri
  tags << "fo=#{@fo.join(':')}"   if @fo
  tags << "rf=#{@rf}"             if @rf
  tags << "pct=#{@pct}"           if @pct

  return tags.join('; ')
end