Class: Gmail::Mailbox

Inherits:
Object show all
Defined in:
lib/gmail/mailbox.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gmail, name) ⇒ Mailbox

Returns a new instance of Mailbox.



13
14
15
16
# File 'lib/gmail/mailbox.rb', line 13

def initialize(gmail, name)
  @gmail = gmail
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/gmail/mailbox.rb', line 11

def name
  @name
end

Instance Method Details

#count(*args) ⇒ Object

This is a convenience method that really probably shouldn’t need to exist, but it does make code more readable if seriously all you want is the count of messages.



77
78
79
# File 'lib/gmail/mailbox.rb', line 77

def count(*args)
  emails(*args).length
end

#emails(key_or_opts = :all, opts = {}) ⇒ Object

Method: emails Args: [ :all | :unread | :read ] Opts: => Date.new



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gmail/mailbox.rb', line 29

def emails(key_or_opts = :all, opts={})
  if key_or_opts.is_a?(Hash) && opts.empty?
    search = ['ALL']
    opts = key_or_opts
  elsif key_or_opts.is_a?(Symbol) && opts.is_a?(Hash)
    aliases = {
      :all => ['ALL'],
      :unread => ['UNSEEN'],
      :read => ['SEEN']
    }
    search = aliases[key_or_opts]
  elsif key_or_opts.is_a?(Array) && opts.empty?
    search = key_or_opts
  else
    raise ArgumentError, "Couldn't make sense of arguments to #emails - should be an optional hash of options preceded by an optional read-status bit; OR simply an array of parameters to pass directly to the IMAP uid_search call."
  end
  if !opts.empty?

    if opts[:after] === opts[:before]
      opts[:on]     = opts[:before]
      opts[:after]  = nil
      opts[:before] = nil
    end
    
    # Support for several search macros
    # :before => Date, :on => Date, :since => Date, :from => String, :to => String
    search.concat ['SINCE', opts[:after].to_imap_date] if opts[:after]
    search.concat ['BEFORE', opts[:before].to_imap_date] if opts[:before]
    search.concat ['ON', opts[:on].to_imap_date] if opts[:on]
    search.concat ['FROM', opts[:from]] if opts[:from]
    search.concat ['TO', opts[:to]] if opts[:to]
    search.concat ['SUBJECT', opts[:subject]] if opts[:subject]

    # Gmail offers us to search the same way we do on the web interface
    # https://developers.google.com/gmail/imap_extensions
    # example: gmail.emails(gm: 'has:attachment in:unread "who is john galt"')
    # 
    search.concat ['X-GM-RAW', opts[:gm]] if opts[:gm]
  end

  # puts "Gathering #{(aliases[key] || key).inspect} messages for mailbox '#{name}'..."
  @gmail.in_mailbox(self) do
    @gmail.imap.uid_search(search).collect { |uid| messages[uid] ||= Message.new(@gmail, self, uid) }
  end
end

#inspectObject



18
19
20
# File 'lib/gmail/mailbox.rb', line 18

def inspect
  "<#Mailbox name=#{@name}>"
end

#messagesObject



81
82
83
# File 'lib/gmail/mailbox.rb', line 81

def messages
  @messages ||= {}
end

#to_sObject



22
23
24
# File 'lib/gmail/mailbox.rb', line 22

def to_s
  name
end