idlemailer 1.0.0 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 347bc7e19b8427031ed9246730a710ca7ad8893f
4
- data.tar.gz: 4e44c6a7525787ab0576655f863fa31b5d1359ff
3
+ metadata.gz: 026e7c3207e3a4d68ad8788514da4df57fb38c7c
4
+ data.tar.gz: 1ba8967dd1cea7dbb1615083c542dc75371220c5
5
5
  SHA512:
6
- metadata.gz: 707144b2594688f8b838dc2eb3aa35f553c2f9bc188a0be8f6f812ccc7e801005a1068691ca8cf54d699564037e4a5fe0bb0f8e4c491425915dedc3e2ae8ef28
7
- data.tar.gz: a349486802b1245cf60dd885b85dfbbb74ea266ee077a0ff0f1b366d864fb2004fd7718ecac63d674ce2bf667d8c2f64dced20935c78aae4f9371fd8a1f931e2
6
+ metadata.gz: 33eabc8cf62e1a2854b56d330150b2e7f5525ea04640e03455eecc256140af4dfa999f30b714e171875db1a2fc2bc7c350da0869445b455389e4e118817356d6
7
+ data.tar.gz: '0294ef2f3f64dce15c02c787f0b1fb0234ca7c902c85a1f5162d4236c2ccf59f1dca1bdce1c96f7b2900c610d04bc4f9d33e2513221c066f33ddf164bdf1b15e'
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # IdleMailer
2
2
 
3
- A lightweight (~100 line) 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.
3
+ A lightweight (~150 line) 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
4
 
5
5
  ## Installation
6
6
 
@@ -31,6 +31,34 @@ mailer = WidgetMailer.new(current_user, widget)
31
31
  mailer.deliver
