Class: GMail::Entity
- Inherits:
-
Object
- Object
- GMail::Entity
- Defined in:
- lib/gmail/entity.rb
Overview
Handle GMail::Entity mails with .eml
as raw mail and
.meta
as metadata (i.e.tags, date)
Instance Attribute Summary (collapse)
-
- (Object) meta
readonly
Returns the value of attribute meta.
-
- (Object) name
readonly
Returns the value of attribute name.
-
- (Object) tags
readonly
Returns the value of attribute tags.
Instance Method Summary (collapse)
-
- (String) gm_id
Return the GMail ID.
-
- (Entity) initialize(filename)
constructor
Create a new object based on the metadata from
filename
. -
- (String) load
Preload metadata.
-
- (Mail::Message) mail
Load and return the mail associated with the given metadata.
-
- (String) mail_path
Return the full path to the mail.
-
- (String) meta_path
Return the full path to the metadata file.
-
- (String) thread_ids
Return the GMail thread IDs.
Constructor Details
- (Entity) initialize(filename)
Create a new object based on the metadata from filename
.
22 23 24 25 26 27 28 29 30 |
# File 'lib/gmail/entity.rb', line 22 def initialize(filename) @path, ext = filename.split(/\./) raise(ArgumentError, "File should end in .meta: #{filename}") if ext != 'meta' raise(ArgumentError, "File does not exist #{filename}") if not File.exists?(filename) @name = File.basename(@path) @mail = nil @tags = [] self.load end |
Instance Attribute Details
- (Object) meta (readonly)
Returns the value of attribute meta
17 18 19 |
# File 'lib/gmail/entity.rb', line 17 def @meta end |
- (Object) name (readonly)
Returns the value of attribute name
16 17 18 |
# File 'lib/gmail/entity.rb', line 16 def name @name end |
- (Object) tags (readonly)
Returns the value of attribute tags
18 19 20 |
# File 'lib/gmail/entity.rb', line 18 def @tags end |
Instance Method Details
- (String) gm_id
Return the GMail ID
64 65 66 |
# File 'lib/gmail/entity.rb', line 64 def gm_id @meta['gm_id'].to_s end |
- (String) load
Preload metadata
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gmail/entity.rb', line 34 def load File.open(self.) do |fh| @meta = JSON.load(fh) end # check consistency raise DataError if @meta.nil? if @name.to_i != @meta['gm_id'] $stderr.puts(@meta) raise DataError, "Error: Internal inconsistency on #{@name} vs #{@meta['gm_id']}" end # Remove "internal tags" @tags = @meta['labels'].delete_if{|e| e =~ /^\\/ } @meta['gm_id'] end |
- (Mail::Message) mail
Load and return the mail associated with the given metadata
53 54 55 56 57 58 59 60 |
# File 'lib/gmail/entity.rb', line 53 def mail if @mail.nil? @mail = Mail.read(self.mail_path) lines = @mail.body.raw_source.split(%r{\r\n}).size @mail['Lines'] = lines.to_s end @mail end |
- (String) mail_path
Return the full path to the mail
76 77 78 |
# File 'lib/gmail/entity.rb', line 76 def mail_path @path + '.eml' end |
- (String) meta_path
Return the full path to the metadata file
82 83 84 |
# File 'lib/gmail/entity.rb', line 82 def @path + '.meta' end |
- (String) thread_ids
Return the GMail thread IDs
70 71 72 |
# File 'lib/gmail/entity.rb', line 70 def thread_ids @meta['thread_ids'].to_s end |