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::MailTo>)
  • :ruf (Array<URI::MailTo>)
  • :sp (:none, :quarantine, :reject)
  • :v (:DMARC1)


63
64
65
# File 'lib/dmarc/record.rb', line 63

def initialize(attributes={})
  @adkim, @aspf, @fo, @p, @pct, @rf, @ri, @rua, @ruf, @sp, @v = attributes.values_at(:adkim, :aspf, :fo, :p, :pct, :rf, :ri, :rua, :ruf, :sp, :v)
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::MailTo> (readonly)

rua field.

Returns:

  • (Array<URI::MailTo>)


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

def rua
  @rua
end

#rufArray<URI::MailTo> (readonly)

rua field.

Returns:

  • (Array<URI::MailTo>)


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

def ruf
  @ruf
end

#sp:none, ... (readonly)

sp field.

Returns:

  • (:none, :quarantine, :reject)


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.



150
151
152
# File 'lib/dmarc/record.rb', line 150

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



141
142
143
144
145
# File 'lib/dmarc/record.rb', line 141

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



174
175
176
177
178
# File 'lib/dmarc/record.rb', line 174

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)


76
77
78
# File 'lib/dmarc/record.rb', line 76

def adkim
  @adkim || :r
end

#aspf:r, :s

aspf field.

Returns:

  • (:r, :s)


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

def aspf
  @aspf || :r
end

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

fo field.

Returns:

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


94
95
96
# File 'lib/dmarc/record.rb', line 94

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

#pctInteger

pct field.

Returns:

  • (Integer)


103
104
105
# File 'lib/dmarc/record.rb', line 103

def pct
  @pct || 100
end

#rf:afrf, :iodef

rf field.

Returns:

  • (:afrf, :iodef)


112
113
114
# File 'lib/dmarc/record.rb', line 112

def rf
  @rf || :afrf
end

#riInteger

ri field.

Returns:

  • (Integer)


121
122
123
# File 'lib/dmarc/record.rb', line 121

def ri
  @ri || 86400
end

#to_sString

Converts the record back to a DMARC String.

Returns:

  • (String)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/dmarc/record.rb', line 185

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