send_gmail 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/exe/send_gmail +3 -0
- data/lib/send_gmail/auth.rb +33 -0
- data/lib/send_gmail/mail_list.rb +59 -0
- data/lib/send_gmail/objects/mail.rb +7 -0
- data/lib/send_gmail/version.rb +3 -0
- data/lib/send_gmail.rb +19 -0
- data/send_gmail.gemspec +32 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ca446d249e7b0c0423fd098572531d215b356af7
|
4
|
+
data.tar.gz: 77966d29e42eb3d69cdac7a162f24ce8919b6588
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1286a65da3eaf19e1e165b67ee8a5ff1ea0fa4153e8a10a5c3543f9225e89792bdd06898e447ff82803d3649caaf13a5de8501135a89ff016453d651d7ec3c68
|
7
|
+
data.tar.gz: 6012b550893be514fa46fb1b5e061b39d903143e128bdf289928815e2fa7343711fd1852e9fcc1cdae7324963d024c6797b3a7cc2b588e1011da8e32b22e3c7e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 h,yusaku
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# SendGmail
|
2
|
+
|
3
|
+
send_gmail handles gmail.
|
4
|
+
now it features only the acquisition of e-mail. this gem is using gmail api.
|
5
|
+
https://developers.google.com/gmail/api/
|
6
|
+
|
7
|
+
## Feature
|
8
|
+
* mail sending
|
9
|
+
* create draft
|
10
|
+
* on/off label
|
11
|
+
* other...
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'send_gmail'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install send_gmail
|
28
|
+
|
29
|
+
## To begin with
|
30
|
+
```ruby
|
31
|
+
$ client = SendGmail::Client.new
|
32
|
+
$ client.authorize(credentials_path, client_id, client_secret, scope)
|
33
|
+
```
|
34
|
+
|
35
|
+
* credentials_path is save authentication information path
|
36
|
+
* client_id, client_sercret is https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
|
37
|
+
* scope is https://developers.google.com/gmail/api/auth/scopes
|
38
|
+
|
39
|
+
## Incoming mail
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
$ mail_id_list = client.mail_id_list(searching_option)
|
43
|
+
=> {
|
44
|
+
:mail_id_list=>["ffffff", "ccccc", ...],
|
45
|
+
:next_page_token=>"123456789"
|
46
|
+
}
|
47
|
+
|
48
|
+
$ mail_id_list[:mail_id_list].map { |mail_id| client.mail_detail(mail_id) }
|
49
|
+
=> [#<SendGmail::Objects::Mail... , ...]
|
50
|
+
```
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
$ mail_list = client.mail_list(searching_option)
|
54
|
+
=> {
|
55
|
+
:mail_id_list => [#<SendGmail::Objects::Mail... , ...],
|
56
|
+
:next_page_token => "123456789"
|
57
|
+
}
|
58
|
+
```
|
59
|
+
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hatappi/send_gmail.
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
68
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'send_gmail'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/exe/send_gmail
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'google/api_client/client_secrets'
|
2
|
+
require 'google/api_client/auth/installed_app'
|
3
|
+
require 'google/api_client/auth/storage'
|
4
|
+
require 'google/api_client/auth/storages/file_store'
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
module SendGmail
|
8
|
+
module Auth
|
9
|
+
def authorize(
|
10
|
+
credentials_path = '/tmp/client_secrets.json',
|
11
|
+
client_id = ENV['GOOGLE_CLIENT_ID'],
|
12
|
+
client_secret = ENV['GOOGLE_CLIENT_SECRET'],
|
13
|
+
scope = 'https://www.googleapis.com/auth/gmail.readonly'
|
14
|
+
)
|
15
|
+
|
16
|
+
FileUtils.mkdir_p(File.dirname(credentials_path))
|
17
|
+
|
18
|
+
file_store = Google::APIClient::FileStore.new(credentials_path)
|
19
|
+
storage = Google::APIClient::Storage.new(file_store)
|
20
|
+
auth = storage.authorize
|
21
|
+
|
22
|
+
if auth.nil? || (auth.expired? && auth.refresh_token.nil?)
|
23
|
+
flow = Google::APIClient::InstalledAppFlow.new(
|
24
|
+
client_id: client_id,
|
25
|
+
client_secret: client_secret,
|
26
|
+
scope: scope
|
27
|
+
)
|
28
|
+
auth = flow.authorize(storage)
|
29
|
+
end
|
30
|
+
@client.authorization = auth
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module SendGmail
|
2
|
+
module MailList
|
3
|
+
def mail_id_list(query, _user_id = 'me', next_page_token = nil)
|
4
|
+
parameters = { userId: 'me', q: query }
|
5
|
+
parameters[:pageToken] = next_page_token unless next_page_token.nil?
|
6
|
+
|
7
|
+
results = @client.execute!(
|
8
|
+
api_method: @gmail_api.users.messages.list,
|
9
|
+
parameters: parameters
|
10
|
+
)
|
11
|
+
|
12
|
+
{
|
13
|
+
mail_id_list: results.data.messages.map(&:id),
|
14
|
+
next_page_token: results.data.nextPageToken
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def mail_list(query, user_id = 'me', next_page_token = nil)
|
19
|
+
parameters = { userId: 'me', q: query }
|
20
|
+
parameters[:pageToken] = next_page_token unless next_page_token.nil?
|
21
|
+
|
22
|
+
results = @client.execute!(
|
23
|
+
api_method: @gmail_api.users.messages.list,
|
24
|
+
parameters: parameters
|
25
|
+
)
|
26
|
+
|
27
|
+
{
|
28
|
+
mail_list: results.data.messages.map { |m| mail_detail(m.id, user_id) },
|
29
|
+
next_page_token: results.data.nextPageToken
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def mail_detail(mail_id, user_id = 'me')
|
34
|
+
results = @client.execute!(
|
35
|
+
api_method: @gmail_api.users.messages.get,
|
36
|
+
parameters: { userId: user_id, id: mail_id }
|
37
|
+
)
|
38
|
+
obj = JSON.parse(results.response.body, symbolize_names: true)
|
39
|
+
base64url_data = obj[:payload][:body][:data]
|
40
|
+
base64url_data ||= obj[:payload][:parts].first[:body][:data]
|
41
|
+
base64url_data ||= obj[:payload][:parts].first[:parts].first[:body][:data]
|
42
|
+
headers = obj[:payload][:headers]
|
43
|
+
|
44
|
+
mail = SendGmail::Objects::Mail.new
|
45
|
+
mail.subject = pickup_contents(headers, 'Subject')
|
46
|
+
mail.from = pickup_contents(headers, 'From')
|
47
|
+
mail.date = Time.parse(pickup_contents(headers, 'Date'))
|
48
|
+
mail.body = Base64.urlsafe_decode64(base64url_data).force_encoding('UTF-8')
|
49
|
+
mail
|
50
|
+
end
|
51
|
+
|
52
|
+
def pickup_contents(headers, name)
|
53
|
+
content = headers.select { |e| e[:name] == name }
|
54
|
+
return nil if content.nil?
|
55
|
+
return nil if content.first.nil?
|
56
|
+
content.first[:value]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/send_gmail.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'google/api_client'
|
2
|
+
require 'send_gmail/version'
|
3
|
+
require 'send_gmail/auth'
|
4
|
+
require 'send_gmail/mail_list'
|
5
|
+
require 'send_gmail/objects/mail'
|
6
|
+
|
7
|
+
module SendGmail
|
8
|
+
class Client
|
9
|
+
include SendGmail::Auth
|
10
|
+
include SendGmail::MailList
|
11
|
+
|
12
|
+
attr_accessor :authorization, :client, :gmail_api
|
13
|
+
|
14
|
+
def initialize(app_name = 'gmail_test', app_ver = 'ver1.0')
|
15
|
+
@client = Google::APIClient.new(application_name: app_name, application_version: app_ver)
|
16
|
+
@gmail_api = client.discovered_api('gmail', 'v1')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/send_gmail.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'send_gmail/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'send_gmail'
|
8
|
+
spec.version = SendGmail::VERSION
|
9
|
+
spec.authors = ['hatappi']
|
10
|
+
spec.email = ['hata.yusaku.1225@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'incoming mail'
|
13
|
+
spec.description = 'incoming mail by google gmail api'
|
14
|
+
spec.homepage = 'https://github.com/hatappi/send_gmail'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
19
|
+
else
|
20
|
+
fail 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
spec.add_development_dependency 'rspec'
|
31
|
+
spec.add_development_dependency 'google-api-client'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: send_gmail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hatappi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: google-api-client
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: incoming mail by google gmail api
|
70
|
+
email:
|
71
|
+
- hata.yusaku.1225@gmail.com
|
72
|
+
executables:
|
73
|
+
- send_gmail
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- exe/send_gmail
|
88
|
+
- lib/send_gmail.rb
|
89
|
+
- lib/send_gmail/auth.rb
|
90
|
+
- lib/send_gmail/mail_list.rb
|
91
|
+
- lib/send_gmail/objects/mail.rb
|
92
|
+
- lib/send_gmail/version.rb
|
93
|
+
- send_gmail.gemspec
|
94
|
+
homepage: https://github.com/hatappi/send_gmail
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata:
|
98
|
+
allowed_push_host: https://rubygems.org
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.4.5.1
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: incoming mail
|
119
|
+
test_files: []
|