Class: GMail::Entity

Inherits:
Object
  • Object
show all
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)

Instance Method Summary (collapse)

Constructor Details

- (Entity) initialize(filename)

Create a new object based on the metadata from filename.

Parameters:

  • filename

    name of the file to open

Raises:

  • (ArgumentError)


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
  @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
  @tags
end

Instance Method Details

- (String) gm_id

Return the GMail ID

Returns:

  • (String)

    gm_id



64
65
66
# File 'lib/gmail/entity.rb', line 64

def gm_id
  @meta['gm_id'].to_s
end

- (String) load

Preload metadata

Returns:

  • (String)

    returns the gm_id

Raises:

  • (DataError)


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.meta_path) 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

Returns:

  • (Mail::Message)

    returns the mail content



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

Returns:

  • (String)

    returns the full filename to the mail itself



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

Returns:

  • (String)

    returns full filename of the metadata



82
83
84
# File 'lib/gmail/entity.rb', line 82

def meta_path
  @path + '.meta'
end

- (String) thread_ids

Return the GMail thread IDs

Returns:

  • (String)

    thread_ids



70
71
72
# File 'lib/gmail/entity.rb', line 70

def thread_ids
  @meta['thread_ids'].to_s
end