mail_plugger 1.0.0.beta1 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- mail_plugger (0.1.0)
4
+ mail_plugger (1.0.0.rc1)
5
5
  mail (~> 2.5)
6
6
 
7
7
  GEM
@@ -45,16 +45,16 @@ GEM
45
45
  diff-lcs (>= 1.2.0, < 2.0)
46
46
  rspec-support (~> 3.10.0)
47
47
  rspec-support (3.10.1)
48
- rubocop (1.7.0)
48
+ rubocop (1.8.0)
49
49
  parallel (~> 1.10)
50
- parser (>= 2.7.1.5)
50
+ parser (>= 3.0.0.0)
51
51
  rainbow (>= 2.2.2, < 4.0)
52
52
  regexp_parser (>= 1.8, < 3.0)
53
53
  rexml
54
54
  rubocop-ast (>= 1.2.0, < 2.0)
55
55
  ruby-progressbar (~> 1.7)
56
- unicode-display_width (>= 1.4.0, < 2.0)
57
- rubocop-ast (1.3.0)
56
+ unicode-display_width (>= 1.4.0, < 3.0)
57
+ rubocop-ast (1.4.0)
58
58
  parser (>= 2.7.1.5)
59
59
  rubocop-performance (1.9.2)
60
60
  rubocop (>= 0.90.0, < 2.0)
@@ -63,14 +63,14 @@ GEM
63
63
  rubocop (~> 1.0)
64
64
  rubocop-ast (>= 1.1.0)
65
65
  ruby-progressbar (1.11.0)
66
- simplecov (0.20.0)
66
+ simplecov (0.21.2)
67
67
  docile (~> 1.1)
68
68
  simplecov-html (~> 0.11)
69
69
  simplecov_json_formatter (~> 0.1)
70
70
  simplecov-html (0.12.3)
71
71
  simplecov_json_formatter (0.1.2)
72
72
  thor (1.0.1)
73
- unicode-display_width (1.7.0)
73
+ unicode-display_width (2.0.0)
74
74
  webmock (3.11.0)
75
75
  addressable (>= 2.3.6)
76
76
  crack (>= 0.3.2)
@@ -92,4 +92,4 @@ DEPENDENCIES
92
92
  webmock (~> 3.0)
93
93
 
94
94
  BUNDLED WITH
95
- 2.2.3
95
+ 2.2.4
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- mail_plugger (0.1.0)
4
+ mail_plugger (1.0.0.rc1)
5
5
  mail (~> 2.5)
6
6
 
7
7
  GEM
@@ -43,16 +43,16 @@ GEM
43
43
  diff-lcs (>= 1.2.0, < 2.0)
44
44
  rspec-support (~> 3.10.0)
45
45
  rspec-support (3.10.1)
46
- rubocop (1.7.0)
46
+ rubocop (1.8.0)
47
47
  parallel (~> 1.10)
48
- parser (>= 2.7.1.5)
48
+ parser (>= 3.0.0.0)
49
49
  rainbow (>= 2.2.2, < 4.0)
50
50
  regexp_parser (>= 1.8, < 3.0)
51
51
  rexml
52
52
  rubocop-ast (>= 1.2.0, < 2.0)
53
53
  ruby-progressbar (~> 1.7)
54
- unicode-display_width (>= 1.4.0, < 2.0)
55
- rubocop-ast (1.3.0)
54
+ unicode-display_width (>= 1.4.0, < 3.0)
55
+ rubocop-ast (1.4.0)
56
56
  parser (>= 2.7.1.5)
57
57
  rubocop-performance (1.9.2)
58
58
  rubocop (>= 0.90.0, < 2.0)
@@ -61,14 +61,14 @@ GEM
61
61
  rubocop (~> 1.0)
62
62
  rubocop-ast (>= 1.1.0)
63
63
  ruby-progressbar (1.11.0)
64
- simplecov (0.20.0)
64
+ simplecov (0.21.2)
65
65
  docile (~> 1.1)
66
66
  simplecov-html (~> 0.11)
67
67
  simplecov_json_formatter (~> 0.1)
68
68
  simplecov-html (0.12.3)
69
69
  simplecov_json_formatter (0.1.2)
70
70
  thor (1.0.1)
71
- unicode-display_width (1.7.0)
71
+ unicode-display_width (2.0.0)
72
72
  webmock (3.11.0)
73
73
  addressable (>= 2.3.6)
74
74
  crack (>= 0.3.2)
@@ -90,4 +90,4 @@ DEPENDENCIES
90
90
  webmock (~> 3.0)
91
91
 
92
92
  BUNDLED WITH
93
- 2.2.3
93
+ 2.2.4
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- mail_plugger (0.1.0)
4
+ mail_plugger (1.0.0.rc1)
5
5
  mail (~> 2.5)
