multiparterb 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 251b6ea729b75bd4bf083c6c5f25e84ed0d40ed4
4
+ data.tar.gz: 87ccb68f99ac3df5071dc7fbcf145174eafbdd97
5
+ SHA512:
6
+ metadata.gz: 0c36a217f12d60c582e9cd4db64c48bbc7e5c6aa9fa253c7aa086976ff33a28f4ea0f0ff84f899bbd1227b5afab3dc278a9378765145aa44aedeb9fdfb65f9c9
7
+ data.tar.gz: b24e28a6fb3116f3a816d3cf1999d792fb362b1e7486e33665e9ceaf0a4975c13e2f413e49d2508e66b12fd6c57a7f7722d085d5df4fb93ae5d3bdc5b2f97715
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011-2014 Econsultancy
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
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # MultipartErb
2
+
3
+ **MultipartErb** allows you to render multipart e-mails from a single template.
4
+
5
+ The template is written in HTML with a reduced set of HTML elements,
6
+ which is then parsed and formatted through user provided formatters,
7
+ which then renders and delivers as both text and HTML parts.
8
+
9
+ ## Installation
10
+
11
+ Add it into your `Gemfile`
12
+
13
+ ```ruby
14
+ gem 'multiparterb'
15
+ ```
16
+
17
+ Install it
18
+
19
+ ```console
20
+ bundle install
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### Templates
26
+
27
+ You create one erb template file written in HTML with a reduced set of elements.
28
+
29
+ Example `app/views/notifier/contact.multipart` :
30
+
31
+ ```erb
32
+ <h1>Heading with a link to <a href="https://example.com">Example</a></h1>
33
+ <p>body with a link to <a href="https://econsultancy.com">Econsultancy</a></p>
34
+ <a href="http://example.com">a link with a <h1>heading</h1> in</a>
35
+ ```
36
+
37
+ ### Formatters
38
+
39
+ You then have to create and provide one or more formatters for MultipartErb to call on each element it finds.
40
+
41
+ This is derived from a base class `BaseFormatter` provided by MultipartErb, any undefined methods that get called will raise a `NotImplementedError`.
42
+
43
+ This example formats the elements as it finds them, standard HTML output. _(TODO: Maybe provide this as standard?)_
44
+
45
+ ```ruby
46
+ class MyHTMLFormatter < BaseFormatter
47
+ def email_heading(text)
48
+ content_tag :h1, text
49
+ end
50
+
51
+ def email_text(text=nil, &block)
52
+ content_tag :p, super
53
+ end
54
+
55
+ def anchor(text, href)
56
+ content_tag(:a, text, href: href)
57
+ end
58
+ end
59
+ ```
60
+
61
+ And here is an example text formatter.
62
+
63
+ ```ruby
64
+ class MyTextFormatter < BaseFormatter
65
+ def email_heading(text)
66
+ "*** #{text} ***\n"
67
+ end
68
+
69
+ def email_text(text=nil, &block)
70
+ text + "\n"
71
+ end
72
+
73
+ def anchor(text, href)
74
+ text + ' (' + href + ')'
75
+ end
76
+ end
77
+ ```
78
+
79
+ Then you need to tell MultipartErb which formatters you want it to use.
80
+
81
+ ```ruby
82
+ MultipartErb.html_formatter = MyHTMLFormatter.new
83
+ MultipartErb.text_formatter = MyTextFormatter.new
84
+ ```
85
+
86
+ It will then call the relevent method for each element it finds in the template.
87
+
88
+ The set of elements this currently supports, and the method that will get called are :
89
+
90
+ * `<h1></h1>` => `email_heading`
91
+ * `<p></p>` => `email_text`
92
+ * `<a href="https://example.com">example</a>` => `anchor`
93
+
94
+ ### Mailers
95
+
96
+ Then your mailers work as normal, and you still get control over which parts you want generated :
97
+
98
+ ```ruby
99
+ class Notifier < ActionMailer::Base
100
+ def contact(recipient)
101
+ @recipient = recipient
102
+ mail(:to => @recipient, :from => "john.doe@example.com") do |format|
103
+ format.text
104
+ format.html
105
+ end
106
+ end
107
+ end
108
+ ```
109
+
110
+ It will generate two parts, one in text and another in html when delivered.
111
+
112
+ If you only wanted to text or html part, then just remove the `format` you don't want.
113
+
114
+
115
+ ### Output
116
+
117
+ For the above given example, the resulted output would be as such :
118
+
119
+ Text version
120
+
121
+ ```
122
+ *** Heading with a link to Econsultancy (https://econsultancy.com) ***
123
+ body with a link to Econsultancy (https://econsultancy.com)
124
+ a link with a *** heading *** in (http://example.com)"
125
+ ```
126
+
127
+ Html version
128
+
129
+ ```
130
+ <h1>Heading with a link to <a href=\"https://econsultancy.com\">Econsultancy</a></h1>
131
+ <p>body with a link to <a href=\"https://econsultancy.com\">Econsultancy</a></p>
132
+ <a href=\"http://example.com\">a link with a <h1>heading</h1> in</a>
133
+ ```
134
+
135
+ ## Notes
136
+
137
+ * The `contact.multipart` template should not have a format in its name. Adding a format would make it unavailable to be rendered in different formats;
138
+
139
+ * The order of the parts matter. It is important for e-mail clients that you call `format.text` before you call `format.html`;
140
+
141
+ * you can normally use ERb inside the template.
142
+
143
+
144
+ ## Bug reports
145
+
146
+ If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as
147
+ possible to help us fixing the possible bug. We also encourage you to help even more by forking and
148
+ sending us a pull request.
149
+
150
+ https://github.com/econsultancy/multiparterb/issues
151
+
152
+ ## Maintainers
153
+
154
+ * Ian Vaughan (https://github.com/IanVaughan)
155
+
156
+ ## Thanks
157
+
158
+ This gem was derived from [Markerb](https://github.com/plataformatec/markerb).
159
+
160
+ ## License
161
+
162
+ MIT License. Copyright 2011-2014 Econsultancy
@@ -0,0 +1,23 @@
1
+ require "rails/generators/erb/mailer/mailer_generator"
2
+
3
+ module MultipartErb
4
+ module Generators
5
+ class MailerGenerator < Erb::Generators::MailerGenerator
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ protected
9
+
10
+ def format
11
+ nil # Our templates have no format
12
+ end
13
+
14
+ def formats
15
+ [format]
16
+ end
17
+
18
+ def handler
19
+ :multipart
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ <%= class_name %>#<%= @action %>
2
+
3
+ <%%= @greeting %>, find me in app/views/<%= @path %>
@@ -0,0 +1,27 @@
1
+ require "action_view"
2
+ require "action_view/template"
3
+ require "multiparterb/formatter"
4
+ require "multiparterb/railtie"
5
+ require "multiparterb/formatters/base_formatter"
6
+
7
+ require 'nokogiri'
8
+
9
+ module MultipartErb
10
+ class Handler
11
+ def erb_handler
12
+ @erb_handler ||= ActionView::Template.registered_template_handler(:erb)
13
+ end
14
+
15
+ def call(template)
16
+ compiled_source = erb_handler.call(template)
17
+
18
+ if template.formats.include?(:html)
19
+ "MultipartErb::Formatter.to_html(begin;#{compiled_source};end).html_safe"
20
+ else
21
+ "MultipartErb::Formatter.to_text(begin;#{compiled_source};end).html_safe"
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ ActionView::Template.register_template_handler(:multipart, MultipartErb::Handler.new)
@@ -0,0 +1,40 @@
1
+ module MultipartErb
2
+ mattr_accessor :html_formatter, :text_formatter
3
+ @@html_formatter = nil
4
+ @@text_formatter = nil
5
+
6
+ class Formatter
7
+ def self.parse(node, formatter)
8
+ result = ""
9
+
10
+ node.children.each do |child|
11
+ result << case child.name
12
+ when 'h1'
13
+ formatter.email_heading(parse(child, formatter).html_safe)
14
+ when 'p'
15
+ formatter.email_text(parse(child, formatter).html_safe)
16
+ when 'a'
17
+ formatter.anchor(
18
+ parse(child, formatter).html_safe,
19
+ child.attributes['href'].content)
20
+ when 'text'
21
+ child.text
22
+ else
23
+ parse(child, formatter)
24
+ end
25
+ end
26
+
27
+ result
28
+ end
29
+
30
+ def self.to_html(compiled_source)
31
+ html_doc = Nokogiri::HTML compiled_source
32
+ parse html_doc, MultipartErb.html_formatter
33
+ end
34
+
35
+ def self.to_text(compiled_source)
36
+ html_doc = Nokogiri::HTML compiled_source
37
+ parse html_doc, MultipartErb.text_formatter
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ class BaseFormatter
2
+ include ActionView::Helpers::TagHelper
3
+ include ActionView::Context
4
+
5
+ def email_heading(text)
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def email_text(text=nil, &block)
10
+ text || capture(&block)
11
+ end
12
+
13
+ def anchor(text, href)
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def unordered_list(text)
18
+ raise NotImplementedError
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module MultipartErb
2
+ class Railtie < ::Rails::Railtie
3
+ config.app_generators.mailer :template_engine => :multipart
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module MultipartErb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ require "test_helper"
2
+
3
+ # TODO: The idea will be to require the user to provide this as a callback
4
+ class FormatterTest < ActiveSupport::TestCase
5
+ setup do
6
+ @template = formatted_html_output %{
7
+ <h1>Heading with a link to <a href="https://econsultancy.com">Econsultancy</a></h1>
8
+ <p>body with a link to <a href="https://econsultancy.com">Econsultancy</a></p>
9
+ <a href="http://example.com">a link with a <h1>heading</h1> in</a>
10
+ }
11
+ end
12
+
13
+ test 'formatted html output' do
14
+ html = formatted_html_output %{
15
+ <h1>Heading with a link to <a href=\"https://econsultancy.com\">Econsultancy</a></h1>
16
+ <p>body with a link to <a href=\"https://econsultancy.com\">Econsultancy</a></p>
17
+ <a href=\"http://example.com\">a link with a <h1>heading</h1> in</a>
18
+ }
19
+ assert_equal html, MultipartErb::Formatter.to_html(@template).strip
20
+ end
21
+
22
+ test 'formatted text output' do
23
+ text = "Heading with a link to Econsultancy (https://econsultancy.com)
24
+ --------------------------------------------------------------
25
+ body with a link to Econsultancy (https://econsultancy.com)
26
+ a link with a heading
27
+ -------
28
+ in (http://example.com)"
29
+ assert_equal text, MultipartErb::Formatter.to_text(@template).strip
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ class MyHTMLFormatter < BaseFormatter
2
+ def email_heading(text)
3
+ content_tag :h1, text
4
+ end
5
+
6
+ def email_text(text=nil, &block)
7
+ content_tag :p, super
8
+ end
9
+
10
+ def anchor(text, href)
11
+ content_tag(:a, text, href: href)
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class MyTextFormatter < BaseFormatter
2
+ def email_heading(text)
3
+ text + "\n" + ("-" * text.length) + "\n"
4
+ end
5
+
6
+ def email_text(text=nil, &block)
7
+ text + "\n"
8
+ end
9
+
10
+ def anchor(text, href)
11
+ text + ' (' + href + ')'
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require "test_helper"
2
+ require "generators/multiparterb/mailer/mailer_generator"
3
+
4
+ class GeneratorTest < Rails::Generators::TestCase
5
+ tests MultipartErb::Generators::MailerGenerator
6
+ destination File.expand_path("../tmp", __FILE__)
7
+ setup :prepare_destination
8
+
9
+ test "assert all views are properly created with given name" do
10
+ run_generator %w(notifier foo bar baz)
11
+
12
+ # TODO: would be nice to have .erb as the pre-processor in the file name
13
+ # assert_file "app/views/notifier/foo.multipart.erb"
14
+ assert_file "app/views/notifier/foo.multipart"
15
+ assert_file "app/views/notifier/bar.multipart"
16
+ assert_file "app/views/notifier/baz.multipart"
17
+ end
18
+ end
@@ -0,0 +1,78 @@
1
+ require "test_helper"
2
+
3
+ class Notifier < ActionMailer::Base
4
+ self.view_paths = File.expand_path("../views", __FILE__)
5
+
6
+ layout false
7
+
8
+ # TODO: want this to output both html and text, unless gem option given to give preferance
9
+ # mail(:to => @recipient, :from => "john.doe@example.com")
10
+
11
+ def contact(recipient, format_type)
12
+ @recipient = recipient
13
+ mail(:to => @recipient, :from => "john.doe@example.com") do |format|
14
+ format.send(format_type)
15
+ end
16
+ end
17
+
18
+ def link(format_type)
19
+ mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
20
+ format.send(format_type)
21
+ end
22
+ end
23
+
24
+ def user(format_type)
25
+ mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
26
+ format.send(format_type)
27
+ end
28
+ end
29
+
30
+ def multiple_format_contact(recipient)
31
+ @recipient = recipient
32
+ mail(:to => @recipient, :from => "john.doe@example.com", :template => "contact") do |format|
33
+ format.text { render 'contact' }
34
+ format.html { render 'contact' }
35
+ end
36
+ end
37
+ end
38
+
39
+ class MultipartErbTest < ActiveSupport::TestCase
40
+ test "plain text should be sent as a plain text" do
41
+ email = Notifier.contact("you@example.com", :text)
42
+ assert_equal "text/plain", email.mime_type
43
+ assert_equal false, email.multipart?
44
+ #assert_equal "Contact Heading\r\n---------------\r\n\r\n", email.body.encoded.strip
45
+ assert_equal "Contact Heading\n---------------\n\n", email.body.raw_source
46
+ end
47
+
48
+ test "html should be sent as html" do
49
+ email = Notifier.contact("you@example.com", :html)
50
+ assert_equal "text/html", email.mime_type
51
+ assert_equal false, email.multipart?
52
+ #assert_equal "Contact Heading\r\n---------------\r\n\r\n", email.body.encoded.strip
53
+ assert_equal "<h1>Contact Heading</h1>", email.body.encoded.strip
54
+ end
55
+
56
+ test 'dealing with multipart e-mails' do
57
+ email = Notifier.multiple_format_contact("you@example.com")
58
+ assert_equal 2, email.parts.size
59
+ assert_equal true, email.multipart?
60
+ assert_equal "multipart/alternative", email.mime_type
61
+ assert_equal "text/plain", email.parts[0].mime_type
62
+ assert_equal "Contact Heading\n---------------\n\n", email.parts[0].body.raw_source
63
+ assert_equal "text/html", email.parts[1].mime_type
64
+ assert_equal "<h1>Contact Heading</h1>", email.parts[1].body.encoded.strip
65
+ end
66
+
67
+ test 'with link' do
68
+ email = Notifier.link(:html)
69
+ assert_equal "text/html", email.mime_type
70
+ assert_equal "<p>A link to <a href=\"https://econsultancy.com\">Econsultancy</a></p>", email.body.encoded.strip
71
+ end
72
+
73
+ test 'with partial' do
74
+ email = Notifier.user(:html)
75
+ assert_equal "text/html", email.mime_type
76
+ #assert_equal '<p>User template rendering a partial User Info Partial</p>', email.body.encoded.strip
77
+ end
78
+ end
@@ -0,0 +1,24 @@
1
+ require 'pry'
2
+
3
+ require "minitest/autorun"
4
+ require "active_support/test_case"
5
+
6
+ require "action_mailer"
7
+ require "rails/railtie"
8
+ require "rails/generators"
9
+ require "rails/generators/test_case"
10
+
11
+ $:.unshift File.expand_path("../../lib", __FILE__)
12
+ require "multiparterb"
13
+ require "formatters/my_text_formatter"
14
+ require "formatters/my_html_formatter"
15
+
16
+ # Avoid annoying warning from I18n.
17
+ I18n.enforce_available_locales = false
18
+
19
+ def formatted_html_output(text)
20
+ text.gsub(/\n +/, '')
21
+ end
22
+
23
+ MultipartErb.html_formatter = MyHTMLFormatter.new
24
+ MultipartErb.text_formatter = MyTextFormatter.new
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multiparterb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ian Vaughan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Multipart templates made easy
42
+ email: ian.vaughan@econsultancy.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - MIT-LICENSE
48
+ - README.md
49
+ - lib/generators/multiparterb/mailer/mailer_generator.rb
50
+ - lib/generators/multiparterb/mailer/templates/view.multipart
51
+ - lib/multiparterb/formatter.rb
52
+ - lib/multiparterb/formatters/base_formatter.rb
53
+ - lib/multiparterb/railtie.rb
54
+ - lib/multiparterb/version.rb
55
+ - lib/multiparterb.rb
56
+ - test/formatter_test.rb
57
+ - test/formatters/my_html_formatter.rb
58
+ - test/formatters/my_text_formatter.rb
59
+ - test/generator_test.rb
60
+ - test/multiparterb_test.rb
61
+ - test/test_helper.rb
62
+ homepage: http://github.com/
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.0.14
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Multipart templates made easy
86
+ test_files:
87
+ - test/formatter_test.rb
88
+ - test/formatters/my_html_formatter.rb
89
+ - test/formatters/my_text_formatter.rb
90
+ - test/generator_test.rb
91
+ - test/multiparterb_test.rb
92
+ - test/test_helper.rb