Class: Cabbage::Email

Inherits:
Object
  • Object
show all
Includes:
EmailParser
Defined in:
lib/cabbage/email/email.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EmailParser

break_by_boundary, extract_value, parse_email

Constructor Details

#initialize(source = "") ⇒ Email

Returns a new instance of Email.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cabbage/email/email.rb', line 7

def initialize(source = "")
  @raw_source = ""
  @raw_parsed = {}
  @header = {}
  @original_keys = {}
  @parts = []
  @multipart = false
  if source != nil && source.class.to_s == "String"
    if source.include?("\n")
      @raw_source = source
      parse()
    else
      load_and_parse(source)
    end
  else
    raise "Bad input to Cabbage::Email#new."
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/cabbage/email/email.rb', line 28

def method_missing(m, *args, &block)
  if self.keys.include?(m)
    self[m]
  elsif self.key.include?(m.intern)
    self[m.intern]
  else
    raise "undefined method in Cabbage::Email"
  end
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



26
27
28
# File 'lib/cabbage/email/email.rb', line 26

def header
  @header
end

#multipartObject (readonly)

Returns the value of attribute multipart.



26
27
28
# File 'lib/cabbage/email/email.rb', line 26

def multipart
  @multipart
end

#original_keysObject (readonly)

Returns the value of attribute original_keys.



26
27
28
# File 'lib/cabbage/email/email.rb', line 26

def original_keys
  @original_keys
end

#partsObject (readonly)

Returns the value of attribute parts.



26
27
28
# File 'lib/cabbage/email/email.rb', line 26

def parts
  @parts
end

#raw_parsedObject (readonly)

Returns the value of attribute raw_parsed.



26
27
28
# File 'lib/cabbage/email/email.rb', line 26

def raw_parsed
  @raw_parsed
end

#raw_sourceObject (readonly)

Returns the value of attribute raw_source.



26
27
28
# File 'lib/cabbage/email/email.rb', line 26

def raw_source
  @raw_source
end

Instance Method Details

#[](key) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cabbage/email/email.rb', line 69

def [](key)
  if @header.has_key?(key)
    @header[key]
  elsif self.mime_types.include?(key)
    @parts[self.mime_types.index(key)].body
  elsif key.class == String && self.keys.include?(key.intern)
    self[key.intern]
  elsif key == :body
    self.body
  elsif key == :plaintext && self.mime_types.include?("text/plain")
    @parts[self.mime_types.index("text/plain")].body
  elsif key == :html && self.mime_types.include?("text/html")
    @parts[self.mime_types.index("text/html")].body
  elsif key == :attachments
    self.attachments
  elsif key.class == Fixnum
    # to be implemented
    nil
  else
    # perhaps raise exception here?
    nil
  end
end

#attachmentsObject



97
98
99
100
101
102
103
# File 'lib/cabbage/email/email.rb', line 97

def attachments
  [].tap do |output|
    @parts.each do |part|
      output << part if part.attachment?
    end
  end
end

#body(type = "text/plain") ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cabbage/email/email.rb', line 115

def body(type = "text/plain")
  if @multipart
    if self.mime_types.include?(type)
      @parts[self.mime_types.index(type)].body
    elsif @parts.size > 0
      @parts[0].body
    else
      nil
    end
  else
    @raw_parsed[:body]
  end
end

#keysObject



93
94
95
# File 'lib/cabbage/email/email.rb', line 93

def keys
  @header.keys + [:body, :html, :plaintext, :attachments] + self.mime_types 
end

#load(filename) ⇒ Object



38
39
40
# File 'lib/cabbage/email/email.rb', line 38

def load(filename)
  open(filename) {|f| @raw_source = f.read }
end

#load_and_parse(filename) ⇒ Object



42
43
44
45
# File 'lib/cabbage/email/email.rb', line 42

def load_and_parse(filename)
  open(filename) {|f| @raw_source = f.read }
  parse()
end

#make_flat(tree) ⇒ Object

private



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cabbage/email/email.rb', line 135

def make_flat(tree)
  results = []
  if tree[:body].class == Array
    tree[:body].each do |this_part|
      if this_part[:header][:content_type] =~ /^multipart/
        results << make_flat(this_part)
      else
        results << this_part
      end
    end
  elsif tree[:body].class == String
    results << tree
  else
    # something went wrong
  end
  return results.flatten
end

#mime_typesObject

returns an array of strings representing available mime types in the array of mime parts.



107
108
109
110
111
112
113
# File 'lib/cabbage/email/email.rb', line 107

def mime_types
  [].tap do |result|
    @parts.each do |part|
      result << part.content_type
    end
  end
end

#multipart?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/cabbage/email/email.rb', line 65

def multipart?
  @multipart
end

#parseObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cabbage/email/email.rb', line 47

def parse
  if @raw_source.empty?
    puts "Nothing to parse."
    return false
  else
    @raw_parsed = EmailParser.parse_email(@raw_source)
  end
  @header = @raw_parsed[:header]
  @original_keys = @raw_parsed[:original_keys]
  if header[:content_type].include?("multipart")
    @multipart = true
  end
  make_flat(@raw_parsed).each do |raw_part|
    @parts << MimePart.new(raw_part)
  end
  return true
end