paubox 0.3.0 → 0.3.2

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.
@@ -1,94 +1,94 @@
1
- # frozen_string_literal: true
2
-
3
- module Paubox
4
- # The MailToMessage class takes a Ruby Mail object and attempts to parse it
5
- # into a Hash formatted for the JSON payload of HTTP api request.
6
- class MailToMessage
7
- include Paubox::FormatHelper
8
- attr_reader :mail
9
- require 'tempfile'
10
-
11
- def initialize(mail, args = {})
12
- @mail = mail
13
- @allow_non_tls = args.fetch(:allow_non_tls, false)
14
- @force_secure_notification = args.fetch(:force_secure_notification, nil)
15
- end
16
-
17
- def send_message_payload
18
- { data: { message: convert_keys_to_json_version(build_parts) } }.to_json
19
- end
20
-
21
- private
22
-
23
- def build_attachments
24
- attachments = mail.attachments
25
- return [] if attachments.empty?
26
-
27
- packaged_attachments = []
28
- attachments.each do |attch|
29
- packaged_attachments << { content: convert_binary_to_base64(attch.body.decoded),
30
- file_name: attch.filename,
31
- content_type: attch.mime_type }
32
- end
33
- packaged_attachments
34
- end
35
-
36
- def build_content
37
- content = {}
38
- if mail.multipart?
39
- html_content = mail.html_part.body.to_s if mail.html_part
40
- text_content = mail.text_part.body.to_s if mail.text_part
41
- content[:html_content] = base64_encode_if_needed(html_content) unless html_content.nil?
42
- content[:text_content] = text_content unless text_content.nil?
43
- elsif mail.content_type.to_s.include? 'text/html'
44
- content[:html_content] = base64_encode_if_needed(mail.body.to_s)
45
- else
46
- content[:text_content] = mail.body.to_s
47
- end
48
- content
49
- end
50
-
51
- def build_headers
52
- %i[from reply_to subject].map { |k| [k, mail[k].to_s] }.to_h
53
- end
54
-
55
- def build_force_secure_notification
56
- if @force_secure_notification.instance_of?(String)
57
- unless @force_secure_notification.to_s.empty? # if force_secure_notification is not nil or empty
58
- force_secure_notification_val = @force_secure_notification.strip.downcase
59
- if force_secure_notification_val == 'true'
60
- return true
61
- elsif force_secure_notification_val == 'false'
62
- return false
63
- else
64
- return nil
65
- end
66
- end
67
- end
68
- nil
69
- end
70
-
71
- def build_parts
72
- msg = {}
73
- msg[:recipients] = string_or_array_to_array(mail.to)
74
- msg[:cc] = string_or_array_to_array(mail.cc)
75
- msg[:bcc] = string_or_array_to_array(mail.bcc)
76
- msg[:allow_non_tls] = @allow_non_tls
77
- @force_secure_notification = build_force_secure_notification
78
- unless @force_secure_notification.nil?
79
- msg[:force_secure_notification] = @force_secure_notification
80
- end
81
- msg[:headers] = build_headers
82
- msg[:content] = build_content
83
- msg[:attachments] = build_attachments
84
- msg
85
- end
86
-
87
- def convert_binary_to_base64(f)
88
- file = Tempfile.new(encoding: 'ascii-8bit')
89
- file.write(f)
90
- file.rewind
91
- Base64.encode64(file.read)
92
- end
93
- end
94
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Paubox
4
+ # The MailToMessage class takes a Ruby Mail object and attempts to parse it
5
+ # into a Hash formatted for the JSON payload of HTTP api request.
6
+ class MailToMessage
7
+ include Paubox::FormatHelper
8
+ attr_reader :mail
9
+ require 'tempfile'
10
+
11
+ def initialize(mail, args = {})
12
+ @mail = mail
13
+ @allow_non_tls = args.fetch(:allow_non_tls, false)
14
+ @force_secure_notification = args.fetch(:force_secure_notification, nil)
15
+ end
16
+
17
+ def send_message_payload
18
+ { data: { message: convert_keys_to_json_version(build_parts) } }.to_json
19
+ end
20
+
21
+ private
22
+
23
+ def build_attachments
24
+ attachments = mail.attachments
25
+ return [] if attachments.empty?
26
+
27
+ packaged_attachments = []
28
+ attachments.each do |attch|
29
+ packaged_attachments << { content: convert_binary_to_base64(attch.body.decoded),
30
+ file_name: attch.filename,
31
+ content_type: attch.mime_type }
32
+ end
33
+ packaged_attachments
34
+ end
35
+
36
+ def build_content
37
+ content = {}
38
+ if mail.multipart?
39
+ html_content = mail.html_part.body.to_s if mail.html_part
40
+ text_content = mail.text_part.body.to_s if mail.text_part
41
+ content[:html_content] = base64_encode_if_needed(html_content) unless html_content.nil?
42
+ content[:text_content] = text_content unless text_content.nil?
43
+ elsif mail.content_type.to_s.include? 'text/html'
44
+ content[:html_content] = base64_encode_if_needed(mail.body.to_s)
45
+ else
46
+ content[:text_content] = mail.body.to_s
47
+ end
48
+ content
49
+ end
50
+
51
+ def build_headers
52
+ %i[from reply_to subject].map { |k| [k, mail[k].to_s] }.to_h
53
+ end
54
+
55
+ def build_force_secure_notification
56
+ if @force_secure_notification.instance_of?(String)
57
+ unless @force_secure_notification.to_s.empty? # if force_secure_notification is not nil or empty
58
+ force_secure_notification_val = @force_secure_notification.strip.downcase
59
+ if force_secure_notification_val == 'true'
60
+ return true
61
+ elsif force_secure_notification_val == 'false'
62
+ return false
63
+ else
64
+ return nil
65
+ end
66
+ end
67
+ end
68
+ nil
69
+ end
70
+
71
+ def build_parts
72
+ msg = {}
73
+ msg[:recipients] = string_or_array_to_array(mail.to)
74
+ msg[:cc] = string_or_array_to_array(mail.cc)
75
+ msg[:bcc] = string_or_array_to_array(mail.bcc)
76
+ msg[:allow_non_tls] = @allow_non_tls
77
+ @force_secure_notification = build_force_secure_notification
78
+ unless @force_secure_notification.nil?
79
+ msg[:force_secure_notification] = @force_secure_notification
80
+ end
81
+ msg[:headers] = build_headers
82
+ msg[:content] = build_content
83
+ msg[:attachments] = build_attachments
84
+ msg
85
+ end
86
+
87
+ def convert_binary_to_base64(f)
88
+ file = Tempfile.new(encoding: 'ascii-8bit')
89
+ file.write(f)
90
+ file.rewind
91
+ Base64.encode64(file.read)
92
+ end
93
+ end
94
+ end
@@ -1,98 +1,98 @@
1
- # frozen_string_literal: true
2
-
3
- module Paubox
4
- # The Message class is for building a Paubox email message using a hash.
5
- class Message
6
- include Paubox::FormatHelper
7
- attr_accessor :from, :to, :cc, :bcc, :subject, :reply_to, :text_content,
8
- :html_content, :allow_non_tls, :force_secure_notification
9
-
10
- def initialize(args)
11
- @from = args[:from]
12
- @to = args[:to]
13
- @cc = args[:cc]
14
- @bcc = args[:bcc]
15
- @subject = args[:subject]
16
- @reply_to = args[:reply_to]
17
- @text_content = args[:text_content]
18
- @html_content = args[:html_content]
19
- @packaged_attachments = []
20
- @attachments = build_attachments(args[:attachments])
21
- @allow_non_tls = args.fetch(:allow_non_tls, false)
22
- @force_secure_notification = args.fetch(:force_secure_notification, nil)
23
- end
24
-
25
- def send_message_payload
26
- { data: { message: convert_keys_to_json_version(build_parts) } }.to_json
27
- end
28
-
29
- def add_attachment(file_path)
30
- @packaged_attachments << { filename: file_path.split('/').last,
31
- content_type: `file --b --mime-type #{file_path}`.chomp,
32
- content: Base64.encode64(File.read(file_path)) }
33
- end
34
-
35
- def attachments
36
- @packaged_attachments
37
- end
38
-
39
- def attachments=(args)
40
- @attachments = build_attachments(args)
41
- end
42
-
43
- private
44
-
45
- def build_attachments(args)
46
- return (@packaged_attachments = []) if args.to_a.empty?
47
-
48
- args.each do |a|
49
- a[:content] = base64_encode_if_needed(a[:content])
50
- @packaged_attachments << a
51
- end
52
- @packaged_attachments
53
- end
54
-
55
- def build_content
56
- content = {}
57
- content[:text_content] = text_content if text_content
58
- content[:html_content] = base64_encode_if_needed(html_content) if html_content
59
- content
60
- end
61
-
62
- def build_headers
63
- %i[from reply_to subject].map { |k| [k, send(k)] }.to_h
64
- end
65
-
66
- def build_force_secure_notification
67
- if @force_secure_notification.instance_of?(String)
68
- unless @force_secure_notification.to_s.empty? # if force_secure_notification is not nil or empty
69
- force_secure_notification_val = @force_secure_notification.strip.downcase
70
- if force_secure_notification_val == 'true'
71
- return true
72
- elsif force_secure_notification_val == 'false'
73
- return false
74
- else
75
- return nil
76
- end
77
- end
78
- end
79
- nil
80
- end
81
-
82
- def build_parts
83
- msg = {}
84
- msg[:recipients] = string_or_array_to_array(to)
85
- msg[:cc] = string_or_array_to_array(cc)
86
- msg[:allow_non_tls] = @allow_non_tls
87
- @force_secure_notification = build_force_secure_notification
88
- unless @force_secure_notification.nil?
89
- msg[:force_secure_notification] = @force_secure_notification
90
- end
91
- msg[:bcc] = string_or_array_to_array(bcc)
92
- msg[:headers] = build_headers
93
- msg[:content] = build_content
94
- msg[:attachments] = attachments
95
- msg
96
- end
97
- end
98
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Paubox
4
+ # The Message class is for building a Paubox email message using a hash.
5
+ class Message
6
+ include Paubox::FormatHelper
7
+ attr_accessor :from, :to, :cc, :bcc, :subject, :reply_to, :text_content,
8
+ :html_content, :allow_non_tls, :force_secure_notification
9
+
10
+ def initialize(args)
11
+ @from = args[:from]
12
+ @to = args[:to]
13
+ @cc = args[:cc]
14
+ @bcc = args[:bcc]
15
+ @subject = args[:subject]
16
+ @reply_to = args[:reply_to]
17
+ @text_content = args[:text_content]
18
+ @html_content = args[:html_content]
19
+ @packaged_attachments = []
20
+ @attachments = build_attachments(args[:attachments])
21
+ @allow_non_tls = args.fetch(:allow_non_tls, false)
22
+ @force_secure_notification = args.fetch(:force_secure_notification, nil)
23
+ end
24
+
25
+ def send_message_payload
26
+ { data: { message: convert_keys_to_json_version(build_parts) } }.to_json
27
+ end
28
+
29
+ def add_attachment(file_path)
30
+ @packaged_attachments << { filename: file_path.split('/').last,
31
+ content_type: `file --b --mime-type #{file_path}`.chomp,
32
+ content: Base64.encode64(File.read(file_path)) }
33
+ end
34
+
35
+ def attachments
36
+ @packaged_attachments
37
+ end
38
+
39
+ def attachments=(args)
40
+ @attachments = build_attachments(args)
41
+ end
42
+
43
+ private
44
+
45
+ def build_attachments(args)
46
+ return (@packaged_attachments = []) if args.to_a.empty?
47
+
48
+ args.each do |a|
49
+ a[:content] = base64_encode_if_needed(a[:content])
50
+ @packaged_attachments << a
51
+ end
52
+ @packaged_attachments
53
+ end
54
+
55
+ def build_content
56
+ content = {}
57
+ content[:text_content] = text_content if text_content
58
+ content[:html_content] = base64_encode_if_needed(html_content) if html_content
59
+ content
60
+ end
61
+
62
+ def build_headers
63
+ %i[from reply_to subject].map { |k| [k, send(k)] }.to_h
64
+ end
65
+
66
+ def build_force_secure_notification
67
+ if @force_secure_notification.instance_of?(String)
68
+ unless @force_secure_notification.to_s.empty? # if force_secure_notification is not nil or empty
69
+ force_secure_notification_val = @force_secure_notification.strip.downcase
70
+ if force_secure_notification_val == 'true'
71
+ return true
72
+ elsif force_secure_notification_val == 'false'
73
+ return false
74
+ else
75
+ return nil
76
+ end
77
+ end
78
+ end
79
+ nil
80
+ end
81
+
82
+ def build_parts
83
+ msg = {}
84
+ msg[:recipients] = string_or_array_to_array(to)
85
+ msg[:cc] = string_or_array_to_array(cc)
86
+ msg[:allow_non_tls] = @allow_non_tls
87
+ @force_secure_notification = build_force_secure_notification
88
+ unless @force_secure_notification.nil?
89
+ msg[:force_secure_notification] = @force_secure_notification
90
+ end
91
+ msg[:bcc] = string_or_array_to_array(bcc)
92
+ msg[:headers] = build_headers
93
+ msg[:content] = build_content
94
+ msg[:attachments] = attachments
95
+ msg
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Paubox
4
+ # The TemplatedMessage class is for building a Paubox email message from a dynamic template, using a hash.
5
+ class TemplatedMessage < Message
6
+ attr_accessor :template_name,
7
+ :template_values
8
+
9
+ def initialize(args)
10
+ @template_name = args[:template][:name]
11
+ @template_values = args[:template][:values]
12
+
13
+ super
14
+ end
15
+
16
+ def send_message_payload
17
+ # template name and values must be outside the `message` object
18
+ msg = convert_keys_to_json_version(build_parts)
19
+
20
+ template_params = {
21
+ template_name: @template_name,
22
+ template_values: @template_values.to_json
23
+ }
24
+
25
+ { data: { message: convert_keys_to_json_version(build_parts) }.merge(template_params) }.to_json
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module Paubox
4
- VERSION = '0.3.0'
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Paubox
4
+ VERSION = '0.3.2'
5
+ end
data/lib/paubox.rb CHANGED
@@ -1,24 +1,25 @@
1
- # frozen_string_literal: true
2
-
3
- require 'paubox/version'
4
- require 'paubox/client'
5
- require 'paubox/format_helper'
6
- require 'paubox/mail_to_message'
7
- require 'paubox/message'
8
- require 'paubox/email_disposition'
9
- require 'mail/paubox'
10
-
11
- module Paubox
12
- class << self
13
- attr_accessor :configuration
14
- end
15
-
16
- def self.configure
17
- self.configuration ||= Configuration.new
18
- yield(configuration)
19
- end
20
-
21
- class Configuration
22
- attr_accessor :api_key, :api_user
23
- end
24
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'paubox/version'
4
+ require 'paubox/client'
5
+ require 'paubox/format_helper'
6
+ require 'paubox/mail_to_message'
7
+ require 'paubox/message'
8
+ require 'paubox/templated_message'
9
+ require 'paubox/email_disposition'
10
+ require 'mail/paubox'
11
+
12
+ module Paubox
13
+ class << self
14
+ attr_accessor :configuration
15
+ end
16
+
17
+ def self.configure
18
+ self.configuration ||= Configuration.new
19
+ yield(configuration)
20
+ end
21
+
22
+ class Configuration
23
+ attr_accessor :api_key, :api_user
24
+ end
25
+ end
data/lib/paubox_ruby.rb CHANGED
@@ -1,3 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
- require 'paubox'
1
+ # frozen_string_literal: true
2
+
3
+ require 'paubox'
data/paubox_ruby.gemspec CHANGED
@@ -1,34 +1,34 @@
1
- # frozen_string_literal: true
2
-
3
- lib = File.expand_path('lib', __dir__)
4
-
5
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
- require 'paubox/version'
7
-
8
- Gem::Specification.new do |spec|
9
- spec.name = 'paubox'
10
- spec.version = Paubox::VERSION
11
- spec.authors = ['Paubox', 'Jonathan Greeley']
12
- spec.email = ['jon.r.greeley@gmail.com']
13
-
14
- spec.summary = "Paubox's Official Ruby SDK"
15
- spec.description = 'Ruby SDK for interacting with the Paubox Transactional Email HTTP API.'
16
- spec.homepage = 'https://www.paubox.com'
17
- spec.license = 'Apache-2.0'
18
-
19
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
- f.match(%r{^(test|spec|features)/})
21
- end
22
- spec.bindir = 'exe'
23
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
- spec.require_paths = ['lib']
25
- spec.required_ruby_version = '>= 2.3'
26
- spec.add_development_dependency 'bundler', '~> 2.0.2'
27
- spec.add_development_dependency 'pry'
28
- spec.add_development_dependency 'rake', '~> 10.0'
29
- spec.add_development_dependency 'rspec', '~> 3.2'
30
- spec.add_development_dependency 'webmock', '~> 2.1'
31
-
32
- spec.add_dependency 'mail', '>= 2.5'
33
- spec.add_dependency 'rest-client', '~> 2.0', '>= 2.0.2'
34
- end
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'paubox/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'paubox'
10
+ spec.version = Paubox::VERSION
11
+ spec.authors = ['Paubox']
12
+ spec.email = ['engineering@paubox.com']
13
+
14
+ spec.summary = "Paubox's Official Ruby SDK"
15
+ spec.description = 'Ruby SDK for interacting with the Paubox Transactional Email HTTP API.'
16
+ spec.homepage = 'https://www.paubox.com'
17
+ spec.license = 'Apache-2.0'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+ spec.required_ruby_version = '>= 2.3'
26
+ spec.add_development_dependency 'bundler', '~> 2.0'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency "rake", ">= 12.3.3"
29
+ spec.add_development_dependency 'rspec', '~> 3.2'
30
+ spec.add_development_dependency 'webmock', '~> 2.1'
31
+
32
+ spec.add_dependency 'mail', '>= 2.5'
33
+ spec.add_dependency 'rest-client', '~> 2.0', '>= 2.0.2'
34
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paubox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paubox
8
- - Jonathan Greeley
9
- autorequire:
8
+ autorequire:
10
9
  bindir: exe
