Class: Gmail::Labels

Inherits:
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.


46
47
48
# File 'lib/gmail/labels.rb', line 46

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

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

Deletes given label from your account.


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

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

#each(*args, &block) ⇒ Object


35
36
37
# File 'lib/gmail/labels.rb', line 35

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

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

Returns true when given label defined.

Returns:

  • (Boolean)

40
41
42
# File 'lib/gmail/labels.rb', line 40

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

#inspectObject


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

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.


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gmail/labels.rb', line 67

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
31
32
33
# File 'lib/gmail/labels.rb', line 26

def sublabels_or_label(label)
  if label.attr.include? :Hasnochildren
    @list << label
  else
    @list << label
    conn.list("#{label.name}/", "%").each { |l| sublabels_or_label(l) }
  end
end