idlemailer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3ef0ca694fb5628659b30090fbaec75f6da934b
4
+ data.tar.gz: fe37ada9cca45b12ec8f7f0f98fe3fbf090209c8
5
+ SHA512:
6
+ metadata.gz: 4f090285bb04f67482db2dc1a29a03612f6deb46cf40c597358ff90333074c2418578f79c0cb347117bee2bba7c0e0dae2e8e599f4d18b21f0ffdcb371a4dd3e
7
+ data.tar.gz: b8edf7286d8a143955dcf288aeb34e06a7a48e8dc87c773d036146a657ab0d9751cb1f3008c71ae51011f8bd879d3fc384e979107aaa4d47beac51f418eef01c
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Jordan Hollinger
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # IdleMailer
2
+
3
+ A lightweight alternative to ActionMailer for hipsters who use Ruby but not Rails. Powered by [mail](http://www.rubydoc.info/gems/mail). Great for API-only backends that need to send email.
4
+
5
+ ## Installation
6
+
7
+ $ [sudo] gem install idlemailer
8
+ # Or add "idlemailer" to your Gemfile
9
+
10
+ ## Use
11
+
12
+ IdleMailer is all about providing mailer classes and templates. But [the mail gem](http://www.rubydoc.info/gems/mail) has a great API, so you have unfettered access to it in your mailers through the "mail" object.
13
+
14
+ # Define your mailer class
15
+ class WidgetMailer
16
+ include IdleMailer::Mailer
17
+
18
+ def initialize(user, widget)
19
+ mail.to = user.email
20
+ mail.subject = "Widget #{widget.sku}"
21
+ @widget = widget
22
+ end
23
+ end
24
+
25
+ # Create widget.html.erb and/or widget.text.erb templates.
26
+ # They'll have access to instance variables like @widget above.
27
+
28
+ # Send your mail
29
+ mailer = WidgetMailer.new(current_user, widget)
30
+ mailer.deliver
31
+
32
+ ## Configure
33
+
34
+ These are the default options. Salt to taste.
35
+
36
+ IdleMailer.config do |config|
37
+ # Directory containing the mailer templates
38
+ config.templates = Pathname.new(Dir.getwd).join('templates')
39
+
40
+ # Name of the layout template. Here, the file(s) would be named
41
+ # mailer_layout.html.erb and/or mailer_layout.text.erb.
42
+ config.layout = 'mailer_layout'
43
+
44
+ # Email delivery method (mail gem). Set to :test when testing or when developing locally
45
+ config.delivery_method = :smtp
46
+
47
+ # Delivery options (mail gem)
48
+ config.delivery_options = {
49
+ user_name: ENV['MAIL_USER'],
50
+ password: ENV['MAIL_PASSWORD'],
51
+ domain: ENV['MAIL_DOMAIN'],
52
+ address: ENV['MAIL_ADDRESS'],
53
+ port: ENV['MAIL_PORT'] || 587,
54
+ authentication: ENV['MAIL_AUTHENTICATION'] || 'plain',
55
+ enable_starttls_auto: (ENV['MAIL_TLS'] ? true : false)
56
+ }
57
+
58
+ # Default "from" address for all mailers
59
+ config.default_from = nil
60
+
61
+ # Write all deliveries to $stdout
62
+ config.log = false
63
+ end
64
+
65
+ ## Testing
66
+
67
+ Put the mailer in testing mode:
68
+
69
+ IdleMailer.config do |config|
70
+ config.delivery_method = :test
71
+ end
72
+
73
+ Then use mail gem's built in testing helpers in your specs:
74
+
75
+ sent = Mail::TestMailer.deliveries.any? { |mail| mail.to.include? @user.email }
76
+
77
+ ## License
78
+
79
+ MIT License. See LICENSE for details.
80
+
81
+ ## Copyright
82
+
83
+ Copyright (c) 2015 Jordan Hollinger
@@ -0,0 +1,17 @@
1
+ module IdleMailer
2
+ # Struct for holding IdleMailer config
3
+ # templates A Pathname object pointing to the mail templates directory (defaults to ./templates)
4
+ # layout Name of layout file, minus extention (defaults to mailer_layout)
5
+ # delivery_method Symbol like :smtp or :test (see the mail gem for all options)
6
+ # delivery_options Hash of delivery options (see the mail gem for all options)
7
+ # default_from Default "from" address if it's left blank
8
+ # log When true, writes delivered messages to $stdout (default false)
9
+ Config = Struct.new(:templates, :layout, :delivery_method, :delivery_options, :default_from, :log)
10
+ @config = Config.new
11
+
12
+ # Takes a block and hands it an IdleMailer::Config object
13
+ def self.config
14
+ yield @config if block_given?
15
+ @config
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ IdleMailer.config do |config|
2
+ config.templates = Pathname.new(Dir.getwd).join('templates')
3
+ config.layout = 'mailer_layout'
4
+ config.delivery_method = :smtp
5
+ config.delivery_options = {
6
+ user_name: ENV['MAIL_USER'],
7
+ password: ENV['MAIL_PASSWORD'],
8
+ domain: ENV['MAIL_DOMAIN'],
9
+ address: ENV['MAIL_ADDRESS'],
10
+ port: ENV['MAIL_PORT'] || 587,
11
+ authentication: ENV['MAIL_AUTHENTICATION'] || 'plain',
12
+ enable_starttls_auto: (ENV['MAIL_TLS'] ? true : false)
13
+ }
14
+ config.default_from = nil
15
+ config.log = false
16
+ end
@@ -0,0 +1,39 @@
1
+ module IdleMailer
2
+ # Module to include in your mailer classes. Example:
3
+ #
4
+ # class WidgetMailer
5
+ # include IdleMailer::Mailer
6
+ #
7
+ # def initialize(address, widget)
8
+ # mail.to = address
9
+ # mail.subject = "Widget #{widget.sku}"
10
+ # # See mail gem docs for more options - http://www.rubydoc.info/gems/mail
11
+ # @widget = widget
12
+ # end
13
+ # end
14
+ #
15
+ # # Name your templates "widget.html.erb" and "widget.text.erb". They'll have access to "@widget".
16
+ #
17
+ # mailer = WidgetMailer.new(current_user.email, widget)
18
+ # mailer.deliver
19
+ #
20
+ module Mailer
21
+ # Deliver mail
22
+ def deliver
23
+ mailer = IdleMailer::Message.new(mail, self)
24
+ mailer.deliver!
25
+ end
26
+
27
+ # Render an ERB template with the mailer's binding
28
+ def render(template)
29
+ template.result(binding { yield })
30
+ end
31
+
32
+ private
33
+
34
+ # Mail config object
35
+ def mail
36
+ @mail ||= Mail.new
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,71 @@
1
+ module IdleMailer
2
+ # Renders templates and delivers message
3
+ class Message
4
+ # Mail object to deliver
5
+ attr_reader :mail
6
+ # The IdleMailer::Mailer object
7
+ attr_reader :mailer
8
+
9
+ # Initialize a new message
10
+ def initialize(mail, mailer)
11
+ @mail = mail
12
+ @mailer = mailer
13
+ end
14
+
15
+ # Deliver mail
16
+ def deliver!
17
+ log_to_stdout if IdleMailer.config.log
18
+ mail.from IdleMailer.config.default_from if mail.from.nil?
19
+ if has_template? 'html'
20
+ html_body = layout('html') { body('html') }
21
+ mail.html_part do
22
+ content_type 'text/html; charset=UTF-8'
23
+ body html_body
24
+ end
25
+ end
26
+ if has_template? 'text'
27
+ text_body = layout('text') { body('text') }
28
+ mail.text_part { body text_body }
29
+ end
30
+ mail.delivery_method IdleMailer.config.delivery_method, IdleMailer.config.delivery_options
31
+ mail.deliver
32
+ end
33
+
34
+ private
35
+
36
+ def body(type)
37
+ render template_path(template_name, type)
38
+ end
39
+
40
+ def layout(type)
41
+ has_layout?(type) ? render(template_path(IdleMailer.config.layout, type)) { yield } : yield
42
+ end
43
+
44
+ def render(path)
45
+ mailer.render(ERB.new(File.read(path))) { yield }
46
+ end
47
+
48
+ def has_template?(type)
49
+ File.exists? template_path(template_name, type)
50
+ end
51
+
52
+ def has_layout?(type)
53
+ File.exists? template_path(IdleMailer.config.layout, type)
54
+ end
55
+
56
+ def template_name
57
+ @name ||= mailer.class.name.sub(/Mailer$/, '').gsub(/([a-z])([A-Z])/, "\\1_\\2").downcase
58
+ end
59
+
60
+ def template_path(name, type)
61
+ IdleMailer.config.templates.join("#{name}.#{type}.erb")
62
+ end
63
+
64
+ def log_to_stdout
65
+ $stdout.puts [mail.to, mail.cc, mail.bcc].flatten.compact.join('; ')
66
+ $stdout.puts mail.subject
67
+ $stdout.puts(layout('html') { body('html') }) if has_template? 'html'
68
+ $stdout.puts(layout('text') { body('text') }) if has_template? 'text'
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ # IdleMailer module
2
+ module IdleMailer
3
+ # Version number
4
+ VERSION = '0.0.1'
5
+ end
data/lib/idlemailer.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'erb'
2
+ require 'pathname'
3
+ require 'mail'
4
+ require 'idlemailer/config'
5
+ require 'idlemailer/defaults'
6
+ require 'idlemailer/message'
7
+ require 'idlemailer/mailer'
8
+ require 'idlemailer/version'
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idlemailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Hollinger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Provides non-Rails projects with a lightweight ActionMailer-like wrapper
28
+ around the mail gem
29
+ email: jordan@jordanhollinger.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/idlemailer.rb
37
+ - lib/idlemailer/config.rb
38
+ - lib/idlemailer/defaults.rb
39
+ - lib/idlemailer/mailer.rb
40
+ - lib/idlemailer/message.rb
41
+ - lib/idlemailer/version.rb
42
+ homepage: https://github.com/jhollinger/idlemailer
43
+ licenses: []
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.4.5
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: A lightweight alternative to ActionMailer
65
+ test_files: []