32
32
  ```
33
33
 
34
+ ### Inline templates
35
+
36
+ Instead of creating template files, you can embed your ERB templates right inside your Ruby class.
37
+
38
+ ```ruby
39
+ class WidgetMailer
40
+ include IdleMailer::Mailer
41
+
42
+ def initialize(user, widget)
43
+ mail.to = user.email
44
+ mail.subject = "Widget #{widget.sku}"
45
+ @widget = widget
46
+ end
47
+
48
+ text %(
49
+ A new widget called <%= @widget %> was just created!
50
+
51
+ Thanks!
52
+ )
53
+
54
+ html %(
55
+ <p>A new widget called <%= @widget %> was just created!</p>
56
+
57
+ <p>Thanks!</p>
58
+ )
59
+ end
60
+ ```
61
+
34
62
  ## Configure
35
63
 
36
64
  These are the default options. Salt to taste.
@@ -40,6 +68,9 @@ IdleMailer.config do |config|
40
68
  # Directory containing the mailer templates
41
69
  config.templates = Pathname.new(Dir.getwd).join('templates')
42
70
 
71
+ # Pre-cache all templates and layouts (instead of re-loading them on each delivery)
72
+ config.cache_templates = true
73
+
43
74
  # Name of the layout template. Here, the file(s) would be named
44
75
  # mailer_layout.html.erb and/or mailer_layout.text.erb.
45
76
  config.layout = 'mailer_layout'
@@ -7,7 +7,7 @@ module IdleMailer
7
7
  # default_from Default "from" address if it's left blank
8
8
  # logger a Logger object for logging email
9
9
  # log_body if true, the entire message body will be logged (instead of just the headers) (default false)
10
- Config = Struct.new(:templates, :layout, :delivery_method, :delivery_options, :default_from, :logger, :log_body)
10
+ Config = Struct.new(:templates, :layout, :cache_templates, :delivery_method, :delivery_options, :default_from, :logger, :log_body)
11
11
  @config = Config.new
12
12
 
13
13
  # Takes a block and hands it an IdleMailer::Config object
@@ -1,6 +1,7 @@
1
1
  IdleMailer.config do |config|
2
2
  config.templates = Pathname.new(Dir.getwd).join('templates')
3
3
  config.layout = 'mailer_layout'
4
+ config.cache_templates = true
4
5
  config.delivery_method = :smtp
5
6
  config.delivery_options = {
6
7
  user_name: ENV['MAIL_USER'],
@@ -18,6 +18,10 @@ module IdleMailer
18
18
  # mailer.deliver
19
19
  #
20
20
  module Mailer
21
+ def self.included(klass)
22
+ klass.extend TemplateManager
23
+ end
24
+
21
25
  # Deliver mail
22
26
  def deliver
23
27
  mailer = IdleMailer::Message.new(mail, self)
@@ -13,17 +13,19 @@ module IdleMailer
13
13
 
14
14
  # Deliver mail
15
15
  def deliver!
16
- if has_template? 'html'
16
+ if mailer.class.has_template? 'html'
17
17
  html_body = layout('html') { body('html') }
18
18
  mail.html_part do
19
19
  content_type 'text/html; charset=UTF-8'
20
20
  body html_body
21
21
  end
22
22
  end
23
- if has_template? 'text'
23
+
24
+ if mailer.class.has_template? 'text'
24
25
  text_body = layout('text') { body('text') }
25
26
  mail.text_part { body text_body }
26
27
  end
28
+
27
29
  config = IdleMailer.config
28
30
  mail.from config.default_from if mail.from.nil?
29
31
  mail.delivery_method config.delivery_method, config.delivery_options
@@ -35,35 +37,15 @@ module IdleMailer
35
37
  private
36
38
 
37
39
  def body(type)
38
- render template_path(template_name, type)
40
+ render mailer.class.template(type)
39
41
  end
40
42
 
41
43
  def layout(type)
42
- has_layout?(type) ? render(template_path(IdleMailer.config.layout, type)) { yield } : yield
43
- end
44
-
45
- def render(path)
46
- mailer.render(ERB.new(File.read(path))) { yield }
47
- end
48
-
49
- def has_template?(type)
50
- File.exists? template_path(template_name, type)
51
- end
52
-
53
- def has_layout?(type)
54
- File.exists? template_path(IdleMailer.config.layout, type)
55
- end
56
-
57
- def template_name
58
- @name ||= mailer.class.name.
59
- gsub(/::/, File::SEPARATOR).
60
- sub(/Mailer$/, '').
61
- gsub(/([a-z])([A-Z])/, "\\1_\\2").
62
- downcase
44
+ mailer.class.has_layout?(type) ? render(mailer.class.layout(type)) { yield } : yield
63
45
  end
64
46
 
65
- def template_path(name, type)
66
- IdleMailer.config.templates.join("#{name}.#{type}.erb")
47
+ def render(template)
48
+ mailer.render(template) { yield }
67
49
  end
68
50
  end
69
51
  end
@@ -0,0 +1,56 @@
1
+ module IdleMailer
2
+ module TemplateManager
3
+ def self.extended(klass)
4
+ klass.class_eval do
5
+ class << self
6
+ attr_reader :layouts, :templates
7
+ end
8
+ @layouts, @templates = {}, {}
9
+ end
10
+ klass.cache_templates! if IdleMailer.config.cache_templates
11
+ end
12
+
13
+ def text(str)
14
+ templates['text'] = ERB.new str
15
+ end
16
+
17
+ def html(str)
18
+ templates['html'] = ERB.new str
19
+ end
20
+
21
+ def template(type)
22
+ templates[type] || ERB.new(template_path(template_name, type).read)
23
+ end
24
+
25
+ def layout(type)
26
+ layouts[type] || ERB.new(template_path(IdleMailer.config.layout, type).read)
27
+ end
28
+
29
+ def has_template?(type)
30
+ templates.has_key?(type) || template_path(template_name, type).exist?
31
+ end
32
+
33
+ def has_layout?(type)
34
+ layouts.has_key?(type) || template_path(IdleMailer.config.layout, type).exist?
35
+ end
36
+
37
+ def template_name
38
+ @name ||= self.name.
39
+ gsub(/::/, File::SEPARATOR).
40
+ sub(/Mailer$/, '').
41
+ gsub(/([a-z])([A-Z])/, "\\1_\\2").
42
+ downcase
43
+ end
44
+
45
+ def template_path(name, type)
46
+ IdleMailer.config.templates.join("#{name}.#{type}.erb")
47
+ end
48
+
49
+ def cache_templates!
50
+ layouts['text'] = layout 'text' if has_layout? 'text'
51
+ layouts['html'] = layout 'html' if has_layout? 'html'
52
+ templates['text'] = template 'text' if has_template? 'text'
53
+ templates['html'] = template 'html' if has_template? 'html'
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  # IdleMailer module
2
2
  module IdleMailer
3
3
  # Version number
4
- VERSION = '1.0.0'
4
+ VERSION = '2.0.0.rc1'
5
5
  end
data/lib/idlemailer.rb CHANGED
@@ -5,4 +5,5 @@ require 'idlemailer/config'
5
5
  require 'idlemailer/defaults'
6
6
  require 'idlemailer/message'
7
7
  require 'idlemailer/mailer'
8
+ require 'idlemailer/template_manager'
8
9
  require 'idlemailer/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idlemailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-03 00:00:00.000000000 Z
11
+ date: 2017-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -38,6 +38,7 @@ files:
38
38
  - lib/idlemailer/defaults.rb
39
39
  - lib/idlemailer/mailer.rb
40
40
  - lib/idlemailer/message.rb
41
+ - lib/idlemailer/template_manager.rb
41
42
  - lib/idlemailer/version.rb
42
43
  homepage: https://github.com/jhollinger/idlemailer
43
44
  licenses: []
@@ -53,14 +54,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
54
  version: '0'
54
55
  required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  requirements:
56
- - - ">="
57
+ - - ">"
57
58
  - !ruby/object:Gem::Version
58
- version: '0'
59
+ version: 1.3.1
59
60
  requirements: []
60
61
  rubyforge_project:
61
- rubygems_version: 2.4.5.1
62
+ rubygems_version: 2.5.2
62
63
  signing_key:
63
64
  specification_version: 4
64
65
  summary: A lightweight alternative to ActionMailer
65
66
  test_files: []
66
- has_rdoc: