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