11
10
  cert_chain: []
12
- date: 2019-07-10 00:00:00.000000000 Z
11
+ date: 2022-10-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
@@ -17,14 +16,14 @@ dependencies:
17
16
  requirements:
18
17
  - - "~>"
19
18
  - !ruby/object:Gem::Version
20
- version: 2.0.2
19
+ version: '2.0'
21
20
  type: :development
22
21
  prerelease: false
23
22
  version_requirements: !ruby/object:Gem::Requirement
24
23
  requirements:
25
24
  - - "~>"
26
25
  - !ruby/object:Gem::Version
27
- version: 2.0.2
26
+ version: '2.0'
28
27
  - !ruby/object:Gem::Dependency
29
28
  name: pry
30
29
  requirement: !ruby/object:Gem::Requirement
@@ -43,16 +42,16 @@ dependencies:
43
42
  name: rake
44
43
  requirement: !ruby/object:Gem::Requirement
45
44
  requirements:
46
- - - "~>"
45
+ - - ">="
47
46
  - !ruby/object:Gem::Version
48
- version: '10.0'
47
+ version: 12.3.3
49
48
  type: :development
50
49
  prerelease: false
51
50
  version_requirements: !ruby/object:Gem::Requirement
52
51
  requirements:
53
- - - "~>"
52
+ - - ">="
54
53
  - !ruby/object:Gem::Version
