class Gmail::Labels

Attributes

conn[R]
connection[R]

Public Class Methods

new(connection) click to toggle source
# File lib/gmail/labels.rb, line 7
def initialize(connection)
  @connection = connection
end

Public Instance Methods

add(label)
Alias for: create
all() click to toggle source

Get list of all defined labels.

# File lib/gmail/labels.rb, line 12
def all
  @list = []

  ## check each item in list for subfolders
  conn.list("", "%").each { |l| sublabels_or_label(l) }

  @list.inject([]) do |labels, label|
    label[:name].each_line { |l| labels << Net::IMAP.decode_utf7(l) }
    labels
  end
end
Also aliased as: list, to_a
create(label) click to toggle source

Creates given label in your account.

# File lib/gmail/labels.rb, line 43
def create(label)
  !!conn.create(Net::IMAP.encode_utf7(label))
rescue StandardError
  false
end
Also aliased as: new, add
delete(label) click to toggle source

Deletes given label from your account.

# File lib/gmail/labels.rb, line 52
def delete(label)
  !!conn.delete(Net::IMAP.encode_utf7(label))
rescue StandardError
  false
end
Also aliased as: remove
each(*args, &block) click to toggle source
# File lib/gmail/labels.rb, line 32
def each(*args, &block)
  all.each(*args, &block)
end
exist?(label)
Alias for: exists?
exists?(label) click to toggle source

Returns true when given label defined.

# File lib/gmail/labels.rb, line 37
def exists?(label)
  all.include?(label)
end
Also aliased as: exist?
inspect() click to toggle source
# File lib/gmail/labels.rb, line 59
def inspect
  "#<Gmail::Labels#{'0x%04x' % (object_id << 1)}>"
end
list()
Alias for: all
localize(label) click to toggle source

Accepts standard mailbox flags returned by LIST's special-use extension: :Inbox, :All, :Drafts, :Sent, :Trash, :Important, :Junk, :Flagged and their string equivalents. Capitalization agnostic.

# File lib/gmail/labels.rb, line 68
def localize(label)
  type = label.to_sym.capitalize
  if [:All, :Drafts, :Sent, :Trash, :Important, :Junk, :Flagged].include? type
    @mailboxes ||= connection.list("", "*")
    @mailboxes.select { |box| box.attr.include? type }.collect(&:name).compact.uniq.first || label.to_s
  elsif type == :Inbox
    'INBOX'
  else
    label
  end
end
new(label)
Alias for: create
remove(label)
Alias for: delete
sublabels_or_label(label) click to toggle source
# File lib/gmail/labels.rb, line 26
def sublabels_or_label(label)
  @list << label
  return if label.attr.include? :Hasnochildren
  conn.list("#{label.name}/", "%").each { |l| sublabels_or_label(l) }
end
to_a()
Alias for: all