6
6
 
7
7
  GEM
@@ -43,16 +43,16 @@ GEM
43
43
  diff-lcs (>= 1.2.0, < 2.0)
44
44
  rspec-support (~> 3.10.0)
45
45
  rspec-support (3.10.1)
46
- rubocop (1.7.0)
46
+ rubocop (1.8.0)
47
47
  parallel (~> 1.10)
48
- parser (>= 2.7.1.5)
48
+ parser (>= 3.0.0.0)
49
49
  rainbow (>= 2.2.2, < 4.0)
50
50
  regexp_parser (>= 1.8, < 3.0)
51
51
  rexml
52
52
  rubocop-ast (>= 1.2.0, < 2.0)
53
53
  ruby-progressbar (~> 1.7)
54
- unicode-display_width (>= 1.4.0, < 2.0)
55
- rubocop-ast (1.3.0)
54
+ unicode-display_width (>= 1.4.0, < 3.0)
55
+ rubocop-ast (1.4.0)
56
56
  parser (>= 2.7.1.5)
57
57
  rubocop-performance (1.9.2)
58
58
  rubocop (>= 0.90.0, < 2.0)
@@ -61,14 +61,14 @@ GEM
61
61
  rubocop (~> 1.0)
62
62
  rubocop-ast (>= 1.1.0)
63
63
  ruby-progressbar (1.11.0)
64
- simplecov (0.20.0)
64
+ simplecov (0.21.2)
65
65
  docile (~> 1.1)
66
66
  simplecov-html (~> 0.11)
67
67
  simplecov_json_formatter (~> 0.1)
68
68
  simplecov-html (0.12.3)
69
69
  simplecov_json_formatter (0.1.2)
70
70
  thor (1.0.1)
71
- unicode-display_width (1.7.0)
71
+ unicode-display_width (2.0.0)
72
72
  webmock (3.11.0)
73
73
  addressable (>= 2.3.6)
74
74
  crack (>= 0.3.2)
@@ -90,4 +90,4 @@ DEPENDENCIES
90
90
  webmock (~> 3.0)
91
91
 
92
92
  BUNDLED WITH
93
- 2.2.3
93
+ 2.2.4
@@ -9,26 +9,22 @@ require 'mail_plugger/version'
9
9
 
10
10
  module MailPlugger
11
11
  class << self
12
- attr_reader :delivery_options, :delivery_settings, :client
12
+ attr_reader :client, :delivery_options, :delivery_settings
13
13
 
14
14
  # Plug in defined API(s) class.
15
15
  #
16
- # @param [String] delivery_system the name of the API
16
+ # @param [String/Symbol] delivery_system the name of the API
17
17
  #
18
18
  # @example using Rails config/initializers/mail_plugger.rb
19
19
  #
20
20
  # The defined API class should have an 'initializer' and a 'deliver' method.
21
21
  # class DefinedApiClientClass
22
- # def initialize(options = {}) # required
23
- # @settings = { api_key: ENV['API_KEY'] }
24
- # @massage_to = options[:to]
25
- # @message_from = options[:from]
26
- # @message_subject = options[:subject]
27
- # @message_body_text = options[:text_part]
28
- # @message_body_html = options[:html_part]
22
+ # def initialize(options = {})
23
+ # @settings = { api_key: '12345' }
24
+ # @options = options
29
25
  # end
30
26
  #
31
- # def deliver # required
27
+ # def deliver
32
28
  # API.new(@settings).client.post(generate_mail_hash)
33
29
  # end
34
30
  #
@@ -38,24 +34,24 @@ module MailPlugger
38
34
  # {
39
35
  # to: generate_recipients,
40
36
  # from: {
41
- # email: @message_from
37
+ # email: @options[:from].first
42
38
  # },
43
- # subject: @message_subject,
39
+ # subject: @options[:subject],
44
40
  # content: [
45
41
  # {
46
42
  # type: 'text/plain',
47
- # value: @message_body_text
43
+ # value: @options[:text_part]
48
44
  # },
49
45
  # {
50
- # type: 'text/html',
51
- # value: @message_body_html
46
+ # type: 'text/html; charset=UTF-8',
47
+ # value: @options[:html_part]
52
48
  # }
53
49
  # ]
54
50
  # }
55
51
  # end
56
52
  #
57
53
  # def generate_recipients
58
- # @massage_to.map do |to|
54
+ # @options[:to].map do |to|
59
55
  # {
60
56
  # email: to
61
57
  # }
@@ -63,18 +59,15 @@ module MailPlugger
63
59
  # end
64
60
  # end
65
61
  #
66
- # MailPlugger.plug_in('definedapi') do |api|
62
+ # MailPlugger.plug_in('defined_api') do |api|
67
63
  # # It will search these options in the Mail::Message object
