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
-
#check_version_of(gem_name, version) ⇒ Boolean
Check the version of a gem.
-
#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 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 the given ‘delivery_options’, ‘client’ and ‘delivery_settings’ are hashes and if one of that does, then check the ‘delivery_system’ is valid or not.
-
#exclude_delivey_settings_keys? ⇒ Boolean
Check that ‘delivery_settings’ has ‘delivery_system’ key or not.
-
#extract_attachments ⇒ Array
Extract attachments.
-
#extract_keys ⇒ Array/NilClass
Return ‘delivery_systems’ array if it’s exist.
-
#extract_keys_from_other_variables ⇒ Array/NilClass
Extract keys from ‘delivery_options’, ‘client’ or ‘delivery_settings’, depends on which is a hash.
-
#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 ‘deliviery_options’ or ‘client’ is a hash, or ‘delivery_settings’ is a hash but not contains ‘DELIVERY_SETTINGS_KEYS’ in first level.
-
#option_value_from(option) ⇒ Hash/Array/Class
Extract the value from the given options.
-
#send_via_smtp? ⇒ Boolean
Check that settings contains any SMTP related settings.
-
#sending_method_get ⇒ Symbol
Choose a ‘sending_method’ for the given conditions.
-
#settings ⇒ Hash
Extract ‘settings’.
Instance Method Details
#check_version_of(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 check_version_of(gem_name, version) requirement = Gem::Requirement.new(version) current_version = Gem.loaded_specs[gem_name].version requirement.satisfied_by?(current_version) end |
#client ⇒ Class
Extract ‘client’. If it’s a hash, then it’ll return the right client belongs to the delivery system. If it’s not a hash, it’ll return the given value. But if the value doesn’t a class, it’ll raise an error.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mail_plugger/mail_helper.rb', line 41 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 belongs to the delivery system. If ‘default_delivery_options’ is nil, it’ll return an empty hash. But if the value doesn’t a hash, it’ll raise an error.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mail_plugger/mail_helper.rb', line 87 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’.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mail_plugger/mail_helper.rb', line 103 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 Mail::Message object.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mail_plugger/mail_helper.rb', line 57 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, belongs to the delivery system. If it’s not a hash, it’ll return the given value. But if the value doesn’t an array, it’ll raise an error.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mail_plugger/mail_helper.rb', line 121 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 error.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/mail_plugger/mail_helper.rb', line 138 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 the given ‘delivery_options’, ‘client’ and ‘delivery_settings’ are hashes and if one of that does, then check 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 error.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/mail_plugger/mail_helper.rb', line 155 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 ‘delivery_system’ key or not. If ‘delivery_settings’ contains ‘DELIVERY_SETTINGS_KEYS’ then it returns false, else true.
174 175 176 177 178 |
# File 'lib/mail_plugger/mail_helper.rb', line 174 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.
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/mail_plugger/mail_helper.rb', line 183 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 ‘delivery_systems’ array if it’s exist. If not, then extract keys from ‘delivery_options’, ‘client’ or ‘delivery_settings’, depends on which is a hash. If none of these are hashes, then returns nil.
200 201 202 |
# File 'lib/mail_plugger/mail_helper.rb', line 200 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’, depends on which is a hash. If none of these are hashes, then returns nil.
208 209 210 211 212 213 214 215 216 |
# File 'lib/mail_plugger/mail_helper.rb', line 208 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 |
#mail_field_value ⇒ String
How to extract the (unparsed) value of the mail message fields.
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/mail_plugger/mail_helper.rb', line 221 def mail_field_value @mail_field_value ||= if check_version_of('mail', '> 2.7.0') %w[unparsed_value] elsif check_version_of('mail', '= 2.7.0') %w[instance_variable_get @unparsed_value] elsif check_version_of('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.
237 238 239 240 241 |
# File 'lib/mail_plugger/mail_helper.rb', line 237 def () return if .nil? .public_send(*mail_field_value) end |
#need_delivery_system? ⇒ Boolean
Check if either ‘deliviery_options’ or ‘client’ is a hash, or ‘delivery_settings’ is a hash but not contains ‘DELIVERY_SETTINGS_KEYS’ in first level.
248 249 250 251 252 |
# File 'lib/mail_plugger/mail_helper.rb', line 248 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.
259 260 261 262 263 264 265 |
# File 'lib/mail_plugger/mail_helper.rb', line 259 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 settings contains any SMTP related settings.
270 271 272 273 274 275 |
# File 'lib/mail_plugger/mail_helper.rb', line 270 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.
280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/mail_plugger/mail_helper.rb', line 280 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, belongs to the delivery system. If ‘delivery_settings’ is nil, it’ll return an empty hash. But if the value doesn’t a hash, it’ll raise an error.
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/mail_plugger/mail_helper.rb', line 299 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 |