Class: Funktional::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/funktional/email.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, expected) ⇒ Email

Returns a new instance of Email.



12
13
14
15
16
# File 'lib/funktional/email.rb', line 12

def initialize(email, expected)
  @original = email
  @expected = expected
  calculate_matches
end

Instance Attribute Details

#matchesObject

Returns the value of attribute matches.



10
11
12
# File 'lib/funktional/email.rb', line 10

def matches
  @matches
end

#originalObject

Returns the value of attribute original.



9
10
11
# File 'lib/funktional/email.rb', line 9

def original
  @original
end

Class Method Details

.find_closest(emails, expected) ⇒ Object



4
5
6
7
# File 'lib/funktional/email.rb', line 4

def self.find_closest(emails, expected)
  emails = emails.map { |email| new(email, expected) }
  emails.sort.first
end

Instance Method Details

#<=>(other) ⇒ Object



23
24
25
# File 'lib/funktional/email.rb', line 23

def <=>(other)
  other.total_matches <=> total_matches
end

#bodyObject



72
73
74
# File 'lib/funktional/email.rb', line 72

def body
  @original.body.raw_source
end

#calculate_matchesObject



18
19
20
21
# File 'lib/funktional/email.rb', line 18

def calculate_matches
  @matches = {}
  @expected.map { |expect| self.send "check_#{expect.first}" }
end

#check_containingObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/funktional/email.rb', line 49

def check_containing
  should_contain = @expected[:containing]
  should_contain = [should_contain] unless should_contain.is_a? Array
  matches = []
  
  should_contain.each do |should_i|
    matches << should_i if body =~ /#{Regexp.escape(should_i)}/
  end
  @matches[:containing] = matches if matches.any?
end

#check_fromObject



35
36
37
# File 'lib/funktional/email.rb', line 35

def check_from
  @matches[:from] = from if from.present? and (@expected[:from] == from)
end

#check_subjectObject



43
44
45
46
47
# File 'lib/funktional/email.rb', line 43

def check_subject
  if subject.present? and (@expected[:subject] == subject)
    @matches[:subject] = subject
  end
end

#check_toObject



39
40
41
# File 'lib/funktional/email.rb', line 39

def check_to
  @matches[:to] = to if to.present? and (@expected[:to] == to)
end

#fromObject



60
61
62
# File 'lib/funktional/email.rb', line 60

def from
  @original.from[0]
end

#subjectObject



68
69
70
# File 'lib/funktional/email.rb', line 68

def subject
  @original.subject
end

#toObject



64
65
66
# File 'lib/funktional/email.rb', line 64

def to
  @original.to[0]
end

#total_matchesObject



27
28
29
30
31
32
33
# File 'lib/funktional/email.rb', line 27

def total_matches
  if @matches.has_key? :containing
    @matches.size + (@matches[:containing].size - 1)
  else
    @matches.size
  end
end