gmail 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +27 -27
- data/.rspec +1 -1
- data/.rubocop.yml +13 -13
- data/.rubocop_todo.yml +239 -239
- data/.travis.yml +19 -19
- data/CHANGELOG.md +145 -139
- data/Gemfile +3 -3
- data/LICENSE +21 -21
- data/README.md +355 -354
- data/Rakefile +46 -46
- data/gmail.gemspec +34 -34
- data/lib/gmail.rb +78 -78
- data/lib/gmail/client.rb +34 -34
- data/lib/gmail/client/base.rb +229 -229
- data/lib/gmail/client/plain.rb +24 -24
- data/lib/gmail/client/xoauth.rb +68 -68
- data/lib/gmail/client/xoauth2.rb +39 -39
- data/lib/gmail/imap_extensions.rb +159 -150
- data/lib/gmail/labels.rb +79 -79
- data/lib/gmail/mailbox.rb +175 -135
- data/lib/gmail/message.rb +207 -207
- data/lib/gmail/version.rb +3 -3
- data/spec/account.yml.example +1 -1
- data/spec/account.yml.obfus +2 -2
- data/spec/gmail/client/base_spec.rb +5 -5
- data/spec/gmail/client/plain_spec.rb +169 -168
- data/spec/gmail/client/xoauth2_spec.rb +186 -186
- data/spec/gmail/client/xoauth_spec.rb +5 -5
- data/spec/gmail/client_spec.rb +5 -5
- data/spec/gmail/imap_extensions_spec.rb +47 -12
- data/spec/gmail/labels_spec.rb +27 -18
- data/spec/gmail/mailbox_spec.rb +84 -47
- data/spec/gmail/message_spec.rb +181 -181
- data/spec/gmail_spec.rb +39 -39
- data/spec/recordings/gmail/_new_connects_with_client_and_give_it_context_when_block_given.yml +28 -28
- data/spec/recordings/gmail/_new_connects_with_gmail_service_and_return_valid_connection_object.yml +28 -28
- data/spec/recordings/gmail/_new_does_not_raise_error_when_couldn_t_connect_with_given_account.yml +13 -13
- data/spec/recordings/gmail/_new_raises_error_when_couldn_t_connect_with_given_account.yml +13 -13
- data/spec/recordings/gmail_client_plain/instance/_connection_automatically_logs_in_to_gmail_account_when_it_s_called.yml +42 -42
- data/spec/recordings/gmail_client_plain/instance/delivers_inline_composed_email.yml +42 -42
- data/spec/recordings/gmail_client_plain/instance/does_not_log_in_when_given_gmail_account_is_invalid.yml +13 -13
- data/spec/recordings/gmail_client_plain/instance/does_not_raise_error_even_though_gmail_account_is_invalid.yml +13 -13
- data/spec/recordings/gmail_client_plain/instance/labels/checks_if_there_is_given_label_defined.yml +196 -196
- data/spec/recordings/gmail_client_plain/instance/labels/creates_given_label.yml +151 -151
- data/spec/recordings/gmail_client_plain/instance/labels/removes_existing_label.yml +146 -146
- data/spec/recordings/gmail_client_plain/instance/labels/returns_list_of_all_available_labels.yml +113 -113
- data/spec/recordings/gmail_client_plain/instance/properly_logs_in_to_valid_gmail_account.yml +42 -42
- data/spec/recordings/gmail_client_plain/instance/properly_logs_out_from_gmail.yml +42 -42
- data/spec/recordings/gmail_client_plain/instance/properly_switches_to_given_mailbox.yml +109 -109
- data/spec/recordings/gmail_client_plain/instance/properly_switches_to_given_mailbox_using_block_style.yml +109 -109
- data/spec/recordings/gmail_client_plain/instance/raises_error_when_given_gmail_account_is_invalid_and_errors_enabled.yml +13 -13
- data/spec/recordings/gmail_client_xo_auth2/instance/does_not_log_in_when_given_gmail_account_is_invalid.yml +13 -13
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/checks_if_there_is_given_label_defined.yml +27 -27
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/creates_given_label.yml +39 -39
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/removes_existing_label.yml +39 -39
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/returns_list_of_all_available_labels.yml +27 -27
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_logs_in_to_valid_gmail_account.yml +15 -15
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_logs_out_from_gmail.yml +15 -15
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_switches_to_given_mailbox.yml +40 -40
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_switches_to_given_mailbox_using_block_style.yml +40 -40
- data/spec/recordings/gmail_client_xo_auth2/instance/raises_error_when_given_gmail_account_is_invalid_and_errors_enabled.yml +13 -13
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/all/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/and_the_mailbox_does_not_exist/returns_the_mailbox_name_as_a_string.yml +110 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/drafts/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/flagged/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/important/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/inbox/localizes_into_the_appropriate_label.yml +42 -42
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/junk/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/sent/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/trash/localizes_into_the_appropriate_label.yml +116 -116
- data/spec/recordings/gmail_mailbox/instance/counts_all_emails.yml +277 -277
- data/spec/recordings/gmail_mailbox/instance/finds_messages.yml +586 -586
- data/spec/recordings/gmail_mailbox/instance/waits_once.yml +136 -0
- data/spec/recordings/gmail_mailbox/instance/waits_repeatedly.yml +141 -0
- data/spec/recordings/gmail_mailbox/instance/waits_with_29-minute_re-issue.yml +136 -0
- data/spec/recordings/gmail_mailbox/instance/waits_with_an_unblocked_connection.yml +207 -0
- data/spec/recordings/gmail_mailbox/on_initialize/sets_client_and_name.yml +42 -42
- data/spec/recordings/gmail_mailbox/on_initialize/works_in_inbox_by_default.yml +42 -42
- data/spec/recordings/gmail_message/initialize/sets_prefetch_attrs.yml +578 -578
- data/spec/recordings/gmail_message/initialize/sets_uid_and_mailbox.yml +580 -580
- data/spec/recordings/gmail_message/instance_methods/deletes_itself.yml +637 -637
- data/spec/recordings/gmail_message/instance_methods/marks_itself_read.yml +682 -682
- data/spec/recordings/gmail_message/instance_methods/marks_itself_unread.yml +686 -686
- data/spec/recordings/gmail_message/instance_methods/moves_from_one_tag_to_other.yml +862 -862
- data/spec/recordings/gmail_message/instance_methods/removes_a_given_label.yml +776 -776
- data/spec/recordings/gmail_message/instance_methods/removes_a_given_label_with_old_method.yml +776 -776
- data/spec/recordings/gmail_message/instance_methods/sets_given_label.yml +690 -690
- data/spec/recordings/gmail_message/instance_methods/sets_given_label_with_old_method.yml +691 -691
- data/spec/spec_helper.rb +53 -53
- data/spec/support/imap_mock.rb +181 -129
- data/spec/support/obfuscation.rb +52 -52
- metadata +78 -5
data/lib/gmail/labels.rb
CHANGED
@@ -1,79 +1,79 @@
|
|
1
|
-
module Gmail
|
2
|
-
class Labels
|
3
|
-
include Enumerable
|
4
|
-
attr_reader :connection
|
5
|
-
alias :conn :connection
|
6
|
-
|
7
|
-
def initialize(connection)
|
8
|
-
@connection = connection
|
9
|
-
end
|
10
|
-
|
11
|
-
# Get list of all defined labels.
|
12
|
-
def all
|
13
|
-
@list = []
|
14
|
-
|
15
|
-
## check each item in list for subfolders
|
16
|
-
conn.list("", "%").each { |l| sublabels_or_label(l) }
|
17
|
-
|
18
|
-
@list.inject([]) do |labels, label|
|
19
|
-
label[:name].each_line { |l| labels << Net::IMAP.decode_utf7(l) }
|
20
|
-
labels
|
21
|
-
end
|
22
|
-
end
|
23
|
-
alias :list :all
|
24
|
-
alias :to_a :all
|
25
|
-
|
26
|
-
def sublabels_or_label(label)
|
27
|
-
if label.attr.include? :Hasnochildren
|
28
|
-
@list << label
|
29
|
-
else
|
30
|
-
@list << label
|
31
|
-
conn.list("#{label.name}/", "%").each { |l| sublabels_or_label(l) }
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def each(*args, &block)
|
36
|
-
all.each(*args, &block)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Returns +true+ when given label defined.
|
40
|
-
def exists?(label)
|
41
|
-
all.include?(label)
|
42
|
-
end
|
43
|
-
alias :exist? :exists?
|
44
|
-
|
45
|
-
# Creates given label in your account.
|
46
|
-
def create(label)
|
47
|
-
!!conn.create(Net::IMAP.encode_utf7(label)) rescue false
|
48
|
-
end
|
49
|
-
alias :new :create
|
50
|
-
alias :add :create
|
51
|
-
|
52
|
-
# Deletes given label from your account.
|
53
|
-
def delete(label)
|
54
|
-
!!conn.delete(Net::IMAP.encode_utf7(label)) rescue false
|
55
|
-
end
|
56
|
-
alias :remove :delete
|
57
|
-
|
58
|
-
def inspect
|
59
|
-
"#<Gmail::Labels#{'0x%04x' % (object_id << 1)}>"
|
60
|
-
end
|
61
|
-
|
62
|
-
# Localizes a specific label flag into a label name
|
63
|
-
|
64
|
-
# Accepts standard mailbox flags returned by LIST's special-use extension:
|
65
|
-
# :Inbox, :All, :Drafts, :Sent, :Trash, :Important, :Junk, :Flagged
|
66
|
-
# and their string equivalents. Capitalization agnostic.
|
67
|
-
def localize(label)
|
68
|
-
type = label.to_sym.capitalize
|
69
|
-
if [:All, :Drafts, :Sent, :Trash, :Important, :Junk, :Flagged].include? type
|
70
|
-
@mailboxes ||= connection.list("", "*")
|
71
|
-
@mailboxes.select { |box| box.attr.include? type }.collect(&:name).compact.uniq.first
|
72
|
-
elsif type == :Inbox
|
73
|
-
'INBOX'
|
74
|
-
else
|
75
|
-
label
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end # Labels
|
79
|
-
end # Gmail
|
1
|
+
module Gmail
|
2
|
+
class Labels
|
3
|
+
include Enumerable
|
4
|
+
attr_reader :connection
|
5
|
+
alias :conn :connection
|
6
|
+
|
7
|
+
def initialize(connection)
|
8
|
+
@connection = connection
|
9
|
+
end
|
10
|
+
|
11
|
+
# Get list of all defined labels.
|
12
|
+
def all
|
13
|
+
@list = []
|
14
|
+
|
15
|
+
## check each item in list for subfolders
|
16
|
+
conn.list("", "%").each { |l| sublabels_or_label(l) }
|
17
|
+
|
18
|
+
@list.inject([]) do |labels, label|
|
19
|
+
label[:name].each_line { |l| labels << Net::IMAP.decode_utf7(l) }
|
20
|
+
labels
|
21
|
+
end
|
22
|
+
end
|
23
|
+
alias :list :all
|
24
|
+
alias :to_a :all
|
25
|
+
|
26
|
+
def sublabels_or_label(label)
|
27
|
+
if label.attr.include? :Hasnochildren
|
28
|
+
@list << label
|
29
|
+
else
|
30
|
+
@list << label
|
31
|
+
conn.list("#{label.name}/", "%").each { |l| sublabels_or_label(l) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def each(*args, &block)
|
36
|
+
all.each(*args, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns +true+ when given label defined.
|
40
|
+
def exists?(label)
|
41
|
+
all.include?(label)
|
42
|
+
end
|
43
|
+
alias :exist? :exists?
|
44
|
+
|
45
|
+
# Creates given label in your account.
|
46
|
+
def create(label)
|
47
|
+
!!conn.create(Net::IMAP.encode_utf7(label)) rescue false
|
48
|
+
end
|
49
|
+
alias :new :create
|
50
|
+
alias :add :create
|
51
|
+
|
52
|
+
# Deletes given label from your account.
|
53
|
+
def delete(label)
|
54
|
+
!!conn.delete(Net::IMAP.encode_utf7(label)) rescue false
|
55
|
+
end
|
56
|
+
alias :remove :delete
|
57
|
+
|
58
|
+
def inspect
|
59
|
+
"#<Gmail::Labels#{'0x%04x' % (object_id << 1)}>"
|
60
|
+
end
|
61
|
+
|
62
|
+
# Localizes a specific label flag into a label name
|
63
|
+
|
64
|
+
# Accepts standard mailbox flags returned by LIST's special-use extension:
|
65
|
+
# :Inbox, :All, :Drafts, :Sent, :Trash, :Important, :Junk, :Flagged
|
66
|
+
# and their string equivalents. Capitalization agnostic.
|
67
|
+
def localize(label)
|
68
|
+
type = label.to_sym.capitalize
|
69
|
+
if [:All, :Drafts, :Sent, :Trash, :Important, :Junk, :Flagged].include? type
|
70
|
+
@mailboxes ||= connection.list("", "*")
|
71
|
+
@mailboxes.select { |box| box.attr.include? type }.collect(&:name).compact.uniq.first || label.to_s
|
72
|
+
elsif type == :Inbox
|
73
|
+
'INBOX'
|
74
|
+
else
|
75
|
+
label
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end # Labels
|
79
|
+
end # Gmail
|
data/lib/gmail/mailbox.rb
CHANGED
@@ -1,135 +1,175 @@
|
|
1
|
-
module Gmail
|
2
|
-
class Mailbox
|
3
|
-
MAILBOX_ALIASES = {
|
4
|
-
:all => ['ALL'],
|
5
|
-
:seen => ['SEEN'],
|
6
|
-
:unseen => ['UNSEEN'],
|
7
|
-
:read => ['SEEN'],
|
8
|
-
:unread => ['UNSEEN'],
|
9
|
-
:flagged => ['FLAGGED'],
|
10
|
-
:unflagged => ['UNFLAGGED'],
|
11
|
-
:starred => ['FLAGGED'],
|
12
|
-
:unstarred => ['UNFLAGGED'],
|
13
|
-
:deleted => ['DELETED'],
|
14
|
-
:undeleted => ['UNDELETED'],
|
15
|
-
:draft => ['DRAFT'],
|
16
|
-
:undrafted => ['UNDRAFT']
|
17
|
-
}
|
18
|
-
|
19
|
-
attr_reader :name
|
20
|
-
attr_reader :encoded_name
|
21
|
-
|
22
|
-
def initialize(gmail, name = "INBOX")
|
23
|
-
@name = Net::IMAP.decode_utf7(name)
|
24
|
-
@encoded_name = Net::IMAP.encode_utf7(name)
|
25
|
-
@gmail = gmail
|
26
|
-
end
|
27
|
-
|
28
|
-
def fetch_uids(*args)
|
29
|
-
args << :all if args.size == 0
|
30
|
-
|
31
|
-
if args.first.is_a?(Symbol)
|
32
|
-
search = MAILBOX_ALIASES[args.shift].dup
|
33
|
-
opts = args.first.is_a?(Hash) ? args.first : {}
|
34
|
-
|
35
|
-
opts[:after] and search.concat ['SINCE', opts[:after].to_imap_date]
|
36
|
-
opts[:before] and search.concat ['BEFORE', opts[:before].to_imap_date]
|
37
|
-
opts[:on] and search.concat ['ON', opts[:on].to_imap_date]
|
38
|
-
opts[:from] and search.concat ['FROM', opts[:from]]
|
39
|
-
opts[:to] and search.concat ['TO', opts[:to]]
|
40
|
-
opts[:subject] and search.concat ['SUBJECT', opts[:subject]]
|
41
|
-
opts[:label] and search.concat ['LABEL', opts[:label]]
|
42
|
-
opts[:attachment] and search.concat ['HAS', 'attachment']
|
43
|
-
opts[:search] and search.concat ['BODY', opts[:search]]
|
44
|
-
opts[:body] and search.concat ['BODY', opts[:body]]
|
45
|
-
opts[:uid] and search.concat ['UID', opts[:uid]]
|
46
|
-
opts[:gm] and search.concat ['X-GM-RAW', opts[:gm]]
|
47
|
-
opts[:query] and search.concat opts[:query]
|
48
|
-
|
49
|
-
@gmail.mailbox(name) {
|
50
|
-
@gmail.conn.uid_search(search)
|
51
|
-
}
|
52
|
-
elsif args.first.is_a?(Hash)
|
53
|
-
fetch_uids(:all, args.first)
|
54
|
-
else
|
55
|
-
raise ArgumentError, "Invalid search criteria"
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# Returns list of emails which meets given criteria.
|
60
|
-
#
|
61
|
-
# ==== Examples
|
62
|
-
#
|
63
|
-
# gmail.inbox.emails(:all)
|
64
|
-
# gmail.inbox.emails(:unread, :from => "friend@gmail.com")
|
65
|
-
# gmail.inbox.emails(:all, :after => Time.now-(20*24*3600))
|
66
|
-
# gmail.mailbox("Test").emails(:read)
|
67
|
-
#
|
68
|
-
# gmail.mailbox("Test") do |box|
|
69
|
-
# box.emails(:read)
|
70
|
-
# box.emails(:unread) do |email|
|
71
|
-
# ... do something with each email...
|
72
|
-
# end
|
73
|
-
# end
|
74
|
-
def emails(*args, &block)
|
75
|
-
fetch_uids(*args).collect do |uid|
|
76
|
-
message = Message.new(self, uid)
|
77
|
-
yield(message) if block_given?
|
78
|
-
message
|
79
|
-
end
|
80
|
-
end
|
81
|
-
alias :search :emails
|
82
|
-
alias :find :emails
|
83
|
-
|
84
|
-
def emails_in_batches(*args, &block)
|
85
|
-
messages = Array.new
|
86
|
-
|
87
|
-
uids = fetch_uids(*args)
|
88
|
-
if uids && uids.any?
|
89
|
-
uids.each_slice(100) do |slice|
|
90
|
-
@gmail.conn.uid_fetch(slice, Message::PREFETCH_ATTRS).each do |data|
|
91
|
-
message = Message.new(self, nil, data)
|
92
|
-
yield(message) if block_given?
|
93
|
-
messages << message
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
messages
|
99
|
-
end
|
100
|
-
alias :search_in_batches :emails_in_batches
|
101
|
-
alias :find_in_batches :emails_in_batches
|
102
|
-
|
103
|
-
# This is a convenience method that really probably shouldn't need to exist,
|
104
|
-
# but it does make code more readable, if seriously all you want is the count
|
105
|
-
# of messages.
|
106
|
-
#
|
107
|
-
# ==== Examples
|
108
|
-
#
|
109
|
-
# gmail.inbox.count(:all)
|
110
|
-
# gmail.inbox.count(:unread, :from => "friend@gmail.com")
|
111
|
-
# gmail.mailbox("Test").count(:all, :after => Time.now-(20*24*3600))
|
112
|
-
def count(*args)
|
113
|
-
emails(*args).size
|
114
|
-
end
|
115
|
-
|
116
|
-
# This permanently removes messages which are marked as deleted
|
117
|
-
def expunge
|
118
|
-
@gmail.mailbox(name) { @gmail.conn.expunge }
|
119
|
-
end
|
120
|
-
|
121
|
-
def
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
1
|
+
module Gmail
|
2
|
+
class Mailbox
|
3
|
+
MAILBOX_ALIASES = {
|
4
|
+
:all => ['ALL'],
|
5
|
+
:seen => ['SEEN'],
|
6
|
+
:unseen => ['UNSEEN'],
|
7
|
+
:read => ['SEEN'],
|
8
|
+
:unread => ['UNSEEN'],
|
9
|
+
:flagged => ['FLAGGED'],
|
10
|
+
:unflagged => ['UNFLAGGED'],
|
11
|
+
:starred => ['FLAGGED'],
|
12
|
+
:unstarred => ['UNFLAGGED'],
|
13
|
+
:deleted => ['DELETED'],
|
14
|
+
:undeleted => ['UNDELETED'],
|
15
|
+
:draft => ['DRAFT'],
|
16
|
+
:undrafted => ['UNDRAFT']
|
17
|
+
}
|
18
|
+
|
19
|
+
attr_reader :name
|
20
|
+
attr_reader :encoded_name
|
21
|
+
|
22
|
+
def initialize(gmail, name = "INBOX")
|
23
|
+
@name = Net::IMAP.decode_utf7(name)
|
24
|
+
@encoded_name = Net::IMAP.encode_utf7(name)
|
25
|
+
@gmail = gmail
|
26
|
+
end
|
27
|
+
|
28
|
+
def fetch_uids(*args)
|
29
|
+
args << :all if args.size == 0
|
30
|
+
|
31
|
+
if args.first.is_a?(Symbol)
|
32
|
+
search = MAILBOX_ALIASES[args.shift].dup
|
33
|
+
opts = args.first.is_a?(Hash) ? args.first : {}
|
34
|
+
|
35
|
+
opts[:after] and search.concat ['SINCE', opts[:after].to_imap_date]
|
36
|
+
opts[:before] and search.concat ['BEFORE', opts[:before].to_imap_date]
|
37
|
+
opts[:on] and search.concat ['ON', opts[:on].to_imap_date]
|
38
|
+
opts[:from] and search.concat ['FROM', opts[:from]]
|
39
|
+
opts[:to] and search.concat ['TO', opts[:to]]
|
40
|
+
opts[:subject] and search.concat ['SUBJECT', opts[:subject]]
|
41
|
+
opts[:label] and search.concat ['LABEL', opts[:label]]
|
42
|
+
opts[:attachment] and search.concat ['HAS', 'attachment']
|
43
|
+
opts[:search] and search.concat ['BODY', opts[:search]]
|
44
|
+
opts[:body] and search.concat ['BODY', opts[:body]]
|
45
|
+
opts[:uid] and search.concat ['UID', opts[:uid]]
|
46
|
+
opts[:gm] and search.concat ['X-GM-RAW', opts[:gm]]
|
47
|
+
opts[:query] and search.concat opts[:query]
|
48
|
+
|
49
|
+
@gmail.mailbox(name) {
|
50
|
+
@gmail.conn.uid_search(search)
|
51
|
+
}
|
52
|
+
elsif args.first.is_a?(Hash)
|
53
|
+
fetch_uids(:all, args.first)
|
54
|
+
else
|
55
|
+
raise ArgumentError, "Invalid search criteria"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns list of emails which meets given criteria.
|
60
|
+
#
|
61
|
+
# ==== Examples
|
62
|
+
#
|
63
|
+
# gmail.inbox.emails(:all)
|
64
|
+
# gmail.inbox.emails(:unread, :from => "friend@gmail.com")
|
65
|
+
# gmail.inbox.emails(:all, :after => Time.now-(20*24*3600))
|
66
|
+
# gmail.mailbox("Test").emails(:read)
|
67
|
+
#
|
68
|
+
# gmail.mailbox("Test") do |box|
|
69
|
+
# box.emails(:read)
|
70
|
+
# box.emails(:unread) do |email|
|
71
|
+
# ... do something with each email...
|
72
|
+
# end
|
73
|
+
# end
|
74
|
+
def emails(*args, &block)
|
75
|
+
fetch_uids(*args).collect do |uid|
|
76
|
+
message = Message.new(self, uid)
|
77
|
+
yield(message) if block_given?
|
78
|
+
message
|
79
|
+
end
|
80
|
+
end
|
81
|
+
alias :search :emails
|
82
|
+
alias :find :emails
|
83
|
+
|
84
|
+
def emails_in_batches(*args, &block)
|
85
|
+
messages = Array.new
|
86
|
+
|
87
|
+
uids = fetch_uids(*args)
|
88
|
+
if uids && uids.any?
|
89
|
+
uids.each_slice(100) do |slice|
|
90
|
+
@gmail.conn.uid_fetch(slice, Message::PREFETCH_ATTRS).each do |data|
|
91
|
+
message = Message.new(self, nil, data)
|
92
|
+
yield(message) if block_given?
|
93
|
+
messages << message
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
messages
|
99
|
+
end
|
100
|
+
alias :search_in_batches :emails_in_batches
|
101
|
+
alias :find_in_batches :emails_in_batches
|
102
|
+
|
103
|
+
# This is a convenience method that really probably shouldn't need to exist,
|
104
|
+
# but it does make code more readable, if seriously all you want is the count
|
105
|
+
# of messages.
|
106
|
+
#
|
107
|
+
# ==== Examples
|
108
|
+
#
|
109
|
+
# gmail.inbox.count(:all)
|
110
|
+
# gmail.inbox.count(:unread, :from => "friend@gmail.com")
|
111
|
+
# gmail.mailbox("Test").count(:all, :after => Time.now-(20*24*3600))
|
112
|
+
def count(*args)
|
113
|
+
emails(*args).size
|
114
|
+
end
|
115
|
+
|
116
|
+
# This permanently removes messages which are marked as deleted
|
117
|
+
def expunge
|
118
|
+
@gmail.mailbox(name) { @gmail.conn.expunge }
|
119
|
+
end
|
120
|
+
|
121
|
+
def wait(options = {}, &block)
|
122
|
+
loop do
|
123
|
+
wait_once(options, &block)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def wait_once(options = {})
|
128
|
+
options[:idle_timeout] ||= 29 * 60
|
129
|
+
|
130
|
+
response = nil
|
131
|
+
loop do
|
132
|
+
complete_cond = @gmail.conn.new_cond
|
133
|
+
complete_now = false
|
134
|
+
|
135
|
+
@gmail.conn.idle do |resp|
|
136
|
+
if resp.kind_of?(Net::IMAP::ContinuationRequest) && resp.data.text == 'idling'
|
137
|
+
Thread.new do
|
138
|
+
@gmail.conn.synchronize do
|
139
|
+
complete_cond.wait(options[:idle_timeout]) unless complete_now
|
140
|
+
@gmail.conn.idle_done
|
141
|
+
end
|
142
|
+
end
|
143
|
+
elsif resp.kind_of?(Net::IMAP::UntaggedResponse) && resp.name == 'EXISTS'
|
144
|
+
response = resp
|
145
|
+
|
146
|
+
@gmail.conn.synchronize do
|
147
|
+
complete_now = true
|
148
|
+
complete_cond.signal
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
break if response
|
154
|
+
end
|
155
|
+
|
156
|
+
yield response if block_given?
|
157
|
+
|
158
|
+
nil
|
159
|
+
end
|
160
|
+
|
161
|
+
def inspect
|
162
|
+
"#<Gmail::Mailbox#{'0x%04x' % (object_id << 1)} name=#{name}>"
|
163
|
+
end
|
164
|
+
|
165
|
+
def to_s
|
166
|
+
name
|
167
|
+
end
|
168
|
+
|
169
|
+
MAILBOX_ALIASES.each_key { |mailbox|
|
170
|
+
define_method(mailbox) do |*args, &block|
|
171
|
+
emails(mailbox, *args, &block)
|
172
|
+
end
|
173
|
+
}
|
174
|
+
end # Message
|
175
|
+
end # Gmail
|