postmark_customized 0.1.0

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
+ SHA256:
3
+ metadata.gz: 139d25036ac960c70d9fb78205bfc0f9c3fba36c1d742d50b3b3b21ad349f9d7
4
+ data.tar.gz: f63d6adadd4d8501329829eba748190dce5c7e0e668fc46842cf2b162da299bd
5
+ SHA512:
6
+ metadata.gz: d5829229fde461c6576b3ec29d8b835c87dcc8b64b588ce972e6e20c7bb8818b35f8d76a4a5c3b761b964b17ec73cb241ef77546134e0bb3c7b81a6b71fe4da3
7
+ data.tar.gz: a00a808798eee7bdfee25cec4b1c15a5c6ebbdbe6b0aab3e0ca00c07a8500293be43d16fade53c63cb6f10885d7efb3f4d1d221019c263c6a67f7a64ebbe7dd3
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'griddler', github: 'thoughtbot/griddler'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Vladyslav Sydorenko
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Griddler::PostmarkCustomized
2
+
3
+ Customized adapter for [Postmark](http://developer.postmarkapp.com/developer-inbound-parse.html) to use with [Griddler](https://github.com/thoughtbot/griddler)
4
+
5
+ Original gem - [griddler-postmark](https://github.com/r38y/griddler-postmark)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'griddler'
13
+ gem 'postmark_customized'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install postmark_customized
23
+
24
+ ## Usage
25
+
26
+ Create an initializer with the following settings:
27
+
28
+ ```ruby
29
+ Griddler.configure do |config|
30
+ config.email_service = :postmark_customized
31
+ end
32
+ ```
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "postmark_customized"
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__)
data/bin/setup ADDED
@@ -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,5 @@
1
+ require "griddler"
2
+ require "griddler/postmark_customized/version"
3
+ require "griddler/postmark_customized/adapter"
4
+
5
+ Griddler.adapter_registry.register(:postmark_customized, Griddler::PostmarkCustomized::Adapter)
@@ -0,0 +1,87 @@
1
+ require 'active_support/core_ext/string/strip'
2
+
3
+ module Griddler
4
+ module PostmarkCustomized
5
+ class Adapter
6
+ def initialize(params)
7
+ @params = params
8
+ end
9
+
10
+ def self.normalize_params(params)
11
+ adapter = new(params)
12
+ adapter.normalize_params
13
+ end
14
+
15
+ def normalize_params
16
+ {
17
+ from: full_email(params[:FromFull]),
18
+ to_email: params[:To],
19
+ from_email: params[:From],
20
+ to: extract_recipients(:ToFull),
21
+ cc: extract_recipients(:CcFull),
22
+ bcc: extract_recipients(:BccFull),
23
+ headers: headers,
24
+ subject: params[:Subject],
25
+ text: params[:TextBody],
26
+ html: params[:HtmlBody],
27
+ original_recipient: params[:OriginalRecipient],
28
+ reply_to: params[:ReplyTo],
29
+ mailbox_hash: params[:MailboxHash],
30
+ attachments: attachment_files
31
+ }
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :params
37
+
38
+ def headers
39
+ Array(@params[:Headers]).inject({}) do |hash, header|
40
+ hash[header[:Name]] = header[:Value]
41
+ hash
42
+ end
43
+ end
44
+
45
+ def extract_recipients(key)
46
+ params[key].to_a.map { |recipient| full_email(recipient) }
47
+ end
48
+
49
+ def full_email(contact_info)
50
+ email = contact_info[:Email]
51
+ if contact_info[:Name].present?
52
+ "#{contact_info[:Name]} <#{email}>"
53
+ else
54
+ email
55
+ end
56
+ end
57
+
58
+ def attachment_files
59
+ attachments = Array(params[:Attachments])
60
+
61
+ attachments.map do |attachment|
62
+ ActionDispatch::Http::UploadedFile.new(
63
+ filename: attachment[:Name],
64
+ type: attachment[:ContentType],
65
+ tempfile: create_tempfile(attachment)
66
+ )
67
+ end
68
+ end
69
+
70
+ def create_tempfile(attachment)
71
+ filename = attachment[:Name].gsub(/\/|\\/, '_')[0..150]
72
+ tempfile = Tempfile.new(filename, Dir::tmpdir, encoding: 'ascii-8bit')
73
+ tempfile.write(content(attachment))
74
+ tempfile.rewind
75
+ tempfile
76
+ end
77
+
78
+ def content(attachment)
79
+ if content = attachment[:Content]
80
+ Base64.decode64(content)
81
+ else
82
+ content
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,5 @@
1
+ module Griddler
2
+ module PostmarkCustomized
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "griddler/postmark_customized/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "postmark_customized"
7
+ spec.version = Griddler::PostmarkCustomized::VERSION
8
+ spec.authors = ["Vladyslav Sydorenko"]
9
+ spec.email = ["svuitis@gmail.com"]
10
+
11
+ spec.summary = %q{Customized Postmark adapter for Griddler}
12
+ spec.description = %q{Allows to accept MailboxHash from Postmark response}
13
+ spec.homepage = "https://github.com/Equitis90/postmark_customized"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 2.0"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+
25
+ spec.add_dependency "griddler"
26
+ end
Binary file
Binary file
@@ -0,0 +1,195 @@
1
+ require 'spec_helper'
2
+
3
+ describe Griddler::PostmarkCustomized::Adapter, '.normalize_params' do
4
+ it_should_behave_like 'Griddler adapter', :postmark_customized,
5
+ {
6
+ FromFull: {
7
+ Email: 'there@example.com',
8
+ Name: 'There',
9
+ },
10
+ ToFull: [{
11
+ Email: 'hi@example.com',
12
+ Name: 'Hello World',
13
+ }],
14
+ CcFull: [{
15
+ Email: 'emily@example.com',
16
+ Name: '',
17
+ }],
18
+ BccFull: [{
19
+ Email: 'mary@example.com',
20
+ Name: 'Mary',
21
+ }],
22
+ TextBody: 'hi',
23
+ MailboxHash: '1234'
24
+ }
25
+
26
+ it 'normalizes parameters' do
27
+ expect(Griddler::PostmarkCustomized::Adapter.normalize_params(default_params)).to be_normalized_to({
28
+ to: ["Robert Paulson <bob@example.com>"],
29
+ cc: ["Jack <jack@example.com>"],
30
+ bcc: ["Mary <mary@example.com>"],
31
+ original_recipient: "dick@example.com",
32
+ reply_to: "john@example.com",
33
+ from: "Tyler Durden <tdurden@example.com>",
34
+ subject: 'Reminder: First and Second Rule',
35
+ text: %r{Dear bob\nReply ABOVE THIS LINE\nhey sup},
36
+ html: /<p>Dear bob<\/p>\nReply ABOVE THIS LINE\nhey sup/,
37
+ headers: {
38
+ "Message-ID" => "<message-id@mail.gmail.com>"
39
+ },
40
+ mailbox_hash: '1234'
41
+ })
42
+ end
43
+
44
+ it 'handles CcFull of nil' do
45
+ no_cc_params = default_params
46
+ no_cc_params[:CcFull] = nil
47
+ normalized = Griddler::PostmarkCustomized::Adapter.normalize_params(no_cc_params)
48
+
49
+ expect(normalized[:cc]).to eq []
50
+ end
51
+
52
+ it 'passes the received array of files' do
53
+ params = default_params.merge({ Attachments: [upload_1_params, upload_2_params] })
54
+
55
+ normalized_params = Griddler::PostmarkCustomized::Adapter.normalize_params(params)
56
+
57
+ first, second = *normalized_params[:attachments]
58
+
59
+ expect(first.original_filename).to eq('photo1.jpg')
60
+ expect(first.content_type).to eq(upload_1_params[:ContentType])
61
+
62
+ expect(second.original_filename).to eq('photo2.jpg')
63
+ expect(second.content_type).to eq(upload_2_params[:ContentType])
64
+ end
65
+
66
+ it 'has no attachments' do
67
+ params = default_params
68
+
69
+ normalized_params = Griddler::PostmarkCustomized::Adapter.normalize_params(params)
70
+
71
+ expect(normalized_params[:attachments]).to be_empty
72
+ end
73
+
74
+ it 'gets rid of the original postmark params' do
75
+ expect(Griddler::PostmarkCustomized::Adapter.normalize_params(default_params)).to be_normalized_to({
76
+ To: nil,
77
+ From: nil,
78
+ ToFull: nil,
79
+ FromFull: nil,
80
+ CcFull: nil,
81
+ Subject: nil,
82
+ TextBody: nil,
83
+ HtmlBody: nil,
84
+ Attachments: nil,
85
+ MailboxHash: nil
86
+ })
87
+ end
88
+
89
+ it 'can handle a blank attachment content' do
90
+ params = default_params.merge({
91
+ Attachments: [
92
+ {
93
+ Name: "empty.gif",
94
+ }
95
+ ]
96
+ })
97
+ expect {
98
+ Griddler::PostmarkCustomized::Adapter.normalize_params(params)
99
+ }.to_not raise_error
100
+ end
101
+
102
+ it 'can handle a really long name' do
103
+ params = default_params.merge({
104
+ Attachments: [
105
+ {
106
+ Name: ("x"*500),
107
+ }
108
+ ]
109
+ })
110
+ expect {
111
+ Griddler::PostmarkCustomized::Adapter.normalize_params(params)
112
+ }.to_not raise_error
113
+ end
114
+
115
+ def default_params
116
+ {
117
+ To: 'bob@example.com',
118
+ From: 'tdurden@example.com',
119
+ FromFull: {
120
+ Email: 'tdurden@example.com',
121
+ Name: 'Tyler Durden'
122
+ },
123
+ ToFull: [{
124
+ Email: 'bob@example.com',
125
+ Name: 'Robert Paulson'
126
+ }],
127
+ CcFull: [{
128
+ Email: 'jack@example.com',
129
+ Name: 'Jack'
130
+ }],
131
+ BccFull: [{
132
+ Email: 'mary@example.com',
133
+ Name: 'Mary',
134
+ }],
135
+ OriginalRecipient: "dick@example.com",
136
+ ReplyTo: "john@example.com",
137
+ Subject: 'Reminder: First and Second Rule',
138
+ TextBody: text_body,
139
+ HtmlBody: text_html,
140
+ Headers: [
141
+ {
142
+ Name: "Message-ID",
143
+ Value: "<message-id@mail.gmail.com>"
144
+ }
145
+ ],
146
+ MailboxHash: '1234'
147
+ }
148
+ end
149
+
150
+ def text_body
151
+ <<-EOS.strip_heredoc.strip
152
+ Dear bob
153
+ Reply ABOVE THIS LINE
154
+ hey sup
155
+ EOS
156
+ end
157
+
158
+ def text_html
159
+ <<-EOS.strip_heredoc.strip
160
+ <p>Dear bob</p>
161
+ Reply ABOVE THIS LINE
162
+ hey sup
163
+ EOS
164
+ end
165
+
166
+ def upload_1_params
167
+ @upload_1_params ||= begin
168
+ file = fixture_file('photo1.jpg')
169
+ {
170
+ Name: 'photo1.jpg',
171
+ Content: Base64.encode64(file.read),
172
+ ContentType: 'image/jpeg',
173
+ ContentLength: file.size,
174
+ ContentID: "photo1.jpg@01D2DF9B.C09E7220"
175
+ }
176
+ end
177
+ end
178
+
179
+ def upload_2_params
180
+ @upload_2_params ||= begin
181
+ file = fixture_file('photo2.jpg')
182
+ {
183
+ Name: 'photo2.jpg',
184
+ Content: Base64.encode64(file.read),
185
+ ContentType: 'image/jpeg',
186
+ ContentLength: file.size
187
+ }
188
+ end
189
+ end
190
+
191
+ def fixture_file(name)
192
+ cwd = File.expand_path File.dirname(__FILE__)
193
+ File.new(File.join(cwd, '..', '..', 'fixtures', name))
194
+ end
195
+ end
@@ -0,0 +1,9 @@
1
+ require 'griddler/testing'
2
+ require 'griddler/postmark_customized'
3
+ require 'action_dispatch'
4
+
5
+ RSpec.configure do |config|
6
+ config.run_all_when_everything_filtered = true
7
+ config.order = 'random'
8
+ config.include Griddler::Testing
9
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postmark_customized
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladyslav Sydorenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-08 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: griddler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Allows to accept MailboxHash from Postmark response
70
+ email:
71
+ - svuitis@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/griddler/postmark_customized.rb
86
+ - lib/griddler/postmark_customized/adapter.rb
87
+ - lib/griddler/postmark_customized/version.rb
88
+ - postmark_customized.gemspec
89
+ - spec/fixtures/photo1.jpg
90
+ - spec/fixtures/photo2.jpg
91
+ - spec/griddler/postmark_customized/adapter_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: https://github.com/Equitis90/postmark_customized
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.8
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Customized Postmark adapter for Griddler
117
+ test_files: []