Class: DMARC::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/dmarc/record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Record

Initializes the record.

Parameters:

  • attributes (Hash{Symbol => Object}) (defaults to: {})

    Attributes for the record.

Options Hash (attributes):

  • :adkim (:r, :s) — default: :r
  • :aspf (:r, :s) — default: :r
  • :fo (Array<'0', '1', 'd', 's'>) — default: '0'
  • :p (:none, :quarantine, :reject)
  • :pct (Integer) — default: 100
  • :rf (:afrf, :iodef) — default: :afrf
  • :ri (Integer) — default: 86400
  • :rua (Array<Uri>)
  • :ruf (Array<Uri>)
  • :sp (:none, :quarantine, :reject)
  • :v (:DMARC1)


63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

Instance Attribute Details

#p:none, ... (readonly)

p field.

Returns:

  • (:none, :quarantine, :reject)


13
14
15
# File 'lib/dmarc/record.rb', line 13

def p
  @p
end

#ruaArray<Uri> (readonly)

rua field.

Returns:



18
19
20
# File 'lib/dmarc/record.rb', line 18

def rua
  @rua
end

#rufArray<Uri> (readonly)

rua field.

Returns:



23
24
25
# File 'lib/dmarc/record.rb', line 23

def ruf
  @ruf
end

#sp:none, ... (readonly)

The sp= field.

Returns:

  • (:none, :quarantine, :reject)

    The value of the sp= field, or that of #p if the field was omitted.



28
29
30
# File 'lib/dmarc/record.rb', line 28

def sp
  @sp
end

#v:DMARC1 (readonly)

v field.

Returns:

  • (:DMARC1)


33
34
35
# File 'lib/dmarc/record.rb', line 33

def v
  @v
end

Class Method Details

.from_txt(rec) ⇒ Object

Deprecated.

use parse instead.



282
283
284
# File 'lib/dmarc/record.rb', line 282

def self.from_txt(rec)
  parse(rec)
end

.parse(record) ⇒ Record

Parses a DMARC record.

Parameters:

  • record (String)

    The raw DMARC record.

Returns:

  • (Record)

    The parsed DMARC record.

Raises:

Since:

  • 0.3.0



273
274
275
276
277
# 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) ⇒ Record?

Queries and parses the DMARC record for a domain.

Parameters:

  • domain (String)

    The domain to query DMARC for.

  • resolver (Resolv::DNS) (defaults to: Resolv::DNS.new)

    The resolver to use.

Returns:

  • (Record, nil)

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

Raises:

Since:

  • 0.3.0



306
307
308
309
310
# 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

Instance Method Details

#adkim:r, :s

adkim= field.

Returns:

  • (:r, :s)

    The value of the adkim= field, or :r if the field was omitted.



115
116
117
# File 'lib/dmarc/record.rb', line 115

def adkim
  @adkim || :r
end

#adkim?Boolean

Determines whether the adkim= field was specified.

Returns:

  • (Boolean)

Since:

  • 0.4.0



105
106
107
# File 'lib/dmarc/record.rb', line 105

def adkim?
  !@adkim.nil?
end

#aspf:r, :s

aspf field.

Returns:

  • (:r, :s)

    The value of the aspf= field, or :r if the field was omitted.



136
137
138
# File 'lib/dmarc/record.rb', line 136

def aspf
  @aspf || :r
end

#aspf?Boolean

Determines whether the aspf= field was specified.

Returns:

  • (Boolean)

Since:

  • 0.4.0



126
127
128
# File 'lib/dmarc/record.rb', line 126

def aspf?
  !@aspf.nil?
end

#foArray<'0', '1', 'd', 's'>

fo field.

Returns:

  • (Array<'0', '1', 'd', 's'>)

    The value of the fo= field, or ["0"] if the field was omitted.



157
158
159
# File 'lib/dmarc/record.rb', line 157

def fo
  @fo || %w[0]
end

#fo?Boolean

Determines whether the fo= field was specified.

Returns:

  • (Boolean)

Since:

  • 0.4.0



147
148
149
# File 'lib/dmarc/record.rb', line 147

def fo?
  !@fo.nil?
end

#p?Boolean

Determines if the p= field was specified?

Returns:

  • (Boolean)

Since:

  • 0.4.0



168
169
170
# File 'lib/dmarc/record.rb', line 168

def p?
  !@p.nil?
end

#pctInteger

pct field.

Returns:

  • (Integer)

    The value of the pct= field, or 100 if the field was omitted.



189
190
191
# File 'lib/dmarc/record.rb', line 189

def pct
  @pct || 100
end

#pct?Boolean

Determines whether the pct= field was specified.

Returns:

  • (Boolean)

Since:

  • 0.4.0



179
180
181
# File 'lib/dmarc/record.rb', line 179

def pct?
  !@pct.nil?
end

#rf:afrf, :iodef

rf field.

Returns:

  • (:afrf, :iodef)

    The value of the rf= field, or :afrf if the field was omitted.



210
211
212
# File 'lib/dmarc/record.rb', line 210

def rf
  @rf || :afrf
end

#rf?Boolean

Determines whether the rf= field was specified.

Returns:

  • (Boolean)

Since:

  • 0.4.0



200
201
202
# File 'lib/dmarc/record.rb', line 200

def rf?
  !@rf.nil?
end

#riInteger

ri field.

Returns:

  • (Integer)

    The value of the ri= field, or 86400 if the field was omitted.



231
232
233
# File 'lib/dmarc/record.rb', line 231

def ri
  @ri || 86400
end

#ri?Boolean

Determines whether the ri= field was specified.

Returns:

  • (Boolean)

Since:

  • 0.4.0



221
222
223
# File 'lib/dmarc/record.rb', line 221

def ri?
  !@ri.nil?
end

#rua?Boolean

Determines if the rua= field was specified?

Returns:

  • (Boolean)

Since:

  • 0.4.0



242
243
244
# File 'lib/dmarc/record.rb', line 242

def rua?
  !@rua.nil?
end

#ruf?Boolean

Determines if the ruf= field was specified?

Returns:

  • (Boolean)

Since:

  • 0.4.0



253
254
255
# File 'lib/dmarc/record.rb', line 253

def ruf?
  !@ruf.nil?
end

#sp?Boolean

Determines if the sp= field was specified?

Returns:

  • (Boolean)

Since:

  • 0.4.0



84
85
86
# File 'lib/dmarc/record.rb', line 84

def sp?
  !@sp.nil?
end

#to_hHash{Symbol => Object}

Converts the record to a Hash.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 0.4.0



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# 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_sString

Converts the record back to a DMARC String.

Returns:

  • (String)


342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# 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