This repository has been archived by the owner on Aug 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from freerange/import-all-a-single-users-messages
Import many messages from multiple accounts Chris, James M and I reviewed Tom's changes and agreed to merge into master.
- Loading branch information
Showing
27 changed files
with
454 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
rvm: | ||
- 1.9.3 | ||
- 1.9.3 | ||
before_script: | ||
- "bundle exec rake db:migrate db:test:prepare" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<ul> | ||
<li><%= @message.subject %></li> | ||
<li><%= @message.date %></li> | ||
<li><%= @message.from %></li> | ||
</ul> | ||
<pre> | ||
<%= @message.original.to_s %> | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class InitialSchema < ActiveRecord::Migration | ||
def change | ||
create_table :messages do |table| | ||
table.string :account | ||
table.string :uid | ||
table.string :subject | ||
table.datetime :date | ||
table.string :from | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
require 'gmail_imap_client' | ||
require 'message_repository' | ||
|
||
class AccountMessageImporter | ||
class << self | ||
def import_for(email, password) | ||
imap_client = GmailImapClient.connect(email, password) | ||
MessageImporter.new(imap_client).import_into(MessageRepository.instance) | ||
mailbox = GoogleMail::Mailbox.connect(email, password) | ||
MessageImporter.new(mailbox).import_into(MessageRepository) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class CacheBackedMessageStore | ||
def initialize(cache = ActiveSupport::Cache::FileStore.new(Rails.root + 'data' + Rails.env + 'messages')) | ||
@cache = cache | ||
end | ||
|
||
def add(account, uid, message) | ||
@cache.write [account, uid], message | ||
end | ||
|
||
def find(account, uid) | ||
@cache.read [account, uid] | ||
end | ||
|
||
class << self | ||
delegate :add, :find, to: :instance | ||
|
||
def instance | ||
@instance ||= new | ||
end | ||
|
||
def configure(*args) | ||
@instance = new(*args) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
require 'fileutils' | ||
require 'base64' | ||
|
||
class FileBasedMessageStore | ||
def initialize(root_path) | ||
attr_reader :root_path | ||
|
||
def initialize(root_path = Rails.root + 'data' + Rails.env + 'messages') | ||
@root_path = root_path | ||
end | ||
|
||
def include?(key) | ||
File.exist? key_path(key) | ||
end | ||
|
||
def [](key) | ||
if include?(key) | ||
Base64.strict_decode64(File.read(key_path(key))) | ||
end | ||
end | ||
|
||
def []=(key, value) | ||
full_path = File.expand_path(key, @root_path) | ||
FileUtils.mkdir_p File.dirname(full_path) | ||
File.write full_path, value | ||
FileUtils.mkdir_p File.dirname(key_path(key)) | ||
File.write key_path(key), Base64.strict_encode64(value) | ||
end | ||
|
||
def values | ||
Dir["#{@root_path}/**"].map do |path| | ||
File.read(path) | ||
Dir["#{root_path}/**"].map do |path| | ||
Base64.strict_decode64(File.read(path)) | ||
end | ||
end | ||
|
||
def key_path(key) | ||
File.expand_path(Digest::MD5.hexdigest(key.to_s), root_path) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module GoogleMail | ||
class ImapCache | ||
delegate :read, :write, :fetch, to: :@cache | ||
|
||
def initialize(cache = ActiveSupport::Cache::FileStore.new(Rails.root + 'tmp' + 'cache' + 'imap')) | ||
@cache = cache | ||
end | ||
|
||
class << self | ||
delegate :read, :write, :fetch, to: :instance | ||
|
||
def instance | ||
@instance ||= new | ||
end | ||
|
||
def configure(*args) | ||
@instance = new(*args) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'net/imap' | ||
|
||
module GoogleMail | ||
class Mailbox | ||
class AuthenticatedConnection | ||
attr_reader :email | ||
delegate :examine, :uid_search, :list, :uid_fetch, to: :@imap | ||
|
||
def initialize(email, password) | ||
@imap = ::Net::IMAP.new 'imap.gmail.com', 993, true | ||
@imap.login email, password | ||
@email = email | ||
end | ||
end | ||
|
||
class CachedConnection | ||
delegate :email, :examine, :uid_search, :list, to: :@connection | ||
|
||
def initialize(email, password, cache = GoogleMail::ImapCache) | ||
@connection = AuthenticatedConnection.new(email, password) | ||
@cache = cache | ||
end | ||
|
||
def uid_fetch(uid, command) | ||
@cache.fetch [@connection.email, uid, command] do | ||
@connection.uid_fetch(uid, command) | ||
end | ||
end | ||
end | ||
|
||
cattr_accessor :connection_class | ||
self.connection_class = CachedConnection | ||
|
||
attr_reader :connection | ||
delegate :email, to: :connection | ||
|
||
def initialize(connection) | ||
@connection = connection | ||
mailboxes = @connection.list('', '%').collect(&:name) | ||
if mailboxes.include?('[Gmail]') | ||
@connection.examine '[Gmail]/All Mail' | ||
else | ||
@connection.examine '[Google Mail]/All Mail' | ||
end | ||
end | ||
|
||
def uids | ||
connection.uid_search('ALL') | ||
end | ||
|
||
def message(uid) | ||
connection.uid_fetch(uid, 'BODY.PEEK[]').map {|m| m.attr['BODY[]']}.first | ||
end | ||
|
||
class << self | ||
def connect(email, password) | ||
new connection_class.new(email, password) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
class MessageImporter | ||
attr_reader :message_client | ||
attr_reader :mailbox | ||
|
||
def initialize(message_client) | ||
@message_client = message_client | ||
def initialize(mailbox) | ||
@mailbox = mailbox | ||
end | ||
|
||
def import_into(repository) | ||
message_client.inbox_messages.each do |message| | ||
repository.store(message) | ||
mailbox.uids.each do |uid| | ||
unless repository.exists?(mailbox.email, uid) | ||
repository.add mailbox.email, uid, mailbox.message(uid) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.