multiparterb 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -16
- data/lib/multiparterb/formatter.rb +20 -12
- data/lib/multiparterb/formatters/base_formatter.rb +4 -18
- data/lib/multiparterb/version.rb +1 -1
- data/test/base_formatter_test.rb +19 -0
- data/test/formatter_test.rb +5 -19
- data/test/formatters/my_html_formatter.rb +12 -4
- data/test/formatters/my_text_formatter.rb +11 -3
- data/test/multiparterb_test.rb +4 -4
- metadata +24 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12bde72054a4d9073d27de1b397a85e2320c3bd9
|
4
|
+
data.tar.gz: 7c88e13a2e99ec33c3ed02e89fc13c76ae0435d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3d6e2fbaba5260d66584402bfc5069e5dff3ff5f3e808ed8f9a6736fa8502e2fadc9eba3d8d2a383d3e1a73cd813a76edea306f3a0e60640abfcb899618ba36
|
7
|
+
data.tar.gz: 6830c84ea752685c21733fe3dfa5d663877e777d8f63d97d32e54d283c9538d2c86cf95d0cd865dce19bad5d38807d0676657f489490e47e8f85c1d472163be7
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# MultipartErb
|
1
|
+
# MultipartErb [![Build Status](https://travis-ci.org/econsultancy/multiparterb.svg?branch=master)](https://travis-ci.org/econsultancy/multiparterb) [![Gem Version](https://badge.fury.io/rb/multiparterb.svg)](http://badge.fury.io/rb/multiparterb)
|
2
2
|
|
3
3
|
**MultipartErb** allows you to render multipart e-mails from a single template.
|
4
4
|
|
@@ -43,13 +43,13 @@ This is derived from a base class `BaseFormatter` provided by MultipartErb, any
|
|
43
43
|
This example formats the elements as it finds them, standard HTML output. _(TODO: Maybe provide this as standard?)_
|
44
44
|
|
45
45
|
```ruby
|
46
|
-
class MyHTMLFormatter < BaseFormatter
|
46
|
+
class MyHTMLFormatter < MultipartErb::BaseFormatter
|
47
47
|
def heading(text)
|
48
|
-
content_tag
|
48
|
+
content_tag(:h1, text)
|
49
49
|
end
|
50
50
|
|
51
|
-
def
|
52
|
-
content_tag
|
51
|
+
def paragraph(text)
|
52
|
+
content_tag(:p, text)
|
53
53
|
end
|
54
54
|
|
55
55
|
def anchor(text, href)
|
@@ -61,13 +61,13 @@ end
|
|
61
61
|
And here is an example text formatter.
|
62
62
|
|
63
63
|
```ruby
|
64
|
-
class MyTextFormatter < BaseFormatter
|
64
|
+
class MyTextFormatter < MultipartErb::BaseFormatter
|
65
65
|
def heading(text)
|
66
66
|
"*** #{text} ***\n"
|
67
67
|
end
|
68
68
|
|
69
|
-
def
|
70
|
-
text
|
69
|
+
def paragraph(text)
|
70
|
+
text
|
71
71
|
end
|
72
72
|
|
73
73
|
def anchor(text, href)
|
@@ -79,6 +79,7 @@ end
|
|
79
79
|
Then you need to tell MultipartErb which formatters you want it to use.
|
80
80
|
|
81
81
|
```ruby
|
82
|
+
# ./config/initializers/multiparterb.rb
|
82
83
|
MultipartErb.html_formatter = MyHTMLFormatter.new
|
83
84
|
MultipartErb.text_formatter = MyTextFormatter.new
|
84
85
|
```
|
@@ -87,9 +88,12 @@ It will then call the relevent method for each element it finds in the template.
|
|
87
88
|
|
88
89
|
The set of elements this currently supports, and the method that will get called are :
|
89
90
|
|
90
|
-
* `<h1
|
91
|
-
* `<p
|
92
|
-
* `<a href="https://example.com">
|
91
|
+
* `<h1>text</h1>` => `Formatter#heading(text)`
|
92
|
+
* `<p>text</p>` => `Formatter#paragraph(text)`
|
93
|
+
* `<a href="https://example.com">text</a>` => `Formatter#anchor(text, href)`
|
94
|
+
* `<ul>text</ul>` => `Formatter#unordered_list(text)`
|
95
|
+
* `<li>text</li>` => `Formatter#list_item(text)`
|
96
|
+
* `<strong>text</strong>` => `Formatter#strong(text)`
|
93
97
|
|
94
98
|
### Mailers
|
95
99
|
|
@@ -111,7 +115,6 @@ It will generate two parts, one in text and another in html when delivered.
|
|
111
115
|
|
112
116
|
If you only wanted to text or html part, then just remove the `format` you don't want.
|
113
117
|
|
114
|
-
|
115
118
|
### Output
|
116
119
|
|
117
120
|
For the above given example, the resulted output would be as such :
|
@@ -136,10 +139,7 @@ Html version
|
|
136
139
|
|
137
140
|
* 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
141
|
|
139
|
-
*
|
140
|
-
|
141
|
-
* you can normally use ERb inside the template.
|
142
|
-
|
142
|
+
* You can use ERb inside the template, which gets parsed before the formatting occurs.
|
143
143
|
|
144
144
|
## Bug reports
|
145
145
|
|
@@ -3,6 +3,16 @@ module MultipartErb
|
|
3
3
|
@@html_formatter = nil
|
4
4
|
@@text_formatter = nil
|
5
5
|
|
6
|
+
SUPPORTED_TAG_METHODS = {
|
7
|
+
# HTML tag => method called
|
8
|
+
'h1' => :heading,
|
9
|
+
'p' => :paragraph,
|
10
|
+
'ul' => :unordered_list,
|
11
|
+
'li' => :list_item,
|
12
|
+
'strong' => :strong,
|
13
|
+
'a' => :anchor,
|
14
|
+
}
|
15
|
+
|
6
16
|
class Formatter
|
7
17
|
def self.parse(node, formatter)
|
8
18
|
"".tap do |result|
|
@@ -13,19 +23,17 @@ module MultipartErb
|
|
13
23
|
end
|
14
24
|
|
15
25
|
def self.lookup(child, formatter)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
when 'text'
|
26
|
-
child.text
|
26
|
+
return child.text if child.name == 'text'
|
27
|
+
parsed_text = parse(child, formatter).html_safe
|
28
|
+
|
29
|
+
if SUPPORTED_TAG_METHODS.keys.include?(child.name)
|
30
|
+
if child.name == 'a'
|
31
|
+
formatter.public_send(SUPPORTED_TAG_METHODS[child.name], parsed_text, child.attributes['href'].content)
|
32
|
+
else
|
33
|
+
formatter.public_send(SUPPORTED_TAG_METHODS[child.name], parsed_text)
|
34
|
+
end
|
27
35
|
else
|
28
|
-
|
36
|
+
parsed_text
|
29
37
|
end
|
30
38
|
end
|
31
39
|
|
@@ -1,20 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def heading(text)
|
6
|
-
raise NotImplementedError
|
7
|
-
end
|
8
|
-
|
9
|
-
def 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
|
1
|
+
module MultipartErb
|
2
|
+
class BaseFormatter
|
3
|
+
include ActionView::Helpers::TagHelper
|
4
|
+
include ActionView::Context
|
19
5
|
end
|
20
6
|
end
|
data/lib/multiparterb/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class BaseFormatterTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
TestFormatter = Class.new(MultipartErb::BaseFormatter)
|
6
|
+
MultipartErb.html_formatter = TestFormatter.new
|
7
|
+
end
|
8
|
+
|
9
|
+
teardown do
|
10
|
+
MultipartErb.html_formatter = MyHTMLFormatter.new
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'missing required formatter method' do
|
14
|
+
exception = assert_raises NoMethodError do
|
15
|
+
MultipartErb::Formatter.to_html('<h1>heading</h1>')
|
16
|
+
end
|
17
|
+
assert_equal "undefined method `heading' for #{MultipartErb.html_formatter}", exception.message
|
18
|
+
end
|
19
|
+
end
|
data/test/formatter_test.rb
CHANGED
@@ -1,31 +1,17 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
|
-
# TODO: The idea will be to require the user to provide this as a callback
|
4
3
|
class FormatterTest < ActiveSupport::TestCase
|
5
4
|
setup do
|
6
|
-
@template =
|
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
|
-
}
|
5
|
+
@template = File.read('./test/views/formatter/formatter.multipart')
|
11
6
|
end
|
12
7
|
|
13
8
|
test 'formatted html output' do
|
14
|
-
html =
|
15
|
-
|
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
|
9
|
+
html = File.read('./test/views/formatter/formatter.html')
|
10
|
+
assert_equal html, MultipartErb::Formatter.to_html(@template)
|
20
11
|
end
|
21
12
|
|
22
13
|
test 'formatted text output' do
|
23
|
-
text =
|
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
|
14
|
+
text = File.read('./test/views/formatter/formatter.text')
|
15
|
+
assert_equal text, MultipartErb::Formatter.to_text(@template)
|
30
16
|
end
|
31
17
|
end
|
@@ -1,13 +1,21 @@
|
|
1
|
-
class MyHTMLFormatter < BaseFormatter
|
1
|
+
class MyHTMLFormatter < MultipartErb::BaseFormatter
|
2
2
|
def heading(text)
|
3
|
-
content_tag
|
3
|
+
content_tag(:h1, text)
|
4
4
|
end
|
5
5
|
|
6
|
-
def
|
7
|
-
content_tag
|
6
|
+
def paragraph(text)
|
7
|
+
content_tag(:p, text)
|
8
8
|
end
|
9
9
|
|
10
10
|
def anchor(text, href)
|
11
11
|
content_tag(:a, text, href: href)
|
12
12
|
end
|
13
|
+
|
14
|
+
def unordered_list(text)
|
15
|
+
content_tag(:ul, text)
|
16
|
+
end
|
17
|
+
|
18
|
+
def list_item(text)
|
19
|
+
content_tag(:li, text)
|
20
|
+
end
|
13
21
|
end
|
@@ -1,13 +1,21 @@
|
|
1
|
-
class MyTextFormatter < BaseFormatter
|
1
|
+
class MyTextFormatter < MultipartErb::BaseFormatter
|
2
2
|
def heading(text)
|
3
3
|
text + "\n" + ("-" * text.length) + "\n"
|
4
4
|
end
|
5
5
|
|
6
|
-
def
|
7
|
-
text
|
6
|
+
def paragraph(text)
|
7
|
+
text
|
8
8
|
end
|
9
9
|
|
10
10
|
def anchor(text, href)
|
11
11
|
text + ' (' + href + ')'
|
12
12
|
end
|
13
|
+
|
14
|
+
def unordered_list(text)
|
15
|
+
text
|
16
|
+
end
|
17
|
+
|
18
|
+
def list_item(text)
|
19
|
+
"- #{text}"
|
20
|
+
end
|
13
21
|
end
|
data/test/multiparterb_test.rb
CHANGED
@@ -70,10 +70,10 @@ class MultipartErbTest < ActiveSupport::TestCase
|
|
70
70
|
assert_equal 2, email.parts.size
|
71
71
|
assert_equal true, email.multipart?
|
72
72
|
assert_equal "multipart/alternative", email.mime_type
|
73
|
-
assert_equal "text/
|
74
|
-
assert_equal "Welcome\
|
75
|
-
assert_equal "text/
|
76
|
-
assert_equal "
|
73
|
+
assert_equal "text/html", email.parts[0].mime_type
|
74
|
+
assert_equal "<p>Welcome\r\n</p>", email.parts[0].body.encoded.strip
|
75
|
+
assert_equal "text/plain", email.parts[1].mime_type
|
76
|
+
assert_equal "Welcome\n", email.parts[1].body.raw_source
|
77
77
|
end
|
78
78
|
|
79
79
|
test "with link" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multiparterb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian Vaughan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -24,22 +24,36 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.2.18
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.2.18
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: pry
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- - '
|
45
|
+
- - '='
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
47
|
+
version: 0.9.10
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- - '
|
52
|
+
- - '='
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
54
|
+
version: 0.9.10
|
41
55
|
description: Multipart templates made easy
|
42
|
-
email:
|
56
|
+
email: tech@econsultancy.com
|
43
57
|
executables: []
|
44
58
|
extensions: []
|
45
59
|
extra_rdoc_files: []
|
@@ -53,13 +67,14 @@ files:
|
|
53
67
|
- lib/multiparterb/railtie.rb
|
54
68
|
- lib/multiparterb/version.rb
|
55
69
|
- lib/multiparterb.rb
|
70
|
+
- test/base_formatter_test.rb
|
56
71
|
- test/formatter_test.rb
|
57
72
|
- test/formatters/my_html_formatter.rb
|
58
73
|
- test/formatters/my_text_formatter.rb
|
59
74
|
- test/generator_test.rb
|
60
75
|
- test/multiparterb_test.rb
|
61
76
|
- test/test_helper.rb
|
62
|
-
homepage: http://github.com/
|
77
|
+
homepage: http://github.com/econsultancy/multiparterb
|
63
78
|
licenses:
|
64
79
|
- MIT
|
65
80
|
metadata: {}
|
@@ -84,6 +99,7 @@ signing_key:
|
|
84
99
|
specification_version: 4
|
85
100
|
summary: Multipart templates made easy
|
86
101
|
test_files:
|
102
|
+
- test/base_formatter_test.rb
|
87
103
|
- test/formatter_test.rb
|
88
104
|
- test/formatters/my_html_formatter.rb
|
89
105
|
- test/formatters/my_text_formatter.rb
|