55
- version: '10.0'
54
+ version: 12.3.3
56
55
  - !ruby/object:Gem::Dependency
57
56
  name: rspec
58
57
  requirement: !ruby/object:Gem::Requirement
@@ -117,7 +116,7 @@ dependencies:
117
116
  version: 2.0.2
118
117
  description: Ruby SDK for interacting with the Paubox Transactional Email HTTP API.
119
118
  email:
120
- - jon.r.greeley@gmail.com
119
+ - engineering@paubox.com
121
120
  executables: []
122
121
  extensions: []
123
122
  extra_rdoc_files: []
@@ -138,6 +137,7 @@ files:
138
137
  - lib/paubox/format_helper.rb
139
138
  - lib/paubox/mail_to_message.rb
140
139
  - lib/paubox/message.rb
140
+ - lib/paubox/templated_message.rb
141
141
  - lib/paubox/version.rb
142
142
  - lib/paubox_ruby.rb
143
143
  - paubox_ruby.gemspec
@@ -145,7 +145,7 @@ homepage: https://www.paubox.com
145
145
  licenses:
146
146
  - Apache-2.0
147
147
  metadata: {}
148
- post_install_message:
148
+ post_install_message:
149
149
  rdoc_options: []
150
150
  require_paths:
151
151
  - lib
@@ -160,8 +160,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  - !ruby/object:Gem::Version
161
161
  version: '0'
162
162
  requirements: []
163
- rubygems_version: 3.0.1
164
- signing_key:
163
+ rubygems_version: 3.3.14
164
+ signing_key:
165
165
  specification_version: 4
166
166
  summary: Paubox's Official Ruby SDK
167
167
  test_files: []