68
64
  # api.delivery_options = [:to, :from, :subject, :text_part, :html_part]
69
- #
65
+ # api.delivery_settings = { return_response: true }
70
66
  # api.client = DefinedApiClientClass
71
67
  # end
72
68
  #
73
69
  def plug_in(delivery_system)
74
- if delivery_system.nil? || delivery_system.strip.empty?
75
- raise Error::WrongDeliverySystem, 'Delivery system is nil or empty. ' \
76
- 'You should provide correct MailPlugger.plug_in parameter'
77
- end
70
+ check_value(delivery_system)
78
71
 
79
72
  @delivery_system = delivery_system
80
73
 
@@ -83,15 +76,32 @@ module MailPlugger
83
76
  raise Error::WrongPlugInOption, e.message
84
77
  end
85
78
 
86
- # Define 'delivery_options' and 'client' setter methods. These methods are
87
- # generating a hash where the key is the 'delivery_system'. This let us to
88
- # set/use more than one API.
89
- %w[delivery_options delivery_settings client].each do |method|
79
+ # Define 'client', 'delivery_options' and 'delivery_settings' setter
80
+ # methods. These methods are generating a hash where the key is the
81
+ # 'delivery_system'. This let us to set/use more than one API.
82
+ %w[client delivery_options delivery_settings].each do |method|
90
83
  define_method "#{method}=" do |value|
91
84
  variable = instance_variable_get("@#{method}")
92
85
  variable = instance_variable_set("@#{method}", {}) if variable.nil?
93
86
  variable[@delivery_system] = value
94
87
  end
95
88
  end
89
+
90
+ private
91
+
92
+ def check_value(delivery_system)
93
+ if delivery_system.nil?
94
+ raise Error::WrongDeliverySystem, '"delivery_system" is nil'
95
+ end
96
+
97
+ if delivery_system.is_a?(String) && delivery_system.strip.empty?
98
+ raise Error::WrongDeliverySystem, '"delivery_system" is empty'
99
+ end
100
+
101
+ return if delivery_system.is_a?(String) || delivery_system.is_a?(Symbol)
102
+
103
+ raise Error::WrongDeliverySystem, '"delivery_system" does not a ' \
104
+ 'String or Symbol'
105
+ end
96
106
  end
97
107
  end
@@ -8,19 +8,31 @@ module MailPlugger
8
8
  # method, then these attributes can be nil, if not then we should set these
9
9
  # attributes.
10
10
  #
11
- # @param [Hash] options with the credentials
11
+ # @param [Hash] options below
12
+ # @option options [Class/Hash] client
13
+ # e.g. DefinedApiClientClass or { 'key' => DefinedApiClientClass }
14
+ #
15
+ # @option options [Array/Hash] delivery_options
16
+ # e.g. [:to, :from, :subject, :body] or
17
+ # { 'key' => [:to, :from, :subject, :body] }
18
+ #
19
+ # @option options [Hash] delivery_settings
20
+ # e.g. { return_response: true }
21
+ #
22
+ # @option options [String/Symbol] default_delivery_system
23
+ # e.g. 'defined_api'
12
24
  def initialize(options = {})
25
+ @client = options[:client] || MailPlugger.client
26
+
13
27
  @delivery_options = options[:delivery_options] ||
14
28
  MailPlugger.delivery_options
15
29
 
16
- @client = options[:client] || MailPlugger.client
30
+ @delivery_settings = options[:delivery_settings] ||
31
+ MailPlugger.delivery_settings
17
32
 
18
33
  @default_delivery_system = options[:default_delivery_system] ||
19
34
  default_delivery_system_get
20
35
 
21
- @delivery_settings = options[:delivery_settings] ||
22
- MailPlugger.delivery_settings
23
-
24
36
  @message = nil
25
37
  end
26
38
 
@@ -31,6 +43,31 @@ module MailPlugger
31
43
  # provided client class which has a 'deliver' method.
32
44
  #
33
45
  # @param [Mail::Message] message what we would like to send
46
+ #
47
+ # @return [Mail::Message/Hash] depend on delivery_settings and method calls
48
+ #
49
+ # @example
50
+ #
51
+ # MailPlugger.plug_in('test_api_client') do |api|
52
+ # api.delivery_options = %i[from to subject body]
53
+ # api.client = DefinedApiClientClass
54
+ # end
55
+ #
56
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
57
+ # subject: 'Test email', body: 'Test email body')
58
+ #
59
+ # MailPlugger::DeliveryMethod.new.deliver!(message)
60
+ #
61
+ # or
62
+ #
63
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
64
+ # subject: 'Test email', body: 'Test email body')
65
+ #
66
+ # MailPlugger::DeliveryMethod.new(
67
+ # delivery_options: %i[from to subject body],
68
+ # client: DefinedApiClientClass
69
+ # ).deliver!(message)
70
+ #
34
71
  def deliver!(message)
