mjml-remote-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 54c05c64b5d9536df569f7040305cf75050d5a1d5651b3dadc325eec559c594a
4
+ data.tar.gz: a3f27b935d46494e65358b21bfd845f9bc8dad1173cc93475a394b77445f48ae
5
+ SHA512:
6
+ metadata.gz: e37fc5cda6d5d4d130a2457047eba9ffed24b7c3de555fe14b24dc2fb8a8b1eca2d36a91dbdb3f4d09c0e43857527610cd57c61ac115278dc629d74dcb008240
7
+ data.tar.gz: 231f742abf8a9457985b6e990440fa550724d6c6c28ba252a3412157f74ee595ac7221edca5a18fbd3c9f00e4829112c8c208d798541b647584d78796bd012ff
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mjml-remote-rails.gemspec
6
+ gemspec
@@ -0,0 +1,146 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mjml-remote-rails (0.1.0)
5
+ faraday (~> 0.12)
6
+ rails (~> 5.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.2.2)
12
+ actionpack (= 5.2.2)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailer (5.2.2)
16
+ actionpack (= 5.2.2)
17
+ actionview (= 5.2.2)
18
+ activejob (= 5.2.2)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.2.2)
22
+ actionview (= 5.2.2)
23
+ activesupport (= 5.2.2)
24
+ rack (~> 2.0)
25
+ rack-test (>= 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.2.2)
29
+ activesupport (= 5.2.2)
30
+ builder (~> 3.1)
31
+ erubi (~> 1.4)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
+ activejob (5.2.2)
35
+ activesupport (= 5.2.2)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.2.2)
38
+ activesupport (= 5.2.2)
39
+ activerecord (5.2.2)
40
+ activemodel (= 5.2.2)
41
+ activesupport (= 5.2.2)
42
+ arel (>= 9.0)
43
+ activestorage (5.2.2)
44
+ actionpack (= 5.2.2)
45
+ activerecord (= 5.2.2)
46
+ marcel (~> 0.3.1)
47
+ activesupport (5.2.2)
48
+ concurrent-ruby (~> 1.0, >= 1.0.2)
49
+ i18n (>= 0.7, < 2)
50
+ minitest (~> 5.1)
51
+ tzinfo (~> 1.1)
52
+ arel (9.0.0)
53
+ builder (3.2.3)
54
+ concurrent-ruby (1.1.4)
55
+ crass (1.0.4)
56
+ diff-lcs (1.3)
57
+ erubi (1.8.0)
58
+ faraday (0.15.4)
59
+ multipart-post (>= 1.2, < 3)
60
+ globalid (0.4.2)
61
+ activesupport (>= 4.2.0)
62
+ i18n (1.5.3)
63
+ concurrent-ruby (~> 1.0)
64
+ loofah (2.2.3)
65
+ crass (~> 1.0.2)
66
+ nokogiri (>= 1.5.9)
67
+ mail (2.7.1)
68
+ mini_mime (>= 0.1.1)
69
+ marcel (0.3.3)
70
+ mimemagic (~> 0.3.2)
71
+ method_source (0.9.2)
72
+ mimemagic (0.3.3)
73
+ mini_mime (1.0.1)
74
+ mini_portile2 (2.4.0)
75
+ minitest (5.11.3)
76
+ multipart-post (2.0.0)
77
+ nio4r (2.3.1)
78
+ nokogiri (1.10.1)
79
+ mini_portile2 (~> 2.4.0)
80
+ rack (2.0.6)
81
+ rack-test (1.1.0)
82
+ rack (>= 1.0, < 3)
83
+ rails (5.2.2)
84
+ actioncable (= 5.2.2)
85
+ actionmailer (= 5.2.2)
86
+ actionpack (= 5.2.2)
87
+ actionview (= 5.2.2)
88
+ activejob (= 5.2.2)
89
+ activemodel (= 5.2.2)
90
+ activerecord (= 5.2.2)
91
+ activestorage (= 5.2.2)
92
+ activesupport (= 5.2.2)
93
+ bundler (>= 1.3.0)
94
+ railties (= 5.2.2)
95
+ sprockets-rails (>= 2.0.0)
96
+ rails-dom-testing (2.0.3)
97
+ activesupport (>= 4.2.0)
98
+ nokogiri (>= 1.6)
99
+ rails-html-sanitizer (1.0.4)
100
+ loofah (~> 2.2, >= 2.2.2)
101
+ railties (5.2.2)
102
+ actionpack (= 5.2.2)
103
+ activesupport (= 5.2.2)
104
+ method_source
105
+ rake (>= 0.8.7)
106
+ thor (>= 0.19.0, < 2.0)
107
+ rake (10.5.0)
108
+ rspec (3.7.0)
109
+ rspec-core (~> 3.7.0)
110
+ rspec-expectations (~> 3.7.0)
111
+ rspec-mocks (~> 3.7.0)
112
+ rspec-core (3.7.1)
113
+ rspec-support (~> 3.7.0)
114
+ rspec-expectations (3.7.0)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.7.0)
117
+ rspec-mocks (3.7.0)
118
+ diff-lcs (>= 1.2.0, < 2.0)
119
+ rspec-support (~> 3.7.0)
120
+ rspec-support (3.7.1)
121
+ sprockets (3.7.2)
122
+ concurrent-ruby (~> 1.0)
123
+ rack (> 1, < 3)
124
+ sprockets-rails (3.2.1)
125
+ actionpack (>= 4.0)
126
+ activesupport (>= 4.0)
127
+ sprockets (>= 3.0.0)
128
+ thor (0.20.3)
129
+ thread_safe (0.3.6)
130
+ tzinfo (1.2.5)
131
+ thread_safe (~> 0.1)
132
+ websocket-driver (0.7.0)
133
+ websocket-extensions (>= 0.1.0)
134
+ websocket-extensions (0.1.3)
135
+
136
+ PLATFORMS
137
+ ruby
138
+
139
+ DEPENDENCIES
140
+ bundler (~> 1.17)
141
+ mjml-remote-rails!
142
+ rake (~> 10.0)
143
+ rspec (~> 3.0)
144
+
145
+ BUNDLED WITH
146
+ 1.17.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 ecoPortal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,88 @@
1
+ # mjml-remote-rails
2
+
3
+ Based on [mjml-rails](https://github.com/sighmon/mjml-rails/), this gem offloads
4
+ the actual MJML processing to a microservice (e.g a lambda function). If the
5
+ microservice returns an error, `Mjml::Remote::Template::ParseError` will be
6
+ raised.
7
+
8
+ See [mjml-lambda](https://gitlab.com/ecoPortal/mjml-lambda) for an example
9
+ lambda function that will do it.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'mjml-remote-rails'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install mjml-remote-rails
26
+
27
+ ## Configuring
28
+
29
+ ```ruby
30
+ Mjml::Remote.setup do |setup|
31
+ # Override template language - default is erb
32
+ config.template_language = :haml
33
+ # config.endpoint is required!
34
+ config.endpoint = "https://<some-server/<some-endpoint>"
35
+ # headers for the requests can be set - default is no headers
36
+ config.headers = { "x-api-key" => "some header based authentication" }
37
+ end
38
+ ```
39
+
40
+ ## ...why?!
41
+
42
+ Calling MJML from ruby is pretty heavy memory-wise and threw a major spanner in
43
+ the works of our heavily multi-threaded background job workers when lots of
44
+ emails were being processed. Installing the mjml binary also added a decent
45
+ amount of weight to our docker containers once you factored in the other
46
+ dependencies you need.
47
+
48
+ Moving the actual MJML processing to a lambda function means we don't need to
49
+ worry about this any more, and as all our email processing is backgrounded
50
+ anyway, it won't really make much of a difference to our users.
51
+
52
+ ## Falling back to regular MJML in development
53
+
54
+ `Gemfile`
55
+
56
+ ```ruby
57
+ group :production do
58
+ gem "mjml-remote-rails"
59
+ end
60
+ group :development do
61
+ gem "mjml-rails", require: false
62
+ gem "mjml-remote-rails", require: false
63
+ end
64
+ ```
65
+
66
+ `config/application.rb` at the top after requiring rails:
67
+
68
+ ```ruby
69
+ if ENV["MJML_ENDPOINT"]
70
+ require "mjml-remote-rails"
71
+ else
72
+ require "mjml-rails"
73
+ end
74
+ ```
75
+
76
+ `config/initializers/mjml.rb`
77
+
78
+ ```ruby
79
+ if Rails.env.production? || ENV["MJML_ENDPOINT"]
80
+ Mjml::Remote.setup do |config|
81
+ ...
82
+ end
83
+ else
84
+ Mjml.setup do |config|
85
+ ...
86
+ end
87
+ end
88
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mjml/remote/rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require "mjml/remote"
@@ -0,0 +1,21 @@
1
+ require "action_view"
2
+ require "action_view/template"
3
+ require "rails/railtie"
4
+ require "mjml/remote/version"
5
+ require "mjml/remote/handler"
6
+ require "mjml/remote/railtie"
7
+ require "mjml/remote/template"
8
+
9
+ module Mjml
10
+ module Remote
11
+ class Error < StandardError; end
12
+ mattr_accessor :endpoint, :headers, :template_language
13
+ @@endpoint = nil
14
+ @@template_language = :erb
15
+ @@headers = Hash.new
16
+
17
+ def self.setup
18
+ yield self if block_given?
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module Mjml
2
+ module Remote
3
+ class Handler
4
+ def template_handler
5
+ @_template_handler ||= ActionView::Template.registered_template_handler(Mjml::Remote.template_language)
6
+ end
7
+ def call(template)
8
+ compiled_source = template_handler.call(template)
9
+ # Per MJML v4 syntax documentation[0] valid/render'able document MUST start with <mjml> root tag
10
+ # If we get here and template source doesn't start with one it means
11
+ # that we are rendering partial named according to legacy naming convention (partials ending with '.mjml')
12
+ # Therefore we skip MJML processing and return raw compiled source. It will be processed
13
+ # by MJML library when top-level layout/template is rendered
14
+ #
15
+ # [0] - https://github.com/mjmlio/mjml/blob/master/doc/guide.md#mjml
16
+ if compiled_source =~ /<mjml(.+)?>/i
17
+ "Mjml::Remote::Template.to_html(begin;#{compiled_source};end).html_safe"
18
+ else
19
+ compiled_source
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module Mjml
2
+ module Remote
3
+ class Railtie < Rails::Railtie
4
+ config.mjml_remote = Mjml::Remote
5
+ initializer "mjml-remote-rails.register_template_handler" do
6
+ ActionView::Template.register_template_handler :mjml, Mjml::Remote::Handler.new
7
+ Mime::Type.register_alias "text/html", :mjml
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ module Mjml
2
+ module Remote
3
+ class Template
4
+ class ParseError < StandardError
5
+ attr_reader :response
6
+ def initialize(response)
7
+ @response = response
8
+ end
9
+ end
10
+ def self.to_html(compiled_source)
11
+ new(compiled_source).render.html_safe
12
+ end
13
+ def initialize(input)
14
+ @input = input
15
+ end
16
+ def render
17
+ response = Faraday.post(
18
+ Mjml::Remote.endpoint,
19
+ @input,
20
+ Mjml::Remote.headers
21
+ )
22
+ content = JSON.parse(response.body)
23
+ if response.success?
24
+ content["html".freeze]
25
+ else
26
+ raise ParseError.new(content)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ module Mjml
2
+ module Remote
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mjml/remote/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mjml-remote-rails"
8
+ spec.version = Mjml::Remote::VERSION
9
+ spec.authors = ["Tapio Saarinen"]
10
+ spec.email = ["prime@factori.se"]
11
+
12
+ spec.summary = %q{Use MJML with rails, rendered via a HTTP request}
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.17"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+
27
+ spec.add_dependency "faraday", "~> 0.12"
28
+ spec.add_dependency "rails", "~> 5.2"
29
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mjml-remote-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tapio Saarinen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.2'
83
+ description:
84
+ email:
85
+ - prime@factori.se
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/mjml-remote-rails.rb
101
+ - lib/mjml/remote.rb
102
+ - lib/mjml/remote/handler.rb
103
+ - lib/mjml/remote/railtie.rb
104
+ - lib/mjml/remote/template.rb
105
+ - lib/mjml/remote/version.rb
106
+ - mjml-remote-rails.gemspec
107
+ homepage:
108
+ licenses: []
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.7.6
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Use MJML with rails, rendered via a HTTP request
130
+ test_files: []