mailtruck 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mailtruck.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bruz Marzolf
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,129 @@
1
+ # Mailtruck
2
+
3
+ [![Build Status](https://travis-ci.org/CXInc/mailtruck-ruby.png)](https://travis-ci.org/CXInc/mailtruck-ruby)
4
+
5
+ There are occasions when it's necessary to receive an email and follow a link
6
+ as part of an integration test, for instance email confirmation. When the
7
+ email is being sent from the app where the test is running, there are some
8
+ nice options (see Similar Projects below) to aid in integration testing. But
9
+ if the email is being sent from another service you don't control, it's not as
10
+ easy to hook into the process. Mailtruck aims to make that doable.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'mailtruck'
17
+
18
+ And then run:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself with:
23
+
24
+ $ gem install mailtruck
25
+
26
+ ## Usage
27
+
28
+ ### Quick Start
29
+
30
+ ```ruby
31
+ mailtruck = Mailtruck.new
32
+
33
+ # an email address where Mailtruck can pick up your email
34
+ address = mailtruck.email_address
35
+ # => somerandomstring@mailtruck.bruzilla.com
36
+
37
+ # Mailtruck will wait until you receive
38
+ emails = mailtruck.wait_for_emails do
39
+ # YOUR CODE TO SEND EMAIL TO address HERE
40
+ end
41
+
42
+ emails.each do |email|
43
+ puts "Email to, from, subject, body: #{email.to}, #{email.from}, " +
44
+ "#{email.subject}, #{email.body}"
45
+ end
46
+ ```
47
+
48
+ ### Configuration
49
+
50
+ ```ruby
51
+ Mailtruck.configure do |config|
52
+ # host where emails will be sent, defaults to mailtruck.bruzilla.com
53
+ config.email_host = "example.com"
54
+
55
+ # Mailtruck server, defaults to http://mailtruck.herokuapp.com/faye
56
+ config.receiver_url = "http://example.com/faye"
57
+
58
+ # timeout in seconds when waiting for email, defaults to 30
59
+ config.timeout = 60
60
+ end
61
+ ```
62
+
63
+ ### Multiple emails
64
+
65
+ When multiple emails are being sent, ask for the number of emails needed and
66
+ it will wait for that number of emails.
67
+
68
+ ```ruby
69
+ mailtruck = Mailtruck.new
70
+
71
+ addresses = [mailtruck.email_address, mailtruck.email_address]
72
+
73
+ emails = mailtruck.wait_for_emails do
74
+ # YOUR CODE TO SEND EMAIL TO EACH ADDRESS IN addresses HERE
75
+ end
76
+ ```
77
+
78
+ ### Capybara
79
+
80
+ When running Mailtruck in Capybara tests, the usual helper methods are
81
+ available:
82
+
83
+ ```ruby
84
+ emails = mailtruck.wait_for_emails do
85
+ # YOUR CODE TO SEND EMAIL TO address HERE
86
+ end
87
+
88
+ emails.first.body.should have_content("Way to go, champ!")
89
+ ```
90
+
91
+ ## How does it work?
92
+
93
+ When `wait_for_emails` is called, Mailtruck connects to a service that will
94
+ listen for email being received at the addresses it has provided with the
95
+ `email_address` method. The code block passed to `wait_for_emails` is then
96
+ run, and assuming it sends emails to the provided addresses, they will get
97
+ posted back to the Mailtruck back end service. They messages are then passed
98
+ to the listener and returned as instances of `Mailtruck::Email`.
99
+
100
+ ## Security
101
+
102
+ There is none currently, so **consider any emails you send through Mailtruck
103
+ public**. This may not be an issue if you're just sending dummy test data and
104
+ content that's not sensitive, but if there's anything in your emails that
105
+ you want to keep private then don't use Mailtruck.
106
+
107
+ Making Mailtruck more secure is definitely a future possibility.
108
+
109
+ ## Tests
110
+
111
+ Simply run:
112
+
113
+ rake test
114
+
115
+ ## Similar Projects
116
+
117
+ - [CapybaraEmail](https://github.com/dockyard/capybara-email)
118
+ - [Letter Opener](https://github.com/ryanb/letter_opener)
119
+ - [MailCatcher](http://mailcatcher.me/)
120
+ - [Mailtrap](http://mailtrap.io/)
121
+ - [MailView](https://github.com/37signals/mail_view)
122
+
123
+ ## Contributing
124
+
125
+ 1. Fork it
126
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
127
+ 3. Commit your changes, with tests (`git commit -am 'Add some feature'`)
128
+ 4. Push to the branch (`git push origin my-new-feature`)
129
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ task :default => [:test]
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'spec'
9
+ t.pattern = "spec/lib/*_spec.rb"
10
+ # t.verbose = true
11
+ end
@@ -0,0 +1,68 @@
1
+ require "securerandom"
2
+ require "faye"
3
+
4
+ require "mailtruck/configuration"
5
+ require "mailtruck/email"
6
+ require "mailtruck/email_address"
7
+ require "mailtruck/receiver"
8
+ require "mailtruck/version"
9
+
10
+ class Mailtruck
11
+
12
+ class Timeout < StandardError; end
13
+
14
+ # Configure Mailtruck.
15
+ #
16
+ # Capybara.configure do |config|
17
+ # config.timeout = 60
18
+ # end
19
+ #
20
+ # === Options
21
+ #
22
+ # [email_host = String] Host where emails will be sent, defaults to mailtruck.bruzilla.com
23
+ # [receiver_url = String] Mailtruck server, defaults to http://mailtruck.herokuapp.com/faye
24
+ # [timeout = Integer] Timeout in seconds when waiting for email, defaults to 30
25
+ #
26
+ def self.configure
27
+ yield configuration
28
+ end
29
+
30
+ # @return [Mailtruck::Configuration] the Mailtruck configuration.
31
+ def self.configuration
32
+ @configuration ||= Configuration.new
33
+ end
34
+
35
+ # Generates an email address that Mailtruck can receive email at.
36
+ #
37
+ # @example
38
+ # mailtruck = Mailtruck.new
39
+ # address = mailtruck.email_address
40
+ #
41
+ # emails = mailtruck.wait_for_emails do
42
+ # MyApp.send_email_to(address)
43
+ # end
44
+ #
45
+ #
46
+ # @yield Block to run that should trigger emails
47
+ # @return [String] an email address
48
+ def email_address
49
+ address = EmailAddress.random
50
+ addresses << address
51
+
52
+ address.to_s
53
+ end
54
+
55
+ # Waits for emails to be sent to #email_address and returns them.
56
+ #
57
+ # @return [Array<Mailtruck::Email>] the received emails
58
+ def wait_for_emails(&block)
59
+ Receiver.wait_for(addresses, block)
60
+ end
61
+
62
+ private
63
+
64
+ def addresses
65
+ @addresses ||= []
66
+ end
67
+
68
+ end
@@ -0,0 +1,9 @@
1
+ class Mailtruck::Configuration
2
+ attr_accessor :email_host, :receiver_url, :timeout
3
+
4
+ def initialize(attributes = {})
5
+ @email_host = attributes[:email_host] || "mailtruck.bruzilla.com"
6
+ @receiver_url = attributes[:receiver_url] || "http://mailtruck.herokuapp.com/faye"
7
+ @timeout = attributes[:timeout] || 30
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ class Mailtruck::Email
2
+ attr_accessor :to, :from, :subject
3
+
4
+ def initialize(attributes)
5
+ [:to, :from, :subject, :html].each do |key|
6
+ instance_variable_set "@#{key}", attributes[key.to_s]
7
+ end
8
+ end
9
+
10
+ def body
11
+ @html
12
+ end
13
+
14
+ end
@@ -0,0 +1,23 @@
1
+ class Mailtruck::EmailAddress
2
+
3
+ attr_reader :prefix
4
+
5
+ def self.random
6
+ new(random_string)
7
+ end
8
+
9
+ def initialize(prefix)
10
+ @prefix = prefix
11
+ end
12
+
13
+ def to_s
14
+ "#{@prefix}@#{Mailtruck.configuration.email_host}"
15
+ end
16
+
17
+ private
18
+
19
+ def self.random_string
20
+ # SecureRandom.urlsafe_base64
21
+ "bob"
22
+ end
23
+ end
@@ -0,0 +1,48 @@
1
+ Thread.abort_on_exception = true
2
+
3
+ class Mailtruck::Receiver
4
+
5
+ def self.wait_for(addresses, block)
6
+ receiver = new(addresses)
7
+ receiver.wait(block)
8
+ end
9
+
10
+ def initialize(addresses)
11
+ @addresses = addresses
12
+ @emails = []
13
+ end
14
+
15
+ def wait(block)
16
+ Timeout::timeout(Mailtruck.configuration.timeout) do
17
+ EM.run {
18
+ subscribe_to_addresses
19
+
20
+ block.call
21
+ }
22
+ end
23
+
24
+ @emails
25
+ rescue Timeout::Error => e
26
+ raise Mailtruck::Timeout
27
+ end
28
+
29
+ private
30
+
31
+ def subscribe_to_addresses
32
+ client.subscribe("/bob") do |message|
33
+ @emails << Mailtruck::Email.new(message)
34
+ check_emails
35
+ end
36
+ end
37
+
38
+ def client
39
+ @client ||= Faye::Client.new(Mailtruck.configuration.receiver_url)
40
+ end
41
+
42
+ def check_emails
43
+ if @emails.size >= @addresses.size
44
+ EM.stop_event_loop
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ class Mailtruck
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mailtruck/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mailtruck"
8
+ spec.version = Mailtruck::VERSION
9
+ spec.authors = ["Bruz Marzolf"]
10
+ spec.email = ["bruz.marzolf@gmail.com"]
11
+ spec.description = %q{Mailtruck helps with testing features that involve sending and receiving email from external services.}
12
+ spec.summary = %q{Receive emails from external services for testing}
13
+ spec.homepage = ""
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 "faye", "~> 0.8"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "mocha", "~> 0.14"
26
+ end
@@ -0,0 +1,50 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Mailtruck do
4
+
5
+ let(:mailtruck) { Mailtruck.new }
6
+
7
+ before(:each) do
8
+ Mailtruck.configure do |config|
9
+ config.email_host = "example.org"
10
+ config.timeout = 0.1
11
+ end
12
+ end
13
+
14
+ it "is configurable" do
15
+ Mailtruck.configure do |config|
16
+ config.email_host = "example.com"
17
+ end
18
+
19
+ Mailtruck.configuration.email_host.must_equal "example.com"
20
+ end
21
+
22
+ it "provides email addresses to send emails to" do
23
+ mailtruck.email_address.must_match /.*@example.org/
24
+ end
25
+
26
+ it "runs some code and waits for emails to come in" do
27
+ address = mailtruck.email_address
28
+
29
+ client = Object.new
30
+ def client.address=(address)
31
+ @address = address
32
+ end
33
+ client.address = address
34
+
35
+ def client.subscribe(path)
36
+ yield "to" => @address
37
+ end
38
+
39
+ Faye::Client.stubs("new" => client)
40
+
41
+ emails = mailtruck.wait_for_emails do
42
+ # code that would trigger an email would go here
43
+ end
44
+
45
+ emails.size.must_equal 1
46
+ email = emails.first
47
+ email.to.must_equal(address)
48
+ end
49
+
50
+ end
@@ -0,0 +1,4 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'mocha/setup'
4
+ require File.expand_path('../../lib/mailtruck.rb', __FILE__)
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailtruck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bruz Marzolf
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faye
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.8'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.8'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: mocha
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.14'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.14'
78
+ description: Mailtruck helps with testing features that involve sending and receiving
79
+ email from external services.
80
+ email:
81
+ - bruz.marzolf@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .travis.yml
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - lib/mailtruck.rb
93
+ - lib/mailtruck/configuration.rb
94
+ - lib/mailtruck/email.rb
95
+ - lib/mailtruck/email_address.rb
96
+ - lib/mailtruck/receiver.rb
97
+ - lib/mailtruck/version.rb
98
+ - mailtruck.gemspec
99
+ - spec/lib/mailtruck_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 3253366713766564001
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ segments:
124
+ - 0
125
+ hash: 3253366713766564001
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.25
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Receive emails from external services for testing
132
+ test_files:
133
+ - spec/lib/mailtruck_spec.rb
134
+ - spec/spec_helper.rb