gmailish 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +20 -0
- data/.simplecov +19 -0
- data/.travis.yml +8 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +89 -0
- data/Rakefile +1 -0
- data/gmailish.gemspec +29 -0
- data/lib/gmailish.rb +7 -0
- data/lib/gmailish/account.rb +41 -0
- data/lib/gmailish/actions.rb +53 -0
- data/lib/gmailish/all.rb +7 -0
- data/lib/gmailish/error.rb +6 -0
- data/lib/gmailish/flagger.rb +27 -0
- data/lib/gmailish/labeler.rb +29 -0
- data/lib/gmailish/message.rb +42 -0
- data/lib/gmailish/mover.rb +25 -0
- data/lib/gmailish/version.rb +3 -0
- data/spec/gmailish_spec.rb +135 -0
- data/spec/lib/gmailish/account_spec.rb +89 -0
- data/spec/lib/gmailish/actions_spec.rb +116 -0
- data/spec/lib/gmailish/error_spec.rb +19 -0
- data/spec/lib/gmailish/flagger_spec.rb +45 -0
- data/spec/lib/gmailish/labeler_spec.rb +75 -0
- data/spec/lib/gmailish/message_spec.rb +146 -0
- data/spec/lib/gmailish/mover_spec.rb +49 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/support/data.rb +7 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f2157ddb5105b3b038cac8f2de54fa826889fdb2
|
4
|
+
data.tar.gz: 48fc9e5d02eeaa557ea49c79be59e4572fbfedf1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d241af1c987e449f01e020f4cf07e71757abda3ed7d1c8bc84a1775f4369653a803597ed12a03e0b358bb64ba88776c23a8077afb509fd881551d517989bc55a
|
7
|
+
data.tar.gz: 5a4f6b5e6c69004c8cac7ff0cdb760f31e88b874a58b7269ef25348f5ddc2180f0dfe010eca7aadb4d2ef387323fc76f5f632f218a3d53ace51a96d21002d0bd
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.simplecov
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'coveralls'
|
2
|
+
require 'simplecov-vim/formatter'
|
3
|
+
|
4
|
+
SimpleCov.configure do
|
5
|
+
minimum_coverage 95
|
6
|
+
maximum_coverage_drop 5
|
7
|
+
|
8
|
+
start('rails') do
|
9
|
+
formatter SimpleCov::Formatter::MultiFormatter[
|
10
|
+
SimpleCov::Formatter::HTMLFormatter,
|
11
|
+
Coveralls::SimpleCov::Formatter,
|
12
|
+
SimpleCov::Formatter::VimFormatter
|
13
|
+
]
|
14
|
+
|
15
|
+
add_group "Long files" do |src_file|
|
16
|
+
src_file.lines.count > 100
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Charles J Hardy
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/ChuckJHardy/Gmailish.png)](https://travis-ci.org/ChuckJHardy/Gmailish) [![Coverage Status](https://coveralls.io/repos/ChuckJHardy/Gmailish/badge.png?branch=master)](https://coveralls.io/r/ChuckJHardy/Gmailish) [![Gem Version](https://badge.fury.io/rb/gmailish.png)](https://rubygems.org/gems/gmailish)
|
2
|
+
|
3
|
+
# Gmailish
|
4
|
+
|
5
|
+
Grabs unread emails from Gmail, marks them as read, applies a `Transfered` label and archives them.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'gmailish', '~> 0.0.1'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gmailish
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
See Mail documentation [here](http://github.com/mikel/mail).
|
24
|
+
|
25
|
+
account = Gmailish::Account.new(username, password)
|
26
|
+
#=> #<Gmailish::Account:0x007f89dcaa7950>
|
27
|
+
|
28
|
+
account.process
|
29
|
+
#=> #<Gmailish::Account:0x007ff175d69ed0>
|
30
|
+
|
31
|
+
messages = account.messages
|
32
|
+
#=> [#<Mail::Message:70337520506800>, #<Mail::Message:70337520506801>]
|
33
|
+
|
34
|
+
message = messages.first
|
35
|
+
|
36
|
+
message.envelope.from #=> 'chuckjhardy@gmail.com'
|
37
|
+
message.from.addresses #=> ['chuckjhardy@gmail.com', 'venkmanapp@gmail.com']
|
38
|
+
message.sender.address #=> 'chuckjhardy@gmail.com'
|
39
|
+
message.to #=> 'chuckjhardy@gmail.com'
|
40
|
+
message.cc #=> 'chuckjhardy@gmail.com'
|
41
|
+
message.subject #=> "This is the subject"
|
42
|
+
message.date.to_s #=> '15 Aug 2013 09:55:06 -1100'
|
43
|
+
message.message_id #=> '<4D6AA7GB.8170198@xxx.xxx>'
|
44
|
+
message.body.decoded #=> 'This is the body of the email..
|
45
|
+
|
46
|
+
## Example
|
47
|
+
|
48
|
+
GmailService.retrieve
|
49
|
+
#=> [#<Mail::Message:70337520506800>, #<Mail::Message:70337520506801>]
|
50
|
+
|
51
|
+
class GmailService
|
52
|
+
def self.retrieve
|
53
|
+
new.messages
|
54
|
+
end
|
55
|
+
|
56
|
+
def messages
|
57
|
+
@messages ||= mailer.messages
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def mailer
|
63
|
+
Gmailish::Account.new(username, password).tap do |account|
|
64
|
+
account.process
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def username
|
69
|
+
ENV['GMAIL_EMAIL']
|
70
|
+
end
|
71
|
+
|
72
|
+
def password
|
73
|
+
ENV['GMAIL_PASSWORD']
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
## Requirements
|
78
|
+
|
79
|
+
* ruby > 1.9.x
|
80
|
+
* net/imap
|
81
|
+
* [Mail](http://rubygems.org/gems/mail)
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
1. Fork it
|
86
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
87
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
88
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
89
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/gmailish.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gmailish/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gmailish"
|
8
|
+
spec.version = Gmailish::VERSION
|
9
|
+
spec.authors = ["Charles J Hardy"]
|
10
|
+
spec.email = ["chuckjhardy@gmail.com"]
|
11
|
+
spec.description = %q{Grabs unread emails for Gmail, marks them as read, applies a given label and archives them.}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = "https://github.com/ChuckJHardy/Gmailish"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "mail", "~> 2.5.3"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.13.0"
|
26
|
+
spec.add_development_dependency "rspec-fire", "~> 1.1.3"
|
27
|
+
spec.add_development_dependency "simplecov", "~> 0.7.1"
|
28
|
+
spec.add_development_dependency "simplecov-vim", "~> 0.0.1"
|
29
|
+
end
|
data/lib/gmailish.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Gmailish
|
2
|
+
class Account
|
3
|
+
ADDRESS = 'imap.gmail.com'
|
4
|
+
PORT = 993
|
5
|
+
UNREAD = 'UNSEEN'
|
6
|
+
|
7
|
+
def initialize(username, password)
|
8
|
+
@username = username
|
9
|
+
@password = password
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.process(username, password)
|
13
|
+
new(username, password).process
|
14
|
+
end
|
15
|
+
|
16
|
+
def process
|
17
|
+
actions.process { messages }
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def messages
|
22
|
+
@messages ||= account.uid_search([UNREAD]).map do |uid|
|
23
|
+
Message.process(account, uid)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
attr_reader :username, :password
|
30
|
+
|
31
|
+
def account
|
32
|
+
@account ||= Net::IMAP.new(ADDRESS, PORT, true, nil, false)
|
33
|
+
end
|
34
|
+
|
35
|
+
def actions
|
36
|
+
@actions ||= Actions.new(account, username, password)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Gmailish
|
2
|
+
class Actions
|
3
|
+
attr_accessor :logged_in
|
4
|
+
|
5
|
+
INBOX = 'inbox'
|
6
|
+
|
7
|
+
def initialize(account, username, password)
|
8
|
+
@account = account
|
9
|
+
@username = username
|
10
|
+
@password = password
|
11
|
+
@logged_in = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def process(&block)
|
15
|
+
if block_given?
|
16
|
+
login
|
17
|
+
inbox
|
18
|
+
yield
|
19
|
+
logout
|
20
|
+
else
|
21
|
+
raise Error::NoMessageError, "Messages must be passed within a block."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def logged_in?
|
26
|
+
logged_in
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_reader :account, :username, :password
|
32
|
+
|
33
|
+
def login
|
34
|
+
account.login(username, password).tap do |response|
|
35
|
+
@logged_in = true if ok?(response)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def logout
|
40
|
+
account.logout.tap do |response|
|
41
|
+
@logged_in = false if ok?(response)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def inbox
|
46
|
+
account.select(INBOX)
|
47
|
+
end
|
48
|
+
|
49
|
+
def ok?(response)
|
50
|
+
response && response.name == 'OK'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/gmailish/all.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Gmailish
|
2
|
+
class Flagger
|
3
|
+
DELETED = :Deleted
|
4
|
+
UNREAD = :Seen
|
5
|
+
|
6
|
+
def initialize(account, uid)
|
7
|
+
@account = account
|
8
|
+
@uid = uid
|
9
|
+
end
|
10
|
+
|
11
|
+
def delete
|
12
|
+
flag(DELETED)
|
13
|
+
end
|
14
|
+
|
15
|
+
def unread
|
16
|
+
flag(UNREAD)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :account, :uid
|
22
|
+
|
23
|
+
def flag(name)
|
24
|
+
account.uid_store(uid, "+FLAGS", [name])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Gmailish
|
2
|
+
class Labeler
|
3
|
+
TRANSFERED = 'Transfered'
|
4
|
+
ARCHIVE = '[Gmail]/All Mail'
|
5
|
+
|
6
|
+
def initialize(account, uid)
|
7
|
+
@account = account
|
8
|
+
@uid = uid
|
9
|
+
end
|
10
|
+
|
11
|
+
def transfered
|
12
|
+
label(TRANSFERED)
|
13
|
+
end
|
14
|
+
|
15
|
+
def all_mail
|
16
|
+
label(ARCHIVE)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :account, :uid
|
22
|
+
|
23
|
+
def label(name)
|
24
|
+
account.uid_copy(uid, name)
|
25
|
+
rescue
|
26
|
+
raise Error::NoLabelError, "Manually create `#{name}' label."
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Gmailish
|
2
|
+
class Message
|
3
|
+
RFC = 'RFC822'
|
4
|
+
|
5
|
+
def initialize(account, uid)
|
6
|
+
@account = account
|
7
|
+
@uid = uid
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.process(account, uid)
|
11
|
+
new(account, uid).process
|
12
|
+
end
|
13
|
+
|
14
|
+
def process
|
15
|
+
message
|
16
|
+
actions
|
17
|
+
message
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :account, :uid
|
23
|
+
|
24
|
+
def actions
|
25
|
+
Labeler.new(account, uid).transfered
|
26
|
+
Flagger.new(account, uid).unread
|
27
|
+
Mover.new(account, uid).archive
|
28
|
+
end
|
29
|
+
|
30
|
+
def raw
|
31
|
+
@raw ||= Array(account.uid_fetch(uid, RFC)).first
|
32
|
+
end
|
33
|
+
|
34
|
+
def body
|
35
|
+
raw.attr[RFC] unless raw.nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
def message
|
39
|
+
@message ||= Mail.new(body)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Gmailish
|
2
|
+
class Mover
|
3
|
+
def initialize(account, uid)
|
4
|
+
@account = account
|
5
|
+
@uid = uid
|
6
|
+
end
|
7
|
+
|
8
|
+
def archive
|
9
|
+
all_mail
|
10
|
+
delete
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
attr_reader :account, :uid
|
16
|
+
|
17
|
+
def all_mail
|
18
|
+
Labeler.new(account, uid).all_mail
|
19
|
+
end
|
20
|
+
|
21
|
+
def delete
|
22
|
+
Flagger.new(account, uid).delete
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|