Class: SPF::Query::Record

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

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.



93
94
95
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
# File 'lib/spf/query/record.rb', line 93

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:



47
48
49
# File 'lib/spf/query/record.rb', line 47

def a
  @a
end

#allMechanism? (readonly)

The right-most ‘all:` mechanism.

Returns:



37
38
39
# File 'lib/spf/query/record.rb', line 37

def all
  @all
end

#existsArray<Mechanism> (readonly)

Selects all ‘exists:` mechanisms.

Returns:



72
73
74
# File 'lib/spf/query/record.rb', line 72

def exists
  @exists
end

#expModifier? (readonly)

The ‘exp=` modifier.

Returns:



82
83
84
# File 'lib/spf/query/record.rb', line 82

def exp
  @exp
end

#includeArray<Mechanism> (readonly)

Selects all ‘include:` mechanisms.

Returns:



42
43
44
# File 'lib/spf/query/record.rb', line 42

def include
  @include
end

#ip4Array<Mechanism> (readonly)

Selects all ‘ip4:` mechanisms.

Returns:



62
63
64
# File 'lib/spf/query/record.rb', line 62

def ip4
  @ip4
end

#ip6Array<Mechanism> (readonly)

Selects all ‘ip6:` mechanisms.

Returns:



67
68
69
# File 'lib/spf/query/record.rb', line 67

def ip6
  @ip6
end

#mechanismsArray<Mechanism> (readonly)

All mechanisms within the record.

Returns:



27
28
29
# File 'lib/spf/query/record.rb', line 27

def mechanisms
  @mechanisms
end

#modifiersArray<Modifier> (readonly)

All modifiers within the record.

Returns:



32
33
34
# File 'lib/spf/query/record.rb', line 32

def modifiers
  @modifiers
end

#mxArray<Mechanism> (readonly)

Selects all ‘mx:` mechanisms.

Returns:



52
53
54
# File 'lib/spf/query/record.rb', line 52

def mx
  @mx
end

#ptrArray<Mechanism> (readonly)

Selects all ‘ptr:` mechanisms.

Returns:



57
58
59
# File 'lib/spf/query/record.rb', line 57

def ptr
  @ptr
end

#redirectModifier? (readonly)

The ‘redirect=` modifier.

Returns:



77
78
79
# File 'lib/spf/query/record.rb', line 77

def redirect
  @redirect
end

#rulesArray<Mechanism, Modifier> (readonly)

The SPF rules.

Returns:



22
23
24
# File 'lib/spf/query/record.rb', line 22

def rules
  @rules
end

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

The SPF version of the record.

Returns:

  • (:spf1)


16
17
18
# File 'lib/spf/query/record.rb', line 16

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:



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

def self.parse(spf)
  Parser.parse(spf)
rescue Parslet::ParseFailed => error
  raise(InvalidRecord.new(error.message,error.cause))
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.



162
163
164
165
166
# File 'lib/spf/query/record.rb', line 162

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.



180
181
182
# File 'lib/spf/query/record.rb', line 180

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

#inspectString

Inspects the record.

Returns:

  • (String)


198
199
200
# File 'lib/spf/query/record.rb', line 198

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

#to_sString

Converts the record back to a String.

Returns:

  • (String)


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

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