gmail-api-ruby 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.bundle/config +1 -0
- data/CHANGELOG.md +3 -0
- data/README.md +10 -4
- data/account.yml.example +17 -5
- data/gmail.gemspec +1 -1
- data/lib/gmail.rb +45 -8
- data/lib/gmail/message.rb +32 -11
- data/lib/gmail/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50da12b58316258876e0a9c5a67aecc25b9c23df
|
4
|
+
data.tar.gz: 939731da0a5f2f3b5ace2a0ebd5f202d7ef5058f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07cfb690f0db78ba1156bef9c78b1b0ca21d5b45c2636eb691591a23479b4aaadb65c5bba4b185ff1998d120397a86455d3c7c7f0493031c92e2070e559d2d40
|
7
|
+
data.tar.gz: fc60b494bf49812736249210d4cf22db71fb84c690c8762b409b8769aa2fd5cf947a50405345644fc713a20a08f06faad951a182a27b85b6f814b03cde82fa29
|
data/.bundle/config
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--- {}
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -29,15 +29,21 @@ If for your usecase, the gem is too limited there are two solutions for you:
|
|
29
29
|
## Initialization
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
Gmail.client_id = "...Your app client id..."
|
33
|
-
Gmail.client_secret = "...Your app Secret..."
|
34
|
-
Gmail.refresh_token = "...the refresh token of the Gmail account you want to use..."
|
32
|
+
Gmail.client_id = "...Your app client id..." or the email from your service account credentials
|
33
|
+
Gmail.client_secret = "...Your app Secret..." or the pem version of hte api key for service account
|
34
|
+
Gmail.refresh_token = "...the refresh token of the Gmail account you want to use..." or the account email address for service account auth
|
35
|
+
# Or for service Accounts
|
36
|
+
Gmail.auth_method = "service_account"
|
37
|
+
Gmail.client_id = "... or the email from your service account credentials..."
|
38
|
+
Gmail.client_secret = "...pem version of the api key ..."
|
39
|
+
Gmail.email_account = "...the email account you're going to use"
|
40
|
+
Gmail.auth_scopes = ['http://mail.google.com'] # an array of the permissions required.
|
35
41
|
```
|
36
42
|
|
37
43
|
## Usage
|
38
44
|
|
39
45
|
```ruby
|
40
|
-
require "
|
46
|
+
require "gmail"
|
41
47
|
```
|
42
48
|
|
43
49
|
### Common methods
|
data/account.yml.example
CHANGED
@@ -1,5 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
# Auth Method
|
2
|
+
google_api_auth_method: ENV['GOOGLE_API_AUTH_METHOD']
|
3
|
+
|
4
|
+
#App Info
|
5
|
+
application_name: ENV['APP_NAME']
|
6
|
+
application_version: ENV['APP_VERSION']
|
7
|
+
|
8
|
+
# Web Application Auth
|
9
|
+
client_id: ENV['GOOGLE_API_CLIENT_ID']
|
10
|
+
client_secret: ENV['GOOGLE_API_CLIENT_SECRET']
|
11
|
+
refresh_token: ENV['GOOGLE_API_REFRESH_TOKEN']
|
12
|
+
|
13
|
+
# Service Account Auth
|
14
|
+
google_api_key: ENV['GOOGLE_API_KEY']
|
15
|
+
google_api_email: ENV['GOOGLE_API_EMAIL']
|
16
|
+
email_account: ENV['GOOGLE_ACCOUNT']
|
17
|
+
auth_scopes: ENV['GOOGLE_AUTH_SCOPES']
|
data/gmail.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
|
|
12
12
|
"
|
13
13
|
s.version = Gmail::VERSION
|
14
14
|
s.platform = Gem::Platform::RUBY
|
15
|
-
s.authors = ["Julien Hobeika"]
|
15
|
+
s.authors = ["Julien Hobeika","Keiran Betteley"]
|
16
16
|
s.homepage = "http://github.com/jhk753/gmail-ruby-api"
|
17
17
|
s.licenses = ['MIT']
|
18
18
|
|
data/lib/gmail.rb
CHANGED
@@ -20,15 +20,14 @@ require 'gmail/draft'
|
|
20
20
|
require 'gmail/thread'
|
21
21
|
require 'gmail/label'
|
22
22
|
|
23
|
-
|
24
|
-
|
25
23
|
module Gmail
|
26
24
|
|
27
25
|
class << self
|
28
|
-
attr_accessor :
|
26
|
+
attr_accessor :auth_method, :client_id, :client_secret,
|
27
|
+
:refresh_token, :auth_scopes, :email_account, :application_name, :application_version
|
29
28
|
attr_reader :service, :client, :mailbox_email
|
30
29
|
def new hash
|
31
|
-
[:client_id, :client_secret, :refresh_token, :application_name, :application_version].each do |accessor|
|
30
|
+
[:auth_method, :client_id, :client_secret, :refresh_token, :auth_scopes, :email_account, :application_name, :application_version].each do |accessor|
|
32
31
|
Gmail.send("#{accessor}=", hash[accessor.to_s])
|
33
32
|
end
|
34
33
|
end
|
@@ -44,11 +43,23 @@ module Gmail
|
|
44
43
|
|
45
44
|
end
|
46
45
|
|
47
|
-
def self.request(method, params={}, body={})
|
46
|
+
def self.request(method, params={}, body={}, auth_method=@auth_method)
|
47
|
+
|
48
48
|
params[:userId] ||= "me"
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
case auth_method
|
50
|
+
when "web_application"
|
51
|
+
if @client.nil?
|
52
|
+
self.connect
|
53
|
+
end
|
54
|
+
when "service_account"
|
55
|
+
if @client.nil?
|
56
|
+
self.service_account_connect
|
57
|
+
elsif self.client.authorization.principal != @email_account
|
58
|
+
self.service_account_connect
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
52
63
|
if body.empty?
|
53
64
|
response = @client.execute(
|
54
65
|
:api_method => method,
|
@@ -71,6 +82,8 @@ module Gmail
|
|
71
82
|
@mailbox_email ||= self.request(@service.users.to_h['gmail.users.getProfile'])[:emailAddress]
|
72
83
|
end
|
73
84
|
|
85
|
+
|
86
|
+
|
74
87
|
def self.connect(client_id=@client_id, client_secret=@client_secret, refresh_token=@refresh_token)
|
75
88
|
unless client_id
|
76
89
|
raise 'No client_id specified'
|
@@ -99,6 +112,30 @@ module Gmail
|
|
99
112
|
|
100
113
|
end
|
101
114
|
|
115
|
+
def self.service_account_connect(
|
116
|
+
client_id=@client_id, client_secret=@client_secret,
|
117
|
+
email_account=@email_account, auth_scopes=@auth_scopes,
|
118
|
+
application_name=@application_name, application_version=@application_version
|
119
|
+
)
|
120
|
+
puts "Authenticating service account - #{email_account}"
|
121
|
+
|
122
|
+
|
123
|
+
@client = Google::APIClient.new(application_name: application_name, application_version: application_version)
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
key = Google::APIClient::KeyUtils.load_from_pem(
|
128
|
+
client_secret,
|
129
|
+
'notasecret')
|
130
|
+
asserter = Google::APIClient::JWTAsserter.new(
|
131
|
+
client_id,
|
132
|
+
auth_scopes,
|
133
|
+
key
|
134
|
+
)
|
135
|
+
@client.authorization = asserter.authorize(email_account)
|
136
|
+
|
137
|
+
end
|
138
|
+
|
102
139
|
def self.parse(response)
|
103
140
|
begin
|
104
141
|
|
data/lib/gmail/message.rb
CHANGED
@@ -108,16 +108,31 @@ module Gmail
|
|
108
108
|
msg.header['X-Bcc'] = bcc unless bcc.nil?#because Mail gem doesn't allow bcc headers...
|
109
109
|
msg.in_reply_to = in_reply_to unless in_reply_to.nil?
|
110
110
|
msg.references = references unless references.nil?
|
111
|
-
if text
|
112
|
-
|
113
|
-
|
114
|
-
|
111
|
+
if text || html
|
112
|
+
bodypart = Mail::Part.new
|
113
|
+
if text
|
114
|
+
bodypart.text_part = Mail::Part.new do |p|
|
115
|
+
content_type 'text/plain; charset=UTF-8'
|
116
|
+
p.body s.text
|
117
|
+
end
|
115
118
|
end
|
119
|
+
if html
|
120
|
+
bodypart.html_part = Mail::Part.new do |p|
|
121
|
+
content_type 'text/html; charset=UTF-8'
|
122
|
+
p.body s.html
|
123
|
+
end
|
124
|
+
end
|
125
|
+
msg.add_part bodypart
|
116
126
|
end
|
117
|
-
if
|
118
|
-
|
119
|
-
|
120
|
-
|
127
|
+
if attachments
|
128
|
+
if attachments.is_a?(Hash)
|
129
|
+
attachments.each do |name, attachment|
|
130
|
+
msg.add_file filename: name, content: attachment
|
131
|
+
end
|
132
|
+
elsif attachments.is_a?(Array)
|
133
|
+
attachments.each do |attachment|
|
134
|
+
msg.add_file(attachment)
|
135
|
+
end
|
121
136
|
end
|
122
137
|
end
|
123
138
|
Base64.urlsafe_encode64 msg.to_s.sub("X-Bcc", "Bcc") #because Mail gem doesn't allow bcc headers...
|
@@ -137,13 +152,17 @@ module Gmail
|
|
137
152
|
msg
|
138
153
|
end
|
139
154
|
|
155
|
+
def self.find_addresses str
|
156
|
+
Mail::AddressList.new("#{str}".to_ascii.gsub(/<(<(.)*@(.)*>)(.)*>/, '\1'))
|
157
|
+
end
|
158
|
+
|
140
159
|
def set_headers_for_reply msg
|
141
160
|
#to_ar = []
|
142
161
|
#split_regexp = Regexp.new("\s*,\s*")
|
143
162
|
own_email = delivered_to || Gmail.mailbox_email
|
144
163
|
|
145
164
|
|
146
|
-
to_ar = (
|
165
|
+
to_ar = (Message.find_addresses(to).addresses + Message.find_addresses(cc).addresses).map(&:to_s)
|
147
166
|
#to_ar = (to || "").split(split_regexp) + (cc || "").split(split_regexp)
|
148
167
|
result = to_ar.grep(Regexp.new(own_email, "i"))
|
149
168
|
to_ar = to_ar - result
|
@@ -186,11 +205,13 @@ module Gmail
|
|
186
205
|
end
|
187
206
|
|
188
207
|
if payload.parts
|
189
|
-
|
208
|
+
content_payload = @values.payload.find_all_object_containing("mimeType", "multipart/alternative").first
|
209
|
+
content_payload ||= @values.payload
|
210
|
+
text_part=content_payload.find_all_object_containing("mimeType", "text/plain").first
|
190
211
|
if text_part
|
191
212
|
@values.text = urlsafe_decode64(text_part.body.data)
|
192
213
|
end
|
193
|
-
html_part
|
214
|
+
html_part=content_payload.find_all_object_containing("mimeType", "text/html").first
|
194
215
|
if html_part
|
195
216
|
@values.html = urlsafe_decode64(html_part.body.data)
|
196
217
|
end
|
data/lib/gmail/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gmail-api-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julien Hobeika
|
8
|
+
- Keiran Betteley
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2015-
|
12
|
+
date: 2015-09-22 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: mime
|
@@ -173,6 +174,7 @@ executables: []
|
|
173
174
|
extensions: []
|
174
175
|
extra_rdoc_files: []
|
175
176
|
files:
|
177
|
+
- ".bundle/config"
|
176
178
|
- ".gitignore"
|
177
179
|
- ".travis.yml"
|
178
180
|
- CHANGELOG.md
|
@@ -181,7 +183,6 @@ files:
|
|
181
183
|
- README.md
|
182
184
|
- Rakefile
|
183
185
|
- TODO.md
|
184
|
-
- account.yml
|
185
186
|
- account.yml.example
|
186
187
|
- gmail.gemspec
|
187
188
|
- lib/gmail.rb
|