gmailish 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .ruby-version
4
+ .DS_Store
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ coverage.vim
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
@@ -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
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ - bundle exec rspec spec/
3
+ rvm:
4
+ - 2.0.0
5
+ - 1.9.3
6
+ notifications:
7
+ email:
8
+ - chuckjhardy@gmail.com
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'coveralls', require: false
4
+
5
+ # Specify your gem's dependencies in gmailish.gemspec
6
+ gemspec
@@ -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.
@@ -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
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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
@@ -0,0 +1,7 @@
1
+ require "gmailish/version"
2
+ require 'net/imap'
3
+ require 'mail'
4
+
5
+ module Gmailish; end
6
+
7
+ require 'gmailish/all'
@@ -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
@@ -0,0 +1,7 @@
1
+ require_relative 'error'
2
+ require_relative 'account'
3
+ require_relative 'actions'
4
+ require_relative 'message'
5
+ require_relative 'labeler'
6
+ require_relative 'flagger'
7
+ require_relative 'mover'
@@ -0,0 +1,6 @@
1
+ module Gmailish
2
+ module Error
3
+ class NoMessageError < StandardError; end
4
+ class NoLabelError < StandardError; end
5
+ end
6
+ end
@@ -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