Class: Replyr::ReplyEmail

Inherits:
Object
  • Object
show all
Defined in:
lib/replyr/reply_email.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ ReplyEmail



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/replyr/reply_email.rb', line 5

def initialize(mail)
  self.to = mail.to.first
  self.from = mail.from.first
  self.subject = mail.subject
  self.body = mail.multipart? ? mail.text_part.decoded : mail.decoded

  # Extract Attachments and store as StringIO in attached_files
  # can later be processed by e.g. carrierwave
  #
  self.attached_files = mail.attachments.map do |attachment|
    file = StringIO.new(attachment.decoded)
    file.class.class_eval { attr_accessor :original_filename, :content_type }
    file.original_filename = attachment.filename
    file.content_type = attachment.mime_type
    file
  end
end

Instance Attribute Details

#attached_filesObject

Returns the value of attribute attached_files.



3
4
5
# File 'lib/replyr/reply_email.rb', line 3

def attached_files
  @attached_files
end

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/replyr/reply_email.rb', line 3

def body
  @body
end

#fromObject

Returns the value of attribute from.



3
4
5
# File 'lib/replyr/reply_email.rb', line 3

def from
  @from
end

#subjectObject

Returns the value of attribute subject.



3
4
5
# File 'lib/replyr/reply_email.rb', line 3

def subject
  @subject
end

#toObject

Returns the value of attribute to.



3
4
5
# File 'lib/replyr/reply_email.rb', line 3

def to
  @to
end

Class Method Details

.process(mail) ⇒ Object



23
24
25
26
# File 'lib/replyr/reply_email.rb', line 23

def self.process(mail)
  reply_mail = new(mail)
  reply_mail.process
end

Instance Method Details

#is_reply_email?Boolean

Checks if this incoming mail is a reply email



30
31
32
# File 'lib/replyr/reply_email.rb', line 30

def is_reply_email?
  to.starts_with?(Replyr.config.prefix)
end

#processObject



38
39
40
41
42
43
44
45
46
# File 'lib/replyr/reply_email.rb', line 38

def process
  if is_reply_email?
    if reply_address = ReplyAddress.new_from_address(to)
      reply_address.model.class.handle_reply(reply_token, stripped_body, attached_files)
    else
      # no valid reply_address
    end
  end
end

#stripped_bodyObject



34
35
36
# File 'lib/replyr/reply_email.rb', line 34

def stripped_body
  EmailReplyParser.parse_reply(body, from).to_s.force_encoding("UTF-8")
end