Class: SPF::Query::Record

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spf/query/record.rb

Overview

Represents a parsed SPF record.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, rules = []) ⇒ Record

Initializes the SPF record.

Parameters:

  • version (:spf1)

    The SPF version.

  • rules (Array<Mechanism, Modifier>) (defaults to: [])

    SPF rules.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/spf/query/record.rb', line 96

def initialize(version,rules=[])
  @version = version
  @rules   = rules

  @mechanisms = @rules.select { |term| term.kind_of?(Mechanism) }
  @modifiers  = @rules.select { |term| term.kind_of?(Modifier)  }

  # prefer the last `all:` mechanism
  @all = @mechanisms.reverse_each.find do |mechanism|
    mechanism.name == :all
  end

  mechanisms_by_name = lambda { |name|
    @mechanisms.select { |mechanism| mechanism.name == name }
  }

  @include = mechanisms_by_name[:include]
  @a       = mechanisms_by_name[:a]
  @mx      = mechanisms_by_name[:mx]
  @ptr     = mechanisms_by_name[:ptr]
  @ip4     = mechanisms_by_name[:ip4]
  @ip6     = mechanisms_by_name[:ip6]
  @exists  = mechanisms_by_name[:exists]

  modifier_by_name = lambda { |name|
    @modifiers.find { |modifier| modifier.name == name }
  }

  @redirect = modifier_by_name[:redirect]
  @exp      = modifier_by_name[:exp]
end

Instance Attribute Details

#aArray<Mechanism> (readonly)

Selects all a: mechanisms.

Returns:



50
51
52
# File 'lib/spf/query/record.rb', line 50

def a
  @a
end

#allMechanism? (readonly)

The right-most all: mechanism.

Returns:



40
41
42
# File 'lib/spf/query/record.rb', line 40

def all
  @all
end

#existsArray<Mechanism> (readonly)

Selects all exists: mechanisms.

Returns:



75
76
77
# File 'lib/spf/query/record.rb', line 75

def exists
  @exists
end

#expModifier? (readonly)

The exp= modifier.

Returns:



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

def exp
  @exp
end

#includeArray<Mechanism> (readonly)

Selects all include: mechanisms.

Returns:



45
46
47
# File 'lib/spf/query/record.rb', line 45

def include
  @include
end

#ip4Array<Mechanism> (readonly)

Selects all ip4: mechanisms.

Returns:



65
66
67
# File 'lib/spf/query/record.rb', line 65

def ip4
  @ip4
end

#ip6Array<Mechanism> (readonly)

Selects all ip6: mechanisms.

Returns:



70
71
72
# File 'lib/spf/query/record.rb', line 70

def ip6
  @ip6
end

#mechanismsArray<Mechanism> (readonly)

All mechanisms within the record.

Returns:



30
31
32
# File 'lib/spf/query/record.rb', line 30

def mechanisms
  @mechanisms
end

#modifiersArray<Modifier> (readonly)

All modifiers within the record.

Returns:



35
36
37
# File 'lib/spf/query/record.rb', line 35

def modifiers
  @modifiers
end

#mxArray<Mechanism> (readonly)

Selects all mx: mechanisms.

Returns:



55
56
57
# File 'lib/spf/query/record.rb', line 55

def mx
  @mx
end

#ptrArray<Mechanism> (readonly)

Selects all ptr: mechanisms.

Returns:



60
61
62
# File 'lib/spf/query/record.rb', line 60

def ptr
  @ptr
end

#redirectModifier? (readonly)

The redirect= modifier.

Returns:



80
81
82
# File 'lib/spf/query/record.rb', line 80

def redirect
  @redirect
end

#rulesArray<Mechanism, Modifier> (readonly)

The SPF rules.

Returns:



25
26
27
# File 'lib/spf/query/record.rb', line 25

def rules
  @rules
end

#version:spf1 (readonly) Also known as: v

The SPF version of the record.

Returns:

  • (:spf1)


19
20
21
# File 'lib/spf/query/record.rb', line 19

def version
  @version
end

Class Method Details

.parse(spf) ⇒ Record

Parses an SPF record.

Parameters:

  • spf (String)

    The raw SPF record.

Returns:

  • (Record)

    The parsed SPF record.

Raises:

See Also:



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/spf/query/record.rb', line 146

def self.parse(spf)
  if spf.include?('spf2.0')
    raise(SenderIDFound,"Sender ID was found in place of SPF")
  end

  begin
    Parser.parse(spf)
  rescue Parslet::ParseFailed => error
    raise(InvalidRecord.new(error.message,error.cause))
  end
end

.query(domain, resolver = Resolv::DNS.new) ⇒ Record?

Queries the domain for it's SPF record.

Parameters:

  • domain (String)

    The domain to query.

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

    The optional resolver to use.

Returns:

  • (Record, nil)

    The parsed SPF record. If no SPF record could be found, nil will be returned.



173
174
175
176
177
# File 'lib/spf/query/record.rb', line 173

def self.query(domain,resolver=Resolv::DNS.new)
  if (spf = Query.query(domain,resolver))
    parse(spf)
  end
end

Instance Method Details

#each {|rule| ... } ⇒ Enumerator

Enumerates over the rules.

Yields:

  • (rule)

    The given block will be passed each rule.

Yield Parameters:

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.



191
192
193
# File 'lib/spf/query/record.rb', line 191

def each(&block)
  @rules.each(&block)
end

#inspectString

Inspects the record.

Returns:

  • (String)


209
210
211
# File 'lib/spf/query/record.rb', line 209

def inspect
  "#<#{self.class}: #{self}>"
end

#to_sString

Converts the record back to a String.

Returns:

  • (String)


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

def to_s
  "v=#{@version} #{@rules.join(' ')}"
end