Class: Mastiff::Email::Message
- Inherits:
-
Object
- Object
- Mastiff::Email::Message
- Includes:
- Redis::Objects
- Defined in:
- lib/mastiff/message.rb
Instance Attribute Summary collapse
-
#attachment_analyzed ⇒ Object
Returns the value of attribute attachment_analyzed.
-
#attachment_name ⇒ Object
Returns the value of attribute attachment_name.
-
#attachment_size ⇒ Object
Returns the value of attribute attachment_size.
-
#busy ⇒ Object
Returns the value of attribute busy.
-
#has_attachments ⇒ Object
Returns the value of attribute has_attachments.
-
#header ⇒ Object
Returns the value of attribute header.
-
#mailbox ⇒ Object
Returns the value of attribute mailbox.
-
#raw_message ⇒ Object
Returns the value of attribute raw_message.
-
#stored_filename ⇒ Object
Returns the value of attribute stored_filename.
-
#uid ⇒ Object
Returns the value of attribute uid.
-
#uploader ⇒ Object
Returns the value of attribute uploader.
-
#validity_id ⇒ Object
Returns the value of attribute validity_id.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #as_mail ⇒ Object
- #attached_file_path ⇒ Object
- #busy? ⇒ Boolean
- #id ⇒ Object
-
#initialize(attrs = {}) ⇒ Message
constructor
A new instance of Message.
- #lock ⇒ Object
- #lock_and_save ⇒ Object
- #raw_source ⇒ Object
- #save ⇒ Object
- #sync_message_attachments ⇒ Object
- #unlock ⇒ Object
- #unlock_and_save ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Message
Returns a new instance of Message.
89 90 91 92 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 124 125 126 127 128 |
# File 'lib/mastiff/message.rb', line 89 def initialize(attrs = {}) attrs.deep_symbolize_keys! @uid = attrs[:uid] @validity_id = attrs[:validity_id] @raw_message = attrs[:raw_message] # @mail_message = attrs[:mail_message] = as_mail if and . @has_attachments = true = .[0] @attachment_name = .filename @attachment_size = 0 @stored_filename = @attachment_name.squish.gsub(" ", "_") @uploader = Mastiff..new self.class. << id @attachment_analyzed = false #decoded = attachment.body.decoded #@attachment_size = decoded.length end if attrs[:header].is_a? Hash @header = attrs[:header] end if and @header.blank? @header = { id: id, busy: false, from: [:from].display_names.first, sender_email: .from.first, subject: .subject, date: .date, attachment_name: @attachment_name, attachment_size: @attachment_size, stored_filename: @stored_filename, } elsif and not @header.blank? # TODO: Change this to a use a gem logger Sidekiq::Logging.logger.info "Header already existed" end end |
Instance Attribute Details
#attachment_analyzed ⇒ Object
Returns the value of attribute attachment_analyzed.
9 10 11 |
# File 'lib/mastiff/message.rb', line 9 def @attachment_analyzed end |
#attachment_name ⇒ Object
Returns the value of attribute attachment_name.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def @attachment_name end |
#attachment_size ⇒ Object
Returns the value of attribute attachment_size.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def @attachment_size end |
#busy ⇒ Object
Returns the value of attribute busy.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def busy @busy end |
#has_attachments ⇒ Object
Returns the value of attribute has_attachments.
9 10 11 |
# File 'lib/mastiff/message.rb', line 9 def @has_attachments end |
#header ⇒ Object
Returns the value of attribute header.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def header @header end |
#mailbox ⇒ Object
Returns the value of attribute mailbox.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def mailbox @mailbox end |
#raw_message ⇒ Object
Returns the value of attribute raw_message.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def @raw_message end |
#stored_filename ⇒ Object
Returns the value of attribute stored_filename.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def stored_filename @stored_filename end |
#uid ⇒ Object
Returns the value of attribute uid.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def uid @uid end |
#uploader ⇒ Object
Returns the value of attribute uploader.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def uploader @uploader end |
#validity_id ⇒ Object
Returns the value of attribute validity_id.
8 9 10 |
# File 'lib/mastiff/message.rb', line 8 def validity_id @validity_id end |
Class Method Details
.get(id) ⇒ Object
30 31 32 |
# File 'lib/mastiff/message.rb', line 30 def self.get(id) self.emails[id] end |
.ids ⇒ Object
26 27 28 29 |
# File 'lib/mastiff/message.rb', line 26 def self.ids prefix = validity self.emails.keys.select{|k| /#{prefix}:/ =~ k }.map{|id| id.split(':').last.to_i}.sort end |
.validity ⇒ Object
22 23 24 |
# File 'lib/mastiff/message.rb', line 22 def self.validity self.uid_validity.value.to_i end |
Instance Method Details
#<=>(other) ⇒ Object
48 49 50 |
# File 'lib/mastiff/message.rb', line 48 def <=> other self.id <=> other.id end |
#as_mail ⇒ Object
63 64 65 |
# File 'lib/mastiff/message.rb', line 63 def as_mail Mail.new raw_source end |
#attached_file_path ⇒ Object
86 87 88 |
# File 'lib/mastiff/message.rb', line 86 def attached_file_path File.join @uploader.store_dir, @stored_filename end |
#busy? ⇒ Boolean
33 34 35 |
# File 'lib/mastiff/message.rb', line 33 def busy? self.header[:busy] end |
#id ⇒ Object
52 53 54 |
# File 'lib/mastiff/message.rb', line 52 def id "#{self.validity_id}:#{self.uid}" end |
#lock ⇒ Object
42 43 44 |
# File 'lib/mastiff/message.rb', line 42 def lock @header[:busy] = true end |
#lock_and_save ⇒ Object
36 37 38 |
# File 'lib/mastiff/message.rb', line 36 def lock_and_save lock and save end |
#raw_source ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/mastiff/message.rb', line 56 def raw_source if @raw_message.blank? self.class.raw[id] else @raw_message end end |
#save ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mastiff/message.rb', line 132 def save = as_mail unless .blank? lock if @has_attachments and not @attachment_analyzed self.class.raw[id] = self. end self.class.emails[id] = self Mastiff..perform_async(id) if @has_attachments and not @attachment_analyzed end |
#sync_message_attachments ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mastiff/message.rb', line 66 def = as_mail if and . and not @attachment_analyzed = .[0] decoded = .body.decoded @attachment_name = .filename @attachment_size = decoded.length @header[:attachment_size] = @attachment_size @stored_filename = @attachment_name.squish.gsub(" ", "_") @uploader.store_mime(@stored_filename,decoded) @attachment_analyzed = true self.class..delete(id) unlock save Mastiff..perform_async(id) end end |
#unlock ⇒ Object
45 46 47 |
# File 'lib/mastiff/message.rb', line 45 def unlock @header[:busy] = false end |
#unlock_and_save ⇒ Object
39 40 41 |
# File 'lib/mastiff/message.rb', line 39 def unlock_and_save unlock and save end |