Module: MailPlugger::MailHelper
- Included in:
- DeliveryMethod
- Defined in:
- lib/mail_plugger/mail_helper.rb
Constant Summary collapse
- DELIVERY_SETTINGS_KEYS =
%i[ fake_plugger_debug fake_plugger_raw_message fake_plugger_response fake_plugger_use_mail_grabber return_response smtp_settings ].freeze
- SENDING_METHODS =
%i[ default_delivery_system plugged_in_first random round_robin ].freeze
Instance Method Summary collapse
-
#client ⇒ Class
Extract ‘client’.
-
#default_data ⇒ Hash
Extract ‘default_delivery_options’.
-
#default_delivery_system_get ⇒ Stirng/NilClass
Tries to set up the ‘default_delivery_system’.
-
#delivery_data ⇒ Hash
Collects data from the Mail::Message object.
-
#delivery_options ⇒ Array
Extract ‘delivery_options’.
-
#delivery_system ⇒ String
Extract ‘delivery_system’ from the Mail::Message object, or if it’s not defined, then use the default one.
-
#delivery_system_value_check ⇒ Object
Check that the given ‘delivery_options’, ‘client’, and ‘delivery_settings’ are hashes, and if one of those is, then check whether the ‘delivery_system’ is valid or not.
-
#exclude_delivey_settings_keys? ⇒ Boolean
Check that ‘delivery_settings’ has the ‘delivery_system’ key or not.
-
#extract_attachments ⇒ Array
Extract attachments.
-
#extract_keys ⇒ Array/NilClass
Return the ‘delivery_systems’ array if it exists.
-
#extract_keys_from_other_variables ⇒ Array/NilClass
Extract keys from ‘delivery_options’, ‘client’, or ‘delivery_settings’, depending on which is a hash.
-
#gem_version_satisfied?(gem_name, version) ⇒ Boolean
Check the version of a gem.
-
#mail_field_value ⇒ String
How to extract the (unparsed) value of the mail message fields.
-
#message_field_value_from(message_field) ⇒ String/Boolean/Hash
Extract the (unparsed) value of the mail message fields.
-
#need_delivery_system? ⇒ Boolean
Check if either ‘delivery_options’ or ‘client’ is a hash, or ‘delivery_settings’ is a hash but does not contain ‘DELIVERY_SETTINGS_KEYS’ at the first level.
-
#option_value_from(option) ⇒ Hash/Array/Class
Extract the value from the given options.
-
#send_via_smtp? ⇒ Boolean
Check that the settings contain any SMTP-related settings.
-
#sending_method_get ⇒ Symbol
Choose a ‘sending_method’ for the given conditions.
-
#settings ⇒ Hash
Extract ‘settings’.
Instance Method Details
#client ⇒ Class
Extract ‘client’. If it’s a hash, then it’ll return the right client that belongs to the delivery system. If it’s not a hash, it’ll return the given value. But if the value isn’t a class, it’ll raise an error.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mail_plugger/mail_helper.rb', line 42 def client api_client = option_value_from(@client) unless api_client.is_a?(Class) raise Error::WrongApiClient, '"client" does not a Class' end unless api_client.method_defined?(:deliver) raise Error::WrongApiClient, '"client" does not have "deliver" method' end api_client end |
#default_data ⇒ Hash
Extract ‘default_delivery_options’. If it’s a hash, then it’ll return the right sending options that belong to the delivery system. If ‘default_delivery_options’ is nil, it’ll return an empty hash. But if the value isn’t a hash, it’ll raise an error.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mail_plugger/mail_helper.rb', line 88 def default_data = option_value_from(@default_delivery_options) return {} if .nil? unless .is_a?(Hash) raise Error::WrongDefaultDeliveryOptions, '"default_delivery_options" does not a Hash' end .transform_keys(&:to_sym) end |
#default_delivery_system_get ⇒ Stirng/NilClass
Tries to set up the ‘default_delivery_system’.
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mail_plugger/mail_helper.rb', line 104 def default_delivery_system_get case sending_method_get when :default_delivery_system @passed_default_delivery_system when :plugged_in_first extract_keys&.first when :random extract_keys&.sample when :round_robin @rotatable_delivery_systems&.next end end |
#delivery_data ⇒ Hash
Collects data from the Mail::Message object.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mail_plugger/mail_helper.rb', line 58 def delivery_data data = {} .each do |option| option = option.to_sym unless option.is_a?(Symbol) data[option] = case option when :from, :to, :cc, :bcc, :subject @message.public_send(option) when :attachments when :body, :html_part, :text_part @message.public_send(option)&.decoded when :message_obj @message else (@message[option]) end end Mail::IndifferentHash.new(default_data.merge(data)) end |
#delivery_options ⇒ Array
Extract ‘delivery_options’. If it’s a hash, then it’ll return the right options that belong to the delivery system. If it’s not a hash, it’ll return the given value. But if the value isn’t an array, it’ll raise an error.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mail_plugger/mail_helper.rb', line 123 def = option_value_from(@delivery_options) unless .is_a?(Array) raise Error::WrongDeliveryOptions, '"delivery_options" does not an Array' end end |
#delivery_system ⇒ String
Extract ‘delivery_system’ from the Mail::Message object, or if it’s not defined, then use the default one. If it’s still nil and one of the ‘delivery_options’, ‘client’, and/or ‘delivery_settings’ is a hash and ‘delivery_settings’ doesn’t contain ‘delivery_system’, then raise an error.
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mail_plugger/mail_helper.rb', line 141 def delivery_system return @delivery_system unless @delivery_system.nil? @delivery_system = (@message && (@message[:delivery_system])) || @default_delivery_system delivery_system_value_check @delivery_system end |
#delivery_system_value_check ⇒ Object
Check that the given ‘delivery_options’, ‘client’, and ‘delivery_settings’ are hashes, and if one of those is, then check whether the ‘delivery_system’ is valid or not. If the given ‘delivery_system’ is nil or doesn’t match with extracted keys, then it will raise an error.
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/mail_plugger/mail_helper.rb', line 158 def delivery_system_value_check return unless need_delivery_system? if @delivery_system.nil? raise Error::WrongDeliverySystem, '"delivery_system" was not defined as a Mail::Message parameter' end return if extract_keys&.include?(@delivery_system) raise Error::WrongDeliverySystem, "\"delivery_system\" '#{@delivery_system}' does not exist" end |
#exclude_delivey_settings_keys? ⇒ Boolean
Check that ‘delivery_settings’ has the ‘delivery_system’ key or not. If ‘delivery_settings’ contains ‘DELIVERY_SETTINGS_KEYS’, then it returns false, else true.
177 178 179 180 181 |
# File 'lib/mail_plugger/mail_helper.rb', line 177 def exclude_delivey_settings_keys? @delivery_settings.keys.none? do |key| DELIVERY_SETTINGS_KEYS.include?(key.to_sym) end end |
#extract_attachments ⇒ Array
Extract attachments.
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mail_plugger/mail_helper.rb', line 186 def @message.&.map do || hash = .inline? ? { cid: .cid } : {} hash.merge( filename: .filename, type: .mime_type, content: Base64.encode64(.decoded) ) end end |
#extract_keys ⇒ Array/NilClass
Return the ‘delivery_systems’ array if it exists. If not, then extract keys from ‘delivery_options’, ‘client’, or ‘delivery_settings’, depending on which is a hash. If none of these are hashes, then returns nil.
203 204 205 |
# File 'lib/mail_plugger/mail_helper.rb', line 203 def extract_keys @delivery_systems || extract_keys_from_other_variables end |
#extract_keys_from_other_variables ⇒ Array/NilClass
Extract keys from ‘delivery_options’, ‘client’, or ‘delivery_settings’, depending on which is a hash. If none of these are hashes, then returns nil.
212 213 214 215 216 217 218 219 220 |
# File 'lib/mail_plugger/mail_helper.rb', line 212 def extract_keys_from_other_variables if @delivery_options.is_a?(Hash) @delivery_options elsif @client.is_a?(Hash) @client elsif @delivery_settings.is_a?(Hash) && exclude_delivey_settings_keys? @delivery_settings end&.keys end |
#gem_version_satisfied?(gem_name, version) ⇒ Boolean
Check the version of a gem.
29 30 31 32 33 34 |
# File 'lib/mail_plugger/mail_helper.rb', line 29 def gem_version_satisfied?(gem_name, version) requirement = Gem::Requirement.new(version) current_version = Gem.loaded_specs[gem_name].version requirement.satisfied_by?(current_version) end |
#mail_field_value ⇒ String
How to extract the (unparsed) value of the mail message fields.
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/mail_plugger/mail_helper.rb', line 225 def mail_field_value @mail_field_value ||= if gem_version_satisfied?('mail', '> 2.7.0') %w[unparsed_value] elsif gem_version_satisfied?('mail', '= 2.7.0') %w[instance_variable_get @unparsed_value] elsif gem_version_satisfied?('mail', '< 2.7.0') %w[instance_variable_get @value] end end |
#message_field_value_from(message_field) ⇒ String/Boolean/Hash
Extract the (unparsed) value of the mail message fields.
241 242 243 244 245 |
# File 'lib/mail_plugger/mail_helper.rb', line 241 def () return if .nil? .public_send(*mail_field_value) end |
#need_delivery_system? ⇒ Boolean
Check if either ‘delivery_options’ or ‘client’ is a hash, or ‘delivery_settings’ is a hash but does not contain ‘DELIVERY_SETTINGS_KEYS’ at the first level.
252 253 254 255 256 |
# File 'lib/mail_plugger/mail_helper.rb', line 252 def need_delivery_system? @delivery_options.is_a?(Hash) || @client.is_a?(Hash) || (@delivery_settings.is_a?(Hash) && exclude_delivey_settings_keys?) end |
#option_value_from(option) ⇒ Hash/Array/Class
Extract the value from the given options.
263 264 265 266 267 268 269 |
# File 'lib/mail_plugger/mail_helper.rb', line 263 def option_value_from(option) if option.is_a?(Hash) && option[delivery_system] option[delivery_system] else option end end |
#send_via_smtp? ⇒ Boolean
Check that the settings contain any SMTP-related settings.
274 275 276 277 278 279 |
# File 'lib/mail_plugger/mail_helper.rb', line 274 def send_via_smtp? return true if settings[:smtp_settings].is_a?(Hash) && settings[:smtp_settings].any? false end |
#sending_method_get ⇒ Symbol
Choose a ‘sending_method’ for the given conditions.
284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/mail_plugger/mail_helper.rb', line 284 def sending_method_get if @sending_method.nil? && !@passed_default_delivery_system.nil? :default_delivery_system elsif @sending_method.nil? || !SENDING_METHODS.include?(@sending_method.to_sym) || (@sending_method.to_sym == :default_delivery_system && @passed_default_delivery_system.nil?) :plugged_in_first else @sending_method.to_sym end end |
#settings ⇒ Hash
Extract ‘settings’. If ‘delivery_settings’ is a hash, then it’ll return the right settings that belong to the delivery system. If ‘delivery_settings’ is nil, it’ll return an empty hash. But if the value isn’t a hash, it’ll raise an error.
303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/mail_plugger/mail_helper.rb', line 303 def settings return @settings unless @settings.nil? extracted_settings = option_value_from(@delivery_settings) || {} unless extracted_settings.is_a?(Hash) raise Error::WrongDeliverySettings, '"delivery_settings" does not a Hash' end @settings = extracted_settings.transform_keys(&:to_sym) end |