mail_plugger 1.0.0.beta1 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +54 -0
- data/README.md +35 -4
- data/lib/fake_plugger/delivery_method.rb +164 -0
- data/lib/fake_plugger/railtie.rb +14 -0
- data/lib/mail_plugger.rb +47 -28
- data/lib/mail_plugger/delivery_method.rb +42 -5
- data/lib/mail_plugger/mail_helper.rb +41 -18
- data/lib/mail_plugger/version.rb +1 -1
- metadata +16 -35
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -22
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -16
- data/.gitignore +0 -11
- data/.rspec +0 -3
- data/.rubocop.yml +0 -50
- data/.travis.yml +0 -22
- data/Appraisals +0 -13
- data/CODE_OF_CONDUCT.md +0 -14
- data/CONTRIBUTING.md +0 -26
- data/Gemfile +0 -19
- data/Gemfile.lock +0 -92
- data/Rakefile +0 -11
- data/bin/console +0 -17
- data/bin/setup +0 -8
- data/docs/usage_in_ruby_on_rails.md +0 -182
- data/docs/usage_in_script_or_console.md +0 -158
- data/gemfiles/.bundle/config +0 -2
- data/gemfiles/mail_2.6.gemfile +0 -15
- data/gemfiles/mail_2.6.gemfile.lock +0 -95
- data/gemfiles/mail_2.7.0.gemfile +0 -15
- data/gemfiles/mail_2.7.0.gemfile.lock +0 -93
- data/gemfiles/mail_2.7.gemfile +0 -15
- data/gemfiles/mail_2.7.gemfile.lock +0 -93
- data/images/mail_plugger.png +0 -0
- data/mail_plugger.gemspec +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ac25a71939ed021c1dafe8a7bba411205c9b9d597a9d46aa88ff60f3356155e
|
4
|
+
data.tar.gz: 92c49ac159e88bc093b04568bf9d69db0a645ff4c1b4e3fe40e7e6fed0be8853
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d62b1e577193fb89879dee0010e86c2dd88a3cceda7303f3d2c79a3258ec055dbc41397facf0f8098825dd1756d7359fc005af0e124efe6134d0c79e1b57d2df
|
7
|
+
data.tar.gz: 7657ff6afd711506a7fa5073473d55775f8cb9519626c5a1684576585e6afb385825c563ee0f2453bb9ccfde9a945f2b22e9a8390fb431fba7dd0c9b769e4f8b
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,59 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
|
4
|
+
## 1.1.1 (2021-01-21)
|
5
|
+
|
6
|
+
### Changes
|
7
|
+
|
8
|
+
* Change FakePlugger::DeliveryMethod to returns with the extracted delivery data instead of the message object.
|
9
|
+
* Update code documentations.
|
10
|
+
* Add missing FakePlugger description in the README.md.
|
11
|
+
|
12
|
+
|
13
|
+
## 1.1.0 (2021-01-18)
|
14
|
+
|
15
|
+
### New features
|
16
|
+
|
17
|
+
* Add FakePlugger to mock MailPlugger.
|
18
|
+
|
19
|
+
### Changes
|
20
|
+
|
21
|
+
* Fix typos in the documentations.
|
22
|
+
* Add more metadata to gempspec file.
|
23
|
+
|
24
|
+
|
25
|
+
## 1.0.1 (2021-01-15)
|
26
|
+
|
27
|
+
### Changes
|
28
|
+
|
29
|
+
* Tidy up the gemspec file and change which files contains in the gem. Now the gem is much smaller.
|
30
|
+
* Add a new mail plugger image which has a different canvas and use it in the README.md.
|
31
|
+
|
32
|
+
|
33
|
+
## 1.0.0 (2021-01-14)
|
34
|
+
|
35
|
+
### Changes
|
36
|
+
|
37
|
+
* Update gems.
|
38
|
+
* Remove `webmock` gem to clean up unused things.
|
39
|
+
* Update documentations.
|
40
|
+
|
41
|
+
|
42
|
+
## 1.0.0.rc1 (2021-01-13)
|
43
|
+
|
44
|
+
### Changes
|
45
|
+
|
46
|
+
* Change description and fix changelog_uri in gemspec file.
|
47
|
+
* Add/Change documentations.
|
48
|
+
* Update gems.
|
49
|
+
* Change `MailPlugger.plug_in` and `MailPlugger::MailHelper.delivery_options` methods to accept strings and symbols.
|
50
|
+
* Change `MailPlugger::MailHelper.delivery_data` method that we can retrieve message object as well.
|
51
|
+
* Check hash keys in `MailPlugger::MailHelper.delivery_system` method, so now if we are add wrong `delivey_system` then we are getting a meaningful error message.
|
52
|
+
* Change `MailPlugger::MailHelper.extract_attachments` method. If an attachment is inline then the hash contains the `filename` and `cid` as well.
|
53
|
+
* Add/Change tests.
|
54
|
+
* Change `MailPlugger::MailHelper.delivery_option` that returns with indifferent hash
|
55
|
+
|
56
|
+
|
3
57
|
## 1.0.0.beta1 (2021-01-02)
|
4
58
|
|
5
59
|
* Implement MailPlugger methods and functionality. See [README.md](https://github.com/norbertszivos/mail_plugger/blob/main/README.md)
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
# M<img src="https://
|
1
|
+
# M<img src="https://raw.githubusercontent.com/norbertszivos/mail_plugger/main/images/mail_plugger800x500.png" height="22" />ilPlugger
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/mail_plugger.svg)](https://badge.fury.io/rb/mail_plugger)
|
4
|
+
[![MIT license](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE.txt)
|
3
5
|
[![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop)
|
4
6
|
[![Build Status](https://travis-ci.com/norbertszivos/mail_plugger.svg?branch=main)](https://travis-ci.com/norbertszivos/mail_plugger)
|
5
7
|
[![Maintainability](https://api.codeclimate.com/v1/badges/bd2cda43214c111d8d16/maintainability)](https://codeclimate.com/github/norbertszivos/mail_plugger/maintainability)
|
@@ -25,15 +27,44 @@ Or install it yourself as:
|
|
25
27
|
|
26
28
|
## Usage
|
27
29
|
|
28
|
-
[
|
30
|
+
- [How to use MailPlugger.plug_in method](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_plug_in_method.md)
|
31
|
+
- [How to use MailPlugger::DeliveryMethod class](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_delivery_method.md)
|
32
|
+
- [How to use MailPlugger in a Ruby script or IRB console](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_script_or_console.md)
|
33
|
+
- [How to use MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md)
|
34
|
+
- [How to add API specific options to the mailer method in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_special_options_in_ruby_on_rails.md)
|
35
|
+
- [How to add attachments to the mailer method in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_attachments_in_ruby_on_rails.md)
|
36
|
+
- [How to use more delivey systems in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_more_delivery_system_in_ruby_on_rails.md)
|
37
|
+
- [How to use one delivey system with more send methods in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_one_delivery_system_with_more_send_methods_in_ruby_on_rails.md)
|
38
|
+
- [How to use AWS SES with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_aws_ses_in_ruby_on_rails.md)
|
39
|
+
- [How to use SparkPost with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_sparkpost_in_ruby_on_rails.md)
|
40
|
+
- [How to use SendGrid with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_sendgrid_in_ruby_on_rails.md)
|
41
|
+
- [How to use Mandrill with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_mandrill_in_ruby_on_rails.md)
|
42
|
+
- [How to use Postmark with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_postmark_in_ruby_on_rails.md)
|
43
|
+
- [How to use Mailgun with MailPlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_mailgun_in_ruby_on_rails.md)
|
44
|
+
|
45
|
+
# F<img src="https://raw.githubusercontent.com/norbertszivos/mail_plugger/main/images/fake_plugger800x500.png" height="22" />kePlugger
|
46
|
+
|
47
|
+
**FakePlugger** is a delivery method to mock **MailPlugger**. It is working similarly like **MailPlugger**, but it won't send any emails (but if we would like it is possible, just we should do manually). Also it can write out debug informations or we can manipulate the response with it.
|
29
48
|
|
30
|
-
|
49
|
+
## Usage
|
50
|
+
|
51
|
+
- [How to use FakePlugger::DeliveryMethod class](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_fake_plugger_delivery_method.md)
|
52
|
+
- [How to use FakePlugger in a Ruby script or IRB console](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_fake_plugger_in_script_or_console.md)
|
53
|
+
- [How to use FakePlugger in Ruby on Rails](https://github.com/norbertszivos/mail_plugger/blob/main/docs/usage_of_fake_plugger_in_ruby_on_rails.md)
|
31
54
|
|
32
55
|
## Development
|
33
56
|
|
34
57
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
35
58
|
|
36
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
60
|
+
|
61
|
+
To release a new version:
|
62
|
+
|
63
|
+
- Update CHANGELOG.md
|
64
|
+
- Update the version number in `version.rb` manually or use `gem-release` gem and run `gem bump -v major|minor|patch|rc|beta`.
|
65
|
+
- Build gem with `bundle exec rake build`.
|
66
|
+
- Run `bundle install` and `bundle exec appraisal install` to update gemfiles and commit the changes.
|
67
|
+
- Run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
37
68
|
|
38
69
|
## Contributing
|
39
70
|
|
@@ -0,0 +1,164 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FakePlugger
|
4
|
+
class DeliveryMethod < MailPlugger::DeliveryMethod
|
5
|
+
# Initialize FakePlugger delivery method attributes. If we are using
|
6
|
+
# MailPlugger.plug_in method, then these attributes can be nil, if not then
|
7
|
+
# we should set these attributes.
|
8
|
+
#
|
9
|
+
# @param [Hash] options check options below
|
10
|
+
# @option options [Class/Hash] client
|
11
|
+
# e.g. DefinedApiClientClass or { 'key' => DefinedApiClientClass }
|
12
|
+
#
|
13
|
+
# @option options [Array/Hash] delivery_options
|
14
|
+
# e.g. [:to, :from, :subject, :body] or
|
15
|
+
# { 'key' => [:to, :from, :subject, :body] }
|
16
|
+
#
|
17
|
+
# @option options [Hash] delivery_settings
|
18
|
+
# e.g. { return_response: true }
|
19
|
+
#
|
20
|
+
# @option options [String/Symbol] default_delivery_system
|
21
|
+
# e.g. 'defined_api'
|
22
|
+
#
|
23
|
+
# @option options [Boolean] debug
|
24
|
+
# if true it will show debug informations
|
25
|
+
#
|
26
|
+
# @option options [Boolean] raw_message
|
27
|
+
# if true it will show raw message
|
28
|
+
#
|
29
|
+
# @option options [String/Symbol/Array/Hash] response
|
30
|
+
# the deliver! method will return with this value or if this value is nil
|
31
|
+
# then it will return with the client object
|
32
|
+
def initialize(options = {})
|
33
|
+
super
|
34
|
+
|
35
|
+
@debug = options[:debug] || settings[:fake_plugger_debug] || false
|
36
|
+
@raw_message = options[:raw_message] ||
|
37
|
+
settings[:fake_plugger_raw_message] || false
|
38
|
+
@response = options[:response] || settings[:fake_plugger_response]
|
39
|
+
end
|
40
|
+
|
41
|
+
# Mock send message with the given client if the message parameter is a
|
42
|
+
# Mail::Message object. If 'response' parameter is nil then it will extract
|
43
|
+
# those information from the Mail::Message object which was provided in the
|
44
|
+
# 'delivery_options'. After that it generates a hash with these data and
|
45
|
+
# returns with the provided client class which has a 'deliver' method, but
|
46
|
+
# it won't call the 'deliver' method.
|
47
|
+
# If the 'response' parameter is a hash with 'return_delivery_data: true'
|
48
|
+
# then it will retrun with the extracted delivery data.
|
49
|
+
# If the 'response' parameter is not nil then retruns with that given data
|
50
|
+
# without call any other methods.
|
51
|
+
# Except if 'debug' is true. In this case it will call those methods which
|
52
|
+
# is calling in normal operation as well.
|
53
|
+
# If 'debug' is true then it prints out some debug informations.
|
54
|
+
# If 'raw_message' is true then it prints out raw message.
|
55
|
+
#
|
56
|
+
# @param [Mail::Message] message what we would like to send
|
57
|
+
#
|
58
|
+
# @return [Mail::Message/Hash] depends on the given value
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
#
|
62
|
+
# MailPlugger.plug_in('test_api_client') do |api|
|
63
|
+
# api.delivery_options = %i[from to subject body]
|
64
|
+
# api.delivery_settings = {
|
65
|
+
# fake_plugger_debug: true,
|
66
|
+
# fake_plugger_raw_message: true,
|
67
|
+
# fake_plugger_response: { response: 'OK' }
|
68
|
+
# }
|
69
|
+
# api.client = DefinedApiClientClass
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# message = Mail.new(from: 'from@example.com', to: 'to@example.com',
|
73
|
+
# subject: 'Test email', body: 'Test email body')
|
74
|
+
#
|
75
|
+
# FakePlugger::DeliveryMethod.new.deliver!(message)
|
76
|
+
#
|
77
|
+
# # or
|
78
|
+
#
|
79
|
+
# message = Mail.new(from: 'from@example.com', to: 'to@example.com',
|
80
|
+
# subject: 'Test email', body: 'Test email body')
|
81
|
+
#
|
82
|
+
# FakePlugger::DeliveryMethod.new(
|
83
|
+
# delivery_options: %i[from to subject body],
|
84
|
+
# client: DefinedApiClientClass,
|
85
|
+
# debug: true,
|
86
|
+
# raw_message: true,
|
87
|
+
# response: { response: 'OK' }
|
88
|
+
# ).deliver!(message)
|
89
|
+
#
|
90
|
+
def deliver!(message)
|
91
|
+
unless message.is_a?(Mail::Message)
|
92
|
+
raise MailPlugger::Error::WrongParameter,
|
93
|
+
'The given parameter is not a Mail::Message'
|
94
|
+
end
|
95
|
+
|
96
|
+
@message = message
|
97
|
+
|
98
|
+
show_debug_info if @debug
|
99
|
+
show_raw_message if @raw_message
|
100
|
+
|
101
|
+
return_with_response
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
# Return with a response which depends on the conditions.
|
107
|
+
def return_with_response
|
108
|
+
return client.new(delivery_data) if @response.nil?
|
109
|
+
if @response.is_a?(Hash) && @response[:return_delivery_data]
|
110
|
+
return delivery_data
|
111
|
+
end
|
112
|
+
|
113
|
+
@response
|
114
|
+
end
|
115
|
+
|
116
|
+
# Show debug informations from variables and methods.
|
117
|
+
def show_debug_info
|
118
|
+
puts <<~DEBUG_INFO
|
119
|
+
|
120
|
+
===================== FakePlugger::DeliveryMethod =====================
|
121
|
+
|
122
|
+
------------------------------ Variables ------------------------------
|
123
|
+
|
124
|
+
==> @client: #{@client.inspect}
|
125
|
+
|
126
|
+
==> @delivery_options: #{@delivery_options.inspect}"
|
127
|
+
|
128
|
+
==> @delivery_settings: #{@delivery_settings.inspect}"
|
129
|
+
|
130
|
+
==> @default_delivery_system: #{@default_delivery_system.inspect}
|
131
|
+
|
132
|
+
==> @message: #{@message.inspect}
|
133
|
+
|
134
|
+
------------------------------- Methods -------------------------------
|
135
|
+
|
136
|
+
==> client: #{client.inspect}
|
137
|
+
|
138
|
+
==> delivery_system: #{delivery_system.inspect}
|
139
|
+
|
140
|
+
==> delivery_options: #{delivery_options.inspect}
|
141
|
+
|
142
|
+
==> delivery_data: #{delivery_data.inspect}
|
143
|
+
|
144
|
+
==> settings: #{settings.inspect}
|
145
|
+
|
146
|
+
=======================================================================
|
147
|
+
|
148
|
+
DEBUG_INFO
|
149
|
+
end
|
150
|
+
|
151
|
+
# Show raw message for debug purpose.
|
152
|
+
def show_raw_message
|
153
|
+
puts <<~RAW_MESSAGE
|
154
|
+
|
155
|
+
============================ Mail::Message ============================
|
156
|
+
|
157
|
+
#{@message}
|
158
|
+
|
159
|
+
=======================================================================
|
160
|
+
|
161
|
+
RAW_MESSAGE
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FakePlugger
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer 'fake_plugger.add_delivery_method' do
|
6
|
+
ActiveSupport.on_load :action_mailer do
|
7
|
+
ActionMailer::Base.add_delivery_method(
|
8
|
+
:fake_plugger,
|
9
|
+
FakePlugger::DeliveryMethod
|
10
|
+
)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/mail_plugger.rb
CHANGED
@@ -7,28 +7,29 @@ require 'mail_plugger/delivery_method'
|
|
7
7
|
require 'mail_plugger/railtie' if defined?(Rails)
|
8
8
|
require 'mail_plugger/version'
|
9
9
|
|
10
|
+
require 'fake_plugger/delivery_method'
|
11
|
+
# If we are using this gem outside of Rails then do not load this code.
|
12
|
+
require 'fake_plugger/railtie' if defined?(Rails)
|
13
|
+
|
10
14
|
module MailPlugger
|
11
15
|
class << self
|
12
|
-
attr_reader :
|
16
|
+
attr_reader :client, :delivery_options, :delivery_settings
|
13
17
|
|
14
18
|
# Plug in defined API(s) class.
|
15
19
|
#
|
16
|
-
# @param [String] delivery_system the name of the API
|
20
|
+
# @param [String/Symbol] delivery_system the name of the API
|
17
21
|
#
|
18
|
-
# @example using Rails config/initializers/mail_plugger.rb
|
22
|
+
# @example using Rails `config/initializers/mail_plugger.rb`
|
19
23
|
#
|
20
|
-
# The defined API class should have an '
|
24
|
+
# # The defined API class should have an 'initialize' and a 'deliver'
|
25
|
+
# # method.
|
21
26
|
# class DefinedApiClientClass
|
22
|
-
# def initialize(options = {})
|
23
|
-
# @settings = { api_key:
|
24
|
-
# @
|
25
|
-
# @message_from = options[:from]
|
26
|
-
# @message_subject = options[:subject]
|
27
|
-
# @message_body_text = options[:text_part]
|
28
|
-
# @message_body_html = options[:html_part]
|
27
|
+
# def initialize(options = {})
|
28
|
+
# @settings = { api_key: '12345' }
|
29
|
+
# @options = options
|
29
30
|
# end
|
30
31
|
#
|
31
|
-
# def deliver
|
32
|
+
# def deliver
|
32
33
|
# API.new(@settings).client.post(generate_mail_hash)
|
33
34
|
# end
|
34
35
|
#
|
@@ -38,24 +39,24 @@ module MailPlugger
|
|
38
39
|
# {
|
39
40
|
# to: generate_recipients,
|
40
41
|
# from: {
|
41
|
-
# email: @
|
42
|
+
# email: @options[:from].first
|
42
43
|
# },
|
43
|
-
# subject: @
|
44
|
+
# subject: @options[:subject],
|
44
45
|
# content: [
|
45
46
|
# {
|
46
47
|
# type: 'text/plain',
|
47
|
-
# value: @
|
48
|
+
# value: @options[:text_part]
|
48
49
|
# },
|
49
50
|
# {
|
50
|
-
# type: 'text/html',
|
51
|
-
# value: @
|
51
|
+
# type: 'text/html; charset=UTF-8',
|
52
|
+
# value: @options[:html_part]
|
52
53
|
# }
|
53
54
|
# ]
|
54
55
|
# }
|
55
56
|
# end
|
56
57
|
#
|
57
58
|
# def generate_recipients
|
58
|
-
# @
|
59
|
+
# @options[:to].map do |to|
|
59
60
|
# {
|
60
61
|
# email: to
|
61
62
|
# }
|
@@ -63,18 +64,15 @@ module MailPlugger
|
|
63
64
|
# end
|
64
65
|
# end
|
65
66
|
#
|
66
|
-
# MailPlugger.plug_in('
|
67
|
+
# MailPlugger.plug_in('defined_api') do |api|
|
67
68
|
# # It will search these options in the Mail::Message object
|
68
69
|
# api.delivery_options = [:to, :from, :subject, :text_part, :html_part]
|
69
|
-
#
|
70
|
+
# api.delivery_settings = { return_response: true }
|
70
71
|
# api.client = DefinedApiClientClass
|
71
72
|
# end
|
72
73
|
#
|
73
74
|
def plug_in(delivery_system)
|
74
|
-
|
75
|
-
raise Error::WrongDeliverySystem, 'Delivery system is nil or empty. ' \
|
76
|
-
'You should provide correct MailPlugger.plug_in parameter'
|
77
|
-
end
|
75
|
+
check_value(delivery_system)
|
78
76
|
|
79
77
|
@delivery_system = delivery_system
|
80
78
|
|
@@ -83,15 +81,36 @@ module MailPlugger
|
|
83
81
|
raise Error::WrongPlugInOption, e.message
|
84
82
|
end
|
85
83
|
|
86
|
-
# Define 'delivery_options' and '
|
87
|
-
# generating a hash where the key is the
|
88
|
-
# set/use more than one API.
|
89
|
-
%w[delivery_options delivery_settings
|
84
|
+
# Define 'client', 'delivery_options' and 'delivery_settings' setter
|
85
|
+
# methods. These methods are generating a hash where the key is the
|
86
|
+
# 'delivery_system'. This let us to set/use more than one API.
|
87
|
+
%w[client delivery_options delivery_settings].each do |method|
|
90
88
|
define_method "#{method}=" do |value|
|
91
89
|
variable = instance_variable_get("@#{method}")
|
92
90
|
variable = instance_variable_set("@#{method}", {}) if variable.nil?
|
93
91
|
variable[@delivery_system] = value
|
94
92
|
end
|
95
93
|
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
# Check 'delivery_system' is valid or not. If it's not valid then
|
98
|
+
# it will raise an error.
|
99
|
+
#
|
100
|
+
# @param [String/Symbol] delivery_system the name of the API
|
101
|
+
def check_value(delivery_system)
|
102
|
+
if delivery_system.nil?
|
103
|
+
raise Error::WrongDeliverySystem, '"delivery_system" is nil'
|
104
|
+
end
|
105
|
+
|
106
|
+
if delivery_system.is_a?(String) && delivery_system.strip.empty?
|
107
|
+
raise Error::WrongDeliverySystem, '"delivery_system" is empty'
|
108
|
+
end
|
109
|
+
|
110
|
+
return if delivery_system.is_a?(String) || delivery_system.is_a?(Symbol)
|
111
|
+
|
112
|
+
raise Error::WrongDeliverySystem, '"delivery_system" does not a ' \
|
113
|
+
'String or Symbol'
|
114
|
+
end
|
96
115
|
end
|
97
116
|
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
|
11
|
+
# @param [Hash] options check 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
|
-
@
|
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,
|