35
72
  unless message.is_a?(Mail::Message)
36
73
  raise Error::WrongParameter,
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'base64'
4
+ require 'mail/indifferent_hash'
4
5
 
5
6
  module MailPlugger
6
7
  module MailHelper
@@ -42,6 +43,8 @@ module MailPlugger
42
43
  data = {}
43
44
 
44
45
  delivery_options.each do |option|
46
+ option = option.to_sym unless option.is_a?(Symbol)
47
+
45
48
  data[option] =
46
49
  case option
47
50
  when :from, :to, :cc, :bcc, :subject
@@ -50,12 +53,14 @@ module MailPlugger
50
53
  extract_attachments
51
54
  when :body, :html_part, :text_part
52
55
  @message.public_send(option)&.decoded
56
+ when :message_obj
57
+ @message
53
58
  else
54
- message_field_value_from(@message[option.to_s])
59
+ message_field_value_from(@message[option])
55
60
  end
56
61
  end
57
62
 
58
- data
63
+ Mail::IndifferentHash.new(data)
59
64
  end
60
65
 
61
66
  # Tries to set up a default delivery system, if the 'delivery_system'
@@ -65,11 +70,7 @@ module MailPlugger
65
70
  #
66
71
  # @return [Stirng] the first key of the 'delivery_options' or 'client'
67
72
  def default_delivery_system_get
68
- if @delivery_options.is_a?(Hash)
69
- @delivery_options
70
- elsif @client.is_a?(Hash)
71
- @client
72
- end&.keys&.first
73
+ extract_keys&.first
73
74
  end
74
75
 
75
76
  # Extract 'delivery_options'. If it's a hash then it'll return the right
@@ -95,16 +96,30 @@ module MailPlugger
95
96
  # @return [String] with the name of the delivery system
96
97
  def delivery_system
97
98
  @delivery_system ||=
98
- (@message && message_field_value_from(@message['delivery_system'])) ||
99
+ (@message && message_field_value_from(@message[:delivery_system])) ||
99
100
  @default_delivery_system
100
101
 
101
- if @delivery_system.nil? &&
102
- (@delivery_options.is_a?(Hash) || @client.is_a?(Hash))
102
+ delivery_system_value_check
103
+
104
+ @delivery_system
105
+ end
106
+
107
+ # Check the given 'delivery_options' or 'client' are hashes and
108
+ # if one of that does then check the 'delivery_system' is valid or not.
109
+ # If the given 'delivery_system' is nil or doesn't match with extracted keys
110
+ # then it will raise error.
111
+ def delivery_system_value_check
112
+ return unless @delivery_options.is_a?(Hash) || @client.is_a?(Hash)
113
+
114
+ if @delivery_system.nil?
103
115
  raise Error::WrongDeliverySystem,
104
116
  '"delivery_system" was not defined as a Mail::Message parameter'
105
117
  end
106
118
 
107
- @delivery_system
119
+ return if extract_keys&.include?(@delivery_system)
120
+
121
+ raise Error::WrongDeliverySystem,
122
+ "\"delivery_system\" '#{@delivery_system}' does not exist"
108
123
  end
109
124
 
110
125
  # Extract attachments.
@@ -112,21 +127,29 @@ module MailPlugger
112
127
  # @return [Array] with extracted attachment hashes
113
128
  def extract_attachments
114
129
  @message.attachments&.map do |attachment|
115
- hash =
116
- if attachment.inline?
117
- { cid: attachment.cid }
118
- else
119
- { filename: attachment.filename }
120
- end
130
+ hash = attachment.inline? ? { cid: attachment.cid } : {}
121
131
 
122
132
  hash.merge(
133
+ filename: attachment.filename,
123
134
  type: attachment.mime_type,
124
135
  content: Base64.encode64(attachment.decoded)
125
136
  )
126
137
  end
127
138
  end
128
139
 
129
- # How to Extract the (uparsed) value of the mail message fields.
140
+ # Extract keys from 'delivery_options' or 'client', depends on which is a
141
+ # hash. If none of these are hashes then returns nil.
142
+ #
143
+ # @return [Array/NilClass] with the keys of 'delivery_options' or 'client'
144
+ def extract_keys
145
+ if @delivery_options.is_a?(Hash)
146
+ @delivery_options
147
+ elsif @client.is_a?(Hash)
148
+ @client
149
+ end&.keys
150
+ end
151
+
152
+ # How to Extract the (unparsed) value of the mail message fields.
130
153
  #
131
154
  # @return [String] version dependent method call
132
155
  def mail_field_value