voomify_email_integrator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +64 -0
- data/lib/email_integrator.rb +6 -0
- data/lib/email_integrator/email_integrator.rb +75 -0
- data/spec/email_integrator_spec.rb +86 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/exception_helper.rb +22 -0
- data/tasks/rspec.rake +21 -0
- metadata +75 -0
data/README.rdoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= email-integrator
|
2
|
+
|
3
|
+
* http://github.com/voomify/email-integrator
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
This gem will allow you to use email as a simple message queue like integrator
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Reads an email inbox and allows a derived class to process the message
|
12
|
+
* Logs errors and ignored records
|
13
|
+
* errors are sent to hoptoad
|
14
|
+
|
15
|
+
== SYNOPSIS:
|
16
|
+
|
17
|
+
FIX (code sample of usage)
|
18
|
+
|
19
|
+
== REQUIREMENTS:
|
20
|
+
|
21
|
+
Hoptoad for error notifications
|
22
|
+
|
23
|
+
== INSTALL:
|
24
|
+
|
25
|
+
This gem is not published. You can install it locally using the following.
|
26
|
+
For rails you should vendor the gem.
|
27
|
+
|
28
|
+
git clone git://github.com/voomify/email-integrator.git
|
29
|
+
|
30
|
+
# This is required by hoe and to install the gem locally you have to run it.
|
31
|
+
$ sudo gem install rubyforge
|
32
|
+
|
33
|
+
# create a default ~/.rubyforge/user-config.yml file.
|
34
|
+
# note the existence of the ~/.rubyforge/user-config.yml file.
|
35
|
+
# you don't need a rubyforge account, only this file with ANY values in it
|
36
|
+
$ rubyforge setup
|
37
|
+
|
38
|
+
cd email-integrator
|
39
|
+
rake install_gem
|
40
|
+
|
41
|
+
== LICENSE:
|
42
|
+
|
43
|
+
(The MIT License)
|
44
|
+
|
45
|
+
Copyright (c) 2010 russelledens and Voomify, LLC
|
46
|
+
|
47
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
48
|
+
a copy of this software and associated documentation files (the
|
49
|
+
'Software'), to deal in the Software without restriction, including
|
50
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
51
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
52
|
+
permit persons to whom the Software is furnished to do so, subject to
|
53
|
+
the following conditions:
|
54
|
+
|
55
|
+
The above copyright notice and this permission notice shall be
|
56
|
+
included in all copies or substantial portions of the Software.
|
57
|
+
|
58
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
59
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
60
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
61
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
62
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
63
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
64
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'net/pop'
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
# This is the base class that iterates an email and processes inbound requests
|
7
|
+
# It should be used with craken or cron
|
8
|
+
#
|
9
|
+
module EmailIntegrator
|
10
|
+
class POP3
|
11
|
+
def initialize(connection)
|
12
|
+
@connection = connection
|
13
|
+
[:server, :username, :password].each do |param|
|
14
|
+
raise ArgumentError, "missing argument #{param.to_s}" unless @connection.has_key? param
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
[:server, :username, :password].each do |method|
|
20
|
+
define_method method do
|
21
|
+
@connection[method]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def is_production?
|
26
|
+
ENV["RAILS_ENV"] == "production" ? true : false
|
27
|
+
end
|
28
|
+
|
29
|
+
def process(pattern)
|
30
|
+
#
|
31
|
+
# Connect to email server and look for emails
|
32
|
+
#
|
33
|
+
errors=[]
|
34
|
+
conn = Net::POP3.new(server)
|
35
|
+
conn.start(username,
|
36
|
+
password) do |pop|
|
37
|
+
pop.mails.each do |msg|
|
38
|
+
begin
|
39
|
+
xml = msg.pop[pattern]
|
40
|
+
if xml
|
41
|
+
processed = yield(xml)
|
42
|
+
msg.delete if processed && is_production?
|
43
|
+
end
|
44
|
+
rescue Exception => e
|
45
|
+
errors << e
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
if errors.size>0
|
50
|
+
raise(RuntimeError,
|
51
|
+
"Processing email integration generated exception(#{errors.size}).\n"+
|
52
|
+
"Until it is resolved it the message will be stuck in the email inbox: (#{server}, #{username})."+
|
53
|
+
"Error Messages: " + error_messages(errors))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def error_messages(errors)
|
58
|
+
message = ""
|
59
|
+
errors.each do |e|
|
60
|
+
message<< "#{e.class} #{e.message}\n#{clean_backtrace(e)}\n"
|
61
|
+
end
|
62
|
+
message
|
63
|
+
end
|
64
|
+
|
65
|
+
def clean_backtrace(exception)
|
66
|
+
if backtrace = exception.backtrace
|
67
|
+
if defined?(RAILS_ROOT)
|
68
|
+
backtrace.map { |line| line.sub(RAILS_ROOT, '')+"\n" }
|
69
|
+
else
|
70
|
+
backtrace
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
|
4
|
+
# Time to add your specs!
|
5
|
+
# http://rspec.info/
|
6
|
+
describe EmailIntegrator do
|
7
|
+
|
8
|
+
before(:all) do
|
9
|
+
class HoptoadNotifier
|
10
|
+
def self.notify(args)
|
11
|
+
raise "Unexpected error"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "throws exception if server is not passed" do
|
17
|
+
should_throw_exception(ArgumentError) do
|
18
|
+
EmailIntegrator::POP3.new(:username=>"username",
|
19
|
+
:password=>"password")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "throws exception if username is not passed" do
|
24
|
+
should_throw_exception(ArgumentError) do
|
25
|
+
EmailIntegrator::POP3.new(:server=>"server",
|
26
|
+
:password=>"password")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "throws exception if password is not passed" do
|
31
|
+
should_throw_exception(ArgumentError) do
|
32
|
+
EmailIntegrator::POP3.new(:server=>"server",
|
33
|
+
:username=>"username")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
it "throws exception if bad server is passed" do
|
39
|
+
should_throw_exception(SocketError) do
|
40
|
+
EmailIntegrator::POP3.new(:server=>"garbage",
|
41
|
+
:username=>"username",
|
42
|
+
:password=>"password").process(/.*/) { |msg| }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "process email" do
|
47
|
+
|
48
|
+
mock_message = mock('message')
|
49
|
+
mock_message.should_receive(:pop).and_return("look at me")
|
50
|
+
|
51
|
+
mock_connection = mock('connection')
|
52
|
+
mock_connection.should_receive(:mails).and_return([mock_message])
|
53
|
+
mock_connection.should_receive(:start).and_yield(mock_connection)
|
54
|
+
|
55
|
+
Net::POP3.should_receive(:new).and_return(mock_connection)
|
56
|
+
|
57
|
+
|
58
|
+
EmailIntegrator::POP3.new(:server=>"garbage",
|
59
|
+
:username=>"username",
|
60
|
+
:password=>"password").process(/.*/) do |xml|
|
61
|
+
xml.should == "look at me"
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
it "process email but don't find a message" do
|
67
|
+
|
68
|
+
mock_message = mock('message')
|
69
|
+
mock_message.should_receive(:pop).and_return("look at me")
|
70
|
+
|
71
|
+
mock_connection = mock('connection')
|
72
|
+
mock_connection.should_receive(:mails).and_return([mock_message])
|
73
|
+
mock_connection.should_receive(:start).and_yield(mock_connection)
|
74
|
+
|
75
|
+
#Net::POP3.should_receive(:new).and_return(MockConnection.new)
|
76
|
+
Net::POP3.should_receive(:new).and_return(mock_connection)
|
77
|
+
|
78
|
+
EmailIntegrator::POP3.new(:server=>"garbage",
|
79
|
+
:username=>"username",
|
80
|
+
:password=>"password").process(/anything/) do |xml|
|
81
|
+
raise 'This should not be called!'
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
+
gem 'rspec'
|
6
|
+
require 'spec'
|
7
|
+
end
|
8
|
+
|
9
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
10
|
+
require 'email_integrator'
|
11
|
+
|
12
|
+
|
13
|
+
# Requires supporting files with custom matchers and macros, etc,
|
14
|
+
# in ./support/ and its subdirectories.
|
15
|
+
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec/test/unit'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
def should_throw_exception(expected_exception_class, &block)
|
5
|
+
|
6
|
+
begin
|
7
|
+
block.call
|
8
|
+
rescue expected_exception_class
|
9
|
+
threw_error = true
|
10
|
+
rescue Exception => err
|
11
|
+
end
|
12
|
+
assert threw_error, message(expected_exception_class, err)
|
13
|
+
end
|
14
|
+
|
15
|
+
def message(expected_exception_class, err)
|
16
|
+
error = "The test did NOT throw the expected exception: #{expected_exception_class}"
|
17
|
+
if err
|
18
|
+
error << " threw #{err.class} instead"
|
19
|
+
else
|
20
|
+
error << " no exceptions were thrown"
|
21
|
+
end
|
22
|
+
end
|
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: voomify_email_integrator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Russell Edens
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-05 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: This gem contains the logic for reading email from a pop3 mailbox
|
23
|
+
email: russell@voomify.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- lib/email_integrator.rb
|
32
|
+
- lib/email_integrator/email_integrator.rb
|
33
|
+
- tasks/rspec.rake
|
34
|
+
- README.rdoc
|
35
|
+
- spec/email_integrator_spec.rb
|
36
|
+
- spec/support/exception_helper.rb
|
37
|
+
- spec/spec_helper.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://www.github.com/voomify/email_integrator
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Voomify email integrator. Reads POP3 emails.
|
72
|
+
test_files:
|
73
|
+
- spec/email_integrator_spec.rb
|
74
|
+
- spec/support/exception_helper.rb
|
75
|
+
- spec/spec_helper.rb
|