Class: Gmail::Labels

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gmail/labels.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Labels

Returns a new instance of Labels.



7
8
9
# File 'lib/gmail/labels.rb', line 7

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly) Also known as: conn

Returns the value of attribute connection.



4
5
6
# File 'lib/gmail/labels.rb', line 4

def connection
  @connection
end

Instance Method Details

#allObject Also known as: list, to_a

Get list of all defined labels.



12
13
14
15
16
17
18
19
20
21
22
# 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

#create(label) ⇒ Object Also known as: new, add

Creates given label in your account.



43
44
45
46
47
# File 'lib/gmail/labels.rb', line 43

def create(label)
  !!conn.create(Net::IMAP.encode_utf7(label))
rescue StandardError
  false
end

#delete(label) ⇒ Object Also known as: remove

Deletes given label from your account.



52
53
54
55
56
# File 'lib/gmail/labels.rb', line 52

def delete(label)
  !!conn.delete(Net::IMAP.encode_utf7(label))
rescue StandardError
  false
end

#each(*args, &block) ⇒ Object



32
33
34
# File 'lib/gmail/labels.rb', line 32

def each(*args, &block)
  all.each(*args, &block)
end

#exists?(label) ⇒ Boolean Also known as: exist?

Returns true when given label defined.

Returns:

  • (Boolean)


37
38
39
# File 'lib/gmail/labels.rb', line 37

def exists?(label)
  all.include?(label)
end

#inspectObject



59
60
61
# File 'lib/gmail/labels.rb', line 59

def inspect
  "#<Gmail::Labels#{'0x%04x' % (object_id << 1)}>"
end

#localize(label) ⇒ Object

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.



68
69
70
71
72
73
74
75
76
77
78
# 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

#sublabels_or_label(label) ⇒ Object



26
27
28
29
30
# 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