gmail 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +13 -0
- data/.rubocop_todo.yml +239 -0
- data/.travis.yml +19 -0
- data/CHANGELOG.md +75 -18
- data/Gemfile +1 -1
- data/README.md +214 -131
- data/Rakefile +11 -0
- data/gmail.gemspec +8 -8
- data/lib/gmail.rb +31 -18
- data/lib/gmail/client.rb +11 -7
- data/lib/gmail/client/base.rb +52 -48
- data/lib/gmail/client/plain.rb +9 -5
- data/lib/gmail/client/xoauth.rb +23 -6
- data/lib/gmail/client/xoauth2.rb +39 -0
- data/lib/gmail/imap_extensions.rb +150 -0
- data/lib/gmail/labels.rb +33 -16
- data/lib/gmail/mailbox.rb +60 -42
- data/lib/gmail/message.rb +142 -101
- data/lib/gmail/version.rb +2 -2
- data/spec/account.yml.obfus +2 -0
- data/spec/gmail/client/base_spec.rb +5 -0
- data/spec/gmail/client/plain_spec.rb +168 -0
- data/spec/gmail/client/xoauth2_spec.rb +186 -0
- data/spec/gmail/client/xoauth_spec.rb +5 -0
- data/spec/gmail/client_spec.rb +5 -0
- data/spec/gmail/imap_extensions_spec.rb +12 -0
- data/spec/gmail/labels_spec.rb +18 -0
- data/spec/{mailbox_spec.rb → gmail/mailbox_spec.rb} +14 -14
- data/spec/gmail/message_spec.rb +181 -0
- data/spec/gmail_spec.rb +21 -21
- data/spec/recordings/gmail/_new_connects_with_client_and_give_it_context_when_block_given.yml +28 -0
- data/spec/recordings/gmail/_new_connects_with_gmail_service_and_return_valid_connection_object.yml +28 -0
- data/spec/recordings/gmail/_new_does_not_raise_error_when_couldn_t_connect_with_given_account.yml +13 -0
- data/spec/recordings/gmail/_new_raises_error_when_couldn_t_connect_with_given_account.yml +13 -0
- data/spec/recordings/gmail_client_plain/instance/_connection_automatically_logs_in_to_gmail_account_when_it_s_called.yml +42 -0
- data/spec/recordings/gmail_client_plain/instance/delivers_inline_composed_email.yml +42 -0
- data/spec/recordings/gmail_client_plain/instance/does_not_log_in_when_given_gmail_account_is_invalid.yml +13 -0
- data/spec/recordings/gmail_client_plain/instance/does_not_raise_error_even_though_gmail_account_is_invalid.yml +13 -0
- data/spec/recordings/gmail_client_plain/instance/labels/checks_if_there_is_given_label_defined.yml +196 -0
- data/spec/recordings/gmail_client_plain/instance/labels/creates_given_label.yml +151 -0
- data/spec/recordings/gmail_client_plain/instance/labels/removes_existing_label.yml +146 -0
- data/spec/recordings/gmail_client_plain/instance/labels/returns_list_of_all_available_labels.yml +113 -0
- data/spec/recordings/gmail_client_plain/instance/properly_logs_in_to_valid_gmail_account.yml +42 -0
- data/spec/recordings/gmail_client_plain/instance/properly_logs_out_from_gmail.yml +42 -0
- data/spec/recordings/gmail_client_plain/instance/properly_switches_to_given_mailbox.yml +109 -0
- data/spec/recordings/gmail_client_plain/instance/properly_switches_to_given_mailbox_using_block_style.yml +109 -0
- data/spec/recordings/gmail_client_plain/instance/raises_error_when_given_gmail_account_is_invalid_and_errors_enabled.yml +13 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/does_not_log_in_when_given_gmail_account_is_invalid.yml +13 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/checks_if_there_is_given_label_defined.yml +27 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/creates_given_label.yml +39 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/removes_existing_label.yml +39 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/labels/returns_list_of_all_available_labels.yml +27 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_logs_in_to_valid_gmail_account.yml +15 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_logs_out_from_gmail.yml +15 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_switches_to_given_mailbox.yml +40 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/properly_switches_to_given_mailbox_using_block_style.yml +40 -0
- data/spec/recordings/gmail_client_xo_auth2/instance/raises_error_when_given_gmail_account_is_invalid_and_errors_enabled.yml +13 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/all/localizes_into_the_appropriate_label.yml +116 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/drafts/localizes_into_the_appropriate_label.yml +116 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/flagged/localizes_into_the_appropriate_label.yml +116 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/important/localizes_into_the_appropriate_label.yml +116 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/inbox/localizes_into_the_appropriate_label.yml +42 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/junk/localizes_into_the_appropriate_label.yml +116 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/sent/localizes_into_the_appropriate_label.yml +116 -0
- data/spec/recordings/gmail_labels/localize/when_given_the_xl_is_tflag/trash/localizes_into_the_appropriate_label.yml +116 -0
- data/spec/recordings/gmail_mailbox/instance/counts_all_emails.yml +277 -0
- data/spec/recordings/gmail_mailbox/instance/finds_messages.yml +586 -0
- data/spec/recordings/gmail_mailbox/on_initialize/sets_client_and_name.yml +42 -0
- data/spec/recordings/gmail_mailbox/on_initialize/works_in_inbox_by_default.yml +42 -0
- data/spec/recordings/gmail_message/initialize/sets_prefetch_attrs.yml +578 -0
- data/spec/recordings/gmail_message/initialize/sets_uid_and_mailbox.yml +580 -0
- data/spec/recordings/gmail_message/instance_methods/deletes_itself.yml +637 -0
- data/spec/recordings/gmail_message/instance_methods/marks_itself_read.yml +682 -0
- data/spec/recordings/gmail_message/instance_methods/marks_itself_unread.yml +686 -0
- data/spec/recordings/gmail_message/instance_methods/moves_from_one_tag_to_other.yml +862 -0
- data/spec/recordings/gmail_message/instance_methods/removes_a_given_label.yml +776 -0
- data/spec/recordings/gmail_message/instance_methods/removes_a_given_label_with_old_method.yml +776 -0
- data/spec/recordings/gmail_message/instance_methods/sets_given_label.yml +690 -0
- data/spec/recordings/gmail_message/instance_methods/sets_given_label_with_old_method.yml +691 -0
- data/spec/spec_helper.rb +34 -10
- data/spec/support/imap_mock.rb +129 -0
- data/spec/support/obfuscation.rb +52 -0
- metadata +79 -30
- data/TODO.md +0 -12
- data/lib/gmail/client/imap_extensions.rb +0 -54
- data/spec/client_spec.rb +0 -178
- data/spec/message_spec.rb +0 -51
data/Rakefile
CHANGED
@@ -33,3 +33,14 @@ rescue LoadError
|
|
33
33
|
end
|
34
34
|
|
35
35
|
task :default => :spec
|
36
|
+
|
37
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
38
|
+
require 'spec/support/obfuscation'
|
39
|
+
desc 'Obfuscates account.yml file.'
|
40
|
+
task :obfuscate do
|
41
|
+
Spec::Obfuscation.encrypt_file(File.join(File.dirname(__FILE__), 'spec', 'account.yml'))
|
42
|
+
end
|
43
|
+
|
44
|
+
task :deobfuscate do
|
45
|
+
puts Spec::Obfuscation.decrypt_file(File.join(File.dirname(__FILE__), 'spec', 'account.yml.obfus'))
|
46
|
+
end
|
data/gmail.gemspec
CHANGED
@@ -14,21 +14,21 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.platform = Gem::Platform::RUBY
|
15
15
|
s.authors = ["Chris Kowalik"]
|
16
16
|
s.email = ["chris@nu7hat.ch"]
|
17
|
-
s.homepage = "http://github.com/
|
18
|
-
|
17
|
+
s.homepage = "http://github.com/gmailgem/gmail"
|
18
|
+
s.licenses = ["MIT"]
|
19
|
+
|
19
20
|
# runtime dependencies
|
20
|
-
s.add_dependency "mime", ">= 0.1"
|
21
21
|
s.add_dependency "mail", ">= 2.2.1"
|
22
22
|
s.add_dependency "gmail_xoauth", ">= 0.3.0"
|
23
|
-
|
23
|
+
|
24
24
|
# development dependencies
|
25
25
|
s.add_development_dependency "rake"
|
26
|
-
s.add_development_dependency "rspec", "
|
27
|
-
s.add_development_dependency "
|
26
|
+
s.add_development_dependency "rspec", ">= 3.1"
|
27
|
+
s.add_development_dependency "rubocop"
|
28
28
|
s.add_development_dependency "gem-release"
|
29
|
-
|
29
|
+
|
30
30
|
s.files = `git ls-files`.split("\n")
|
31
31
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
32
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
32
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
33
33
|
s.require_paths = ["lib"]
|
34
34
|
end
|
data/lib/gmail.rb
CHANGED
@@ -10,7 +10,8 @@ end
|
|
10
10
|
|
11
11
|
class Object
|
12
12
|
def to_imap_date
|
13
|
-
|
13
|
+
date = respond_to?(:utc) ? utc.to_s : to_s
|
14
|
+
Date.parse(date).strftime("%d-%B-%Y")
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
@@ -27,7 +28,7 @@ module Gmail
|
|
27
28
|
# ==== Examples
|
28
29
|
#
|
29
30
|
# Gmail.new(:plain, "foo@gmail.com", "password")
|
30
|
-
# Gmail.new(:xoauth, "foo@gmail.com",
|
31
|
+
# Gmail.new(:xoauth, "foo@gmail.com",
|
31
32
|
# :consumer_key => "",
|
32
33
|
# :consumer_secret => "",
|
33
34
|
# :token => "",
|
@@ -44,22 +45,34 @@ module Gmail
|
|
44
45
|
# end
|
45
46
|
#
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
#
|
53
|
-
if block_given? # if block_given?
|
54
|
-
yield client # yield client
|
55
|
-
client.logout # client.logout
|
56
|
-
end # end
|
57
|
-
#
|
58
|
-
client # client
|
59
|
-
end # end
|
60
|
-
}
|
48
|
+
def new(*args, &block)
|
49
|
+
args.unshift(:plain) unless args.first.is_a?(Symbol)
|
50
|
+
client = Gmail::Client.new(*args)
|
51
|
+
client.connect
|
52
|
+
client.login
|
61
53
|
|
62
|
-
|
63
|
-
|
54
|
+
if block_given?
|
55
|
+
yield client
|
56
|
+
client.logout
|
57
|
+
end
|
58
|
+
|
59
|
+
client
|
60
|
+
end
|
61
|
+
alias_method :connect, :new
|
62
|
+
|
63
|
+
def new!(*args, &block)
|
64
|
+
args.unshift(:plain) unless args.first.is_a?(Symbol)
|
65
|
+
client = Gmail::Client.new(*args)
|
66
|
+
client.connect!
|
67
|
+
client.login!
|
68
|
+
|
69
|
+
if block_given?
|
70
|
+
yield client
|
71
|
+
client.logout
|
72
|
+
end
|
73
|
+
|
74
|
+
client
|
75
|
+
end
|
76
|
+
alias_method :connect!, :new!
|
64
77
|
end # << self
|
65
78
|
end # Gmail
|
data/lib/gmail/client.rb
CHANGED
@@ -1,30 +1,34 @@
|
|
1
1
|
module Gmail
|
2
2
|
module Client
|
3
|
-
# Raised when connection with
|
3
|
+
# Raised when connection with Gmail IMAP service couldn't be established.
|
4
4
|
class ConnectionError < SocketError; end
|
5
5
|
# Raised when given username or password are invalid.
|
6
6
|
class AuthorizationError < Net::IMAP::NoResponseError; end
|
7
|
-
# Raised when delivered email is invalid.
|
7
|
+
# Raised when delivered email is invalid.
|
8
8
|
class DeliveryError < ArgumentError; end
|
9
9
|
# Raised when given client is not registered
|
10
10
|
class UnknownClient < ArgumentError; end
|
11
11
|
|
12
|
-
def self.
|
12
|
+
def self.clients
|
13
13
|
@clients ||= {}
|
14
|
-
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.register(name, klass)
|
17
|
+
clients[name] = klass
|
15
18
|
end
|
16
19
|
|
17
20
|
def self.new(name, *args)
|
18
|
-
if client =
|
21
|
+
if client = clients[name]
|
19
22
|
client.new(*args)
|
20
23
|
else
|
21
|
-
raise UnknownClient, "No such client: #{name}"
|
24
|
+
raise UnknownClient, "No such client: #{name}"
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
|
-
require 'gmail/
|
28
|
+
require 'gmail/imap_extensions'
|
26
29
|
require 'gmail/client/base'
|
27
30
|
require 'gmail/client/plain'
|
28
31
|
require 'gmail/client/xoauth'
|
32
|
+
require 'gmail/client/xoauth2'
|
29
33
|
end # Client
|
30
34
|
end # Gmail
|
data/lib/gmail/client/base.rb
CHANGED
@@ -3,89 +3,94 @@ require 'thread'
|
|
3
3
|
module Gmail
|
4
4
|
module Client
|
5
5
|
class Base
|
6
|
-
#
|
6
|
+
# Gmail IMAP defaults
|
7
7
|
GMAIL_IMAP_HOST = 'imap.gmail.com'
|
8
8
|
GMAIL_IMAP_PORT = 993
|
9
|
-
|
10
|
-
#
|
9
|
+
|
10
|
+
# Gmail SMTP defaults
|
11
11
|
GMAIL_SMTP_HOST = "smtp.gmail.com"
|
12
12
|
GMAIL_SMTP_PORT = 587
|
13
|
-
|
13
|
+
|
14
14
|
attr_reader :username
|
15
15
|
attr_reader :options
|
16
|
-
|
17
|
-
def initialize(username, options={})
|
16
|
+
|
17
|
+
def initialize(username, options = {})
|
18
18
|
defaults = {}
|
19
19
|
@username = fill_username(username)
|
20
20
|
@options = defaults.merge(options)
|
21
21
|
@mailbox_mutex = Mutex.new
|
22
22
|
end
|
23
|
-
|
24
|
-
# Connect to gmail service.
|
25
|
-
def connect(raise_errors=false)
|
23
|
+
|
24
|
+
# Connect to gmail service.
|
25
|
+
def connect(raise_errors = false)
|
26
26
|
@imap = Net::IMAP.new(GMAIL_IMAP_HOST, GMAIL_IMAP_PORT, true, nil, false)
|
27
|
-
|
27
|
+
Gmail::ImapExtensions.patch_net_imap_response_parser
|
28
28
|
@imap
|
29
29
|
rescue SocketError
|
30
|
-
raise_errors and raise ConnectionError, "Couldn't establish connection with
|
30
|
+
raise_errors and raise ConnectionError, "Couldn't establish connection with Gmail IMAP service"
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
# This version of connect will raise error on failure...
|
34
34
|
def connect!
|
35
35
|
connect(true)
|
36
36
|
end
|
37
|
-
|
38
|
-
# Return current connection. Log in automaticaly to specified account if
|
37
|
+
|
38
|
+
# Return current connection. Log in automaticaly to specified account if
|
39
39
|
# it is necessary.
|
40
40
|
def connection
|
41
41
|
login and at_exit { logout } unless logged_in?
|
42
42
|
@imap
|
43
43
|
end
|
44
44
|
alias :conn :connection
|
45
|
-
|
45
|
+
|
46
46
|
# Login to specified account.
|
47
47
|
def login(*args)
|
48
48
|
raise NotImplementedError, "The `#{self.class.name}#login` method is not implemented."
|
49
49
|
end
|
50
50
|
alias :sign_in :login
|
51
|
-
|
51
|
+
|
52
52
|
# This version of login will raise error on failure...
|
53
53
|
def login!
|
54
54
|
login(true)
|
55
55
|
end
|
56
56
|
alias :sign_in! :login!
|
57
|
-
|
57
|
+
|
58
58
|
# Returns +true+ when you are logged in to specified account.
|
59
59
|
def logged_in?
|
60
60
|
!!@logged_in
|
61
61
|
end
|
62
62
|
alias :signed_in? :logged_in?
|
63
|
-
|
64
|
-
# Logout from
|
63
|
+
|
64
|
+
# Logout from Gmail service.
|
65
65
|
def logout
|
66
66
|
@imap && logged_in? and @imap.logout
|
67
67
|
ensure
|
68
68
|
@logged_in = false
|
69
69
|
end
|
70
70
|
alias :sign_out :logout
|
71
|
-
|
72
|
-
#
|
71
|
+
|
72
|
+
# Disconnect from Gmail service.
|
73
|
+
def disconnect
|
74
|
+
@imap && @imap.disconnect
|
75
|
+
end
|
76
|
+
|
77
|
+
# Return labels object, which helps you with managing your Gmail labels.
|
73
78
|
# See <tt>Gmail::Labels</tt> for details.
|
74
79
|
def labels
|
75
80
|
@labels ||= Labels.new(conn)
|
76
81
|
end
|
77
|
-
|
82
|
+
|
78
83
|
# Compose new e-mail.
|
79
84
|
#
|
80
85
|
# ==== Examples
|
81
|
-
#
|
86
|
+
#
|
82
87
|
# mail = gmail.compose
|
83
88
|
# mail.from "test@gmail.org"
|
84
89
|
# mail.to "friend@gmail.com"
|
85
90
|
#
|
86
91
|
# ... or block style:
|
87
|
-
#
|
88
|
-
# mail = gmail.compose do
|
92
|
+
#
|
93
|
+
# mail = gmail.compose do
|
89
94
|
# from "test@gmail.org"
|
90
95
|
# to "friend@gmail.com"
|
91
96
|
# subject "Hello!"
|
@@ -95,24 +100,24 @@ module Gmail
|
|
95
100
|
# Now you can deliver your mail:
|
96
101
|
#
|
97
102
|
# gmail.deliver(mail)
|
98
|
-
def compose(mail=nil, &block)
|
103
|
+
def compose(mail = nil, &block)
|
99
104
|
if block_given?
|
100
105
|
mail = Mail.new(&block)
|
101
|
-
elsif !mail
|
106
|
+
elsif !mail
|
102
107
|
mail = Mail.new
|
103
|
-
end
|
108
|
+
end
|
104
109
|
|
105
110
|
mail.delivery_method(*smtp_settings)
|
106
111
|
mail.from = username unless mail.from
|
107
112
|
mail
|
108
113
|
end
|
109
114
|
alias :message :compose
|
110
|
-
|
111
|
-
# Compose (optionaly) and send given email.
|
115
|
+
|
116
|
+
# Compose (optionaly) and send given email.
|
112
117
|
#
|
113
118
|
# ==== Examples
|
114
119
|
#
|
115
|
-
# gmail.deliver do
|
120
|
+
# gmail.deliver do
|
116
121
|
# to "friend@gmail.com"
|
117
122
|
# subject "Hello friend!"
|
118
123
|
# body "Hi! How are you?"
|
@@ -125,19 +130,19 @@ module Gmail
|
|
125
130
|
#
|
126
131
|
# mail = gmail.compose { ... }
|
127
132
|
# gmail.deliver(mail)
|
128
|
-
def deliver(mail=nil, raise_errors=false, &block)
|
133
|
+
def deliver(mail = nil, raise_errors = false, &block)
|
129
134
|
mail = compose(mail, &block) if block_given?
|
130
135
|
mail.deliver!
|
131
136
|
rescue Object => ex
|
132
137
|
raise_errors and raise DeliveryError, "Couldn't deliver email: #{ex.to_s}"
|
133
138
|
end
|
134
|
-
|
139
|
+
|
135
140
|
# This version of deliver will raise error on failure...
|
136
|
-
def deliver!(mail=nil, &block)
|
141
|
+
def deliver!(mail = nil, &block)
|
137
142
|
deliver(mail, true, &block)
|
138
143
|
end
|
139
|
-
|
140
|
-
# Do something with given mailbox or within it context.
|
144
|
+
|
145
|
+
# Do something with given mailbox or within it context.
|
141
146
|
#
|
142
147
|
# ==== Examples
|
143
148
|
#
|
@@ -154,9 +159,9 @@ module Gmail
|
|
154
159
|
# end
|
155
160
|
def mailbox(name, &block)
|
156
161
|
@mailbox_mutex.synchronize do
|
157
|
-
name = name
|
162
|
+
name = labels.localize(name)
|
158
163
|
mailbox = (mailboxes[name] ||= Mailbox.new(self, name))
|
159
|
-
switch_to_mailbox(
|
164
|
+
switch_to_mailbox(mailbox) if @current_mailbox != mailbox
|
160
165
|
|
161
166
|
if block_given?
|
162
167
|
mailbox_stack << @current_mailbox
|
@@ -172,21 +177,21 @@ module Gmail
|
|
172
177
|
alias :in_mailbox :mailbox
|
173
178
|
alias :in_label :mailbox
|
174
179
|
alias :label :mailbox
|
175
|
-
|
180
|
+
|
176
181
|
# Alias for <tt>mailbox("INBOX")</tt>. See <tt>Gmail::Client#mailbox</tt>
|
177
182
|
# for details.
|
178
183
|
def inbox
|
179
184
|
mailbox("INBOX")
|
180
185
|
end
|
181
|
-
|
186
|
+
|
182
187
|
def mailboxes
|
183
188
|
@mailboxes ||= {}
|
184
189
|
end
|
185
|
-
|
190
|
+
|
186
191
|
def inspect
|
187
192
|
"#<Gmail::Client#{'0x%04x' % (object_id << 1)} (#{username}) #{'dis' if !logged_in?}connected>"
|
188
193
|
end
|
189
|
-
|
194
|
+
|
190
195
|
def fill_username(username)
|
191
196
|
username =~ /@/ ? username : "#{username}@gmail.com"
|
192
197
|
end
|
@@ -194,21 +199,20 @@ module Gmail
|
|
194
199
|
def mail_domain
|
195
200
|
username.split('@').last
|
196
201
|
end
|
197
|
-
|
202
|
+
|
198
203
|
private
|
199
|
-
|
204
|
+
|
200
205
|
def switch_to_mailbox(mailbox)
|
201
206
|
if mailbox
|
202
|
-
|
203
|
-
conn.select(mailbox)
|
207
|
+
conn.select(mailbox.encoded_name)
|
204
208
|
end
|
205
209
|
@current_mailbox = mailbox
|
206
210
|
end
|
207
|
-
|
211
|
+
|
208
212
|
def mailbox_stack
|
209
213
|
@mailbox_stack ||= []
|
210
214
|
end
|
211
|
-
|
215
|
+
|
212
216
|
def smtp_settings
|
213
217
|
[:smtp, {
|
214
218
|
:address => GMAIL_SMTP_HOST,
|
data/lib/gmail/client/plain.rb
CHANGED
@@ -2,16 +2,20 @@ module Gmail
|
|
2
2
|
module Client
|
3
3
|
class Plain < Base
|
4
4
|
attr_reader :password
|
5
|
-
|
6
|
-
def initialize(username, password, options={})
|
5
|
+
|
6
|
+
def initialize(username, password, options = {})
|
7
7
|
@password = password
|
8
8
|
super(username, options)
|
9
9
|
end
|
10
10
|
|
11
|
-
def login(raise_errors=false)
|
11
|
+
def login(raise_errors = false)
|
12
12
|
@imap and @logged_in = (login = @imap.login(username, password)) && login.name == 'OK'
|
13
|
-
rescue Net::IMAP::NoResponseError
|
14
|
-
raise_errors
|
13
|
+
rescue Net::IMAP::NoResponseError => e
|
14
|
+
if raise_errors
|
15
|
+
message = "Couldn't login to given Gmail account: #{username}"
|
16
|
+
message += " (#{e.response.data.text.strip})"
|
17
|
+
raise(AuthorizationError.new(e.response), message, e.backtrace)
|
18
|
+
end
|
15
19
|
end
|
16
20
|
end # Plain
|
17
21
|
|
data/lib/gmail/client/xoauth.rb
CHANGED
@@ -8,24 +8,40 @@ module Gmail
|
|
8
8
|
attr_reader :consumer_key
|
9
9
|
attr_reader :consumer_secret
|
10
10
|
|
11
|
-
def initialize(username, options={})
|
11
|
+
def initialize(username, options = {})
|
12
12
|
@token = options.delete(:token)
|
13
13
|
@secret = options.delete(:secret)
|
14
14
|
@consumer_key = options.delete(:consumer_key)
|
15
15
|
@consumer_secret = options.delete(:consumer_secret)
|
16
|
-
|
16
|
+
|
17
17
|
super(username, options)
|
18
18
|
end
|
19
19
|
|
20
|
-
def login(raise_errors=false)
|
20
|
+
def login(raise_errors = false)
|
21
21
|
@imap and @logged_in = (login = @imap.authenticate('XOAUTH', username,
|
22
22
|
:consumer_key => consumer_key,
|
23
23
|
:consumer_secret => consumer_secret,
|
24
24
|
:token => token,
|
25
25
|
:token_secret => secret
|
26
26
|
)) && login.name == 'OK'
|
27
|
-
rescue
|
28
|
-
raise_errors
|
27
|
+
rescue Net::IMAP::NoResponseError => e
|
28
|
+
if raise_errors
|
29
|
+
message = "Couldn't login to given Gmail account: #{username}"
|
30
|
+
message += " (#{e.response.data.text.strip})"
|
31
|
+
raise(AuthorizationError.new(e.response), message, e.backtrace)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def access_token
|
36
|
+
consumer_options = {
|
37
|
+
:site => "https://www.google.com",
|
38
|
+
:request_token_path => "/accounts/OAuthGetRequestToken",
|
39
|
+
:authorize_path => "/accounts/OAuthAuthorizeToken",
|
40
|
+
:access_token_path => "/accounts/OAuthGetAccessToken"
|
41
|
+
}
|
42
|
+
consumer = OAuth::Consumer.new(consumer_key, consumer_secret, consumer_options)
|
43
|
+
@access_token ||= OAuth::AccessToken.new(consumer, token, secret)
|
44
|
+
@access_token
|
29
45
|
end
|
30
46
|
|
31
47
|
def smtp_settings
|
@@ -38,7 +54,8 @@ module Gmail
|
|
38
54
|
:consumer_key => consumer_key,
|
39
55
|
:consumer_secret => consumer_secret,
|
40
56
|
:token => token,
|
41
|
-
:token_secret => secret
|
57
|
+
:token_secret => secret,
|
58
|
+
:access_token => access_token
|
42
59
|
},
|
43
60
|
:authentication => :xoauth,
|
44
61
|
:enable_starttls_auto => true
|