class Howitzer::Email
This class describes single email
Attributes
Public Class Methods
@return [<MailAdapters::Abstract>] a mail adapter class
# File lib/howitzer/email.rb, line 13 def self.adapter return @adapter if @adapter self.adapter = Howitzer.mail_adapter.to_sym @adapter end
Specifies a mail adapter @param adapter_name
[String, Symbol] an email adapter name @raise [NoMailAdapterError] when the adapter name is not String or Symbol
# File lib/howitzer/email.rb, line 61 def self.adapter=(adapter_name) @adapter_name = adapter_name case adapter_name when Symbol, String require "howitzer/mail_adapters/#{adapter_name}" @adapter = MailAdapters.const_get(adapter_name.to_s.capitalize.to_s) else raise Howitzer::NoMailAdapterError end end
Searches a mail by a recepient @param recipient [String] recepient’s email address @param params [Hash] placeholders with appropriate values @raise [NoEmailSubjectError] when a subject is not specified for the email class @return [Email] an instance of the email message @see .subject
# File lib/howitzer/email.rb, line 79 def self.find_by_recipient(recipient, params = {}) if defined?(subject_value).nil? || subject_value.nil? raise Howitzer::NoEmailSubjectError, "Please specify email subject. For example:\n" \ "class SomeEmail < Howitzer::Email\n " \ "subject ‘some subject text’\nend" end new(adapter.find(recipient, expand_subject(params), wait: wait_time_value)) end
# File lib/howitzer/email.rb, line 88 def initialize(message) @message = message end
Protected Class Methods
DSL method to specify a subject pattern directly in an email class @param value [String] an email subject with optional placeholders (strings started with : symbol) @example
class WelcomeEmail < Howitzer::Email subject 'Welcome on board :name' end WelcomeEmail.find_by_recipient('john.smith@example.com', name: 'John')
@!visibility public
# File lib/howitzer/email.rb, line 35 def subject(value) define_singleton_method(:subject_value) { value } private_class_method :subject_value end
DSL method to specify a custom wait email time directly in an email class @param value [Integer] an wait time for a particular email.
If it is ommitted, default Howitzer.mail_wait_time will be used.
@example
class WelcomeEmail < Howitzer::Email wait_time 10.minutes end
@!visibility public
# File lib/howitzer/email.rb, line 49 def wait_time(value) define_singleton_method(:wait_time_value) { value } private_class_method :wait_time_value end
Private Class Methods
# File lib/howitzer/email.rb, line 140 def self.expand_subject(params) params.inject(subject_value.dup) { |a, (k, v)| a.sub(":#{k}", v.to_s) } end
Public Instance Methods
@return [String, nil] a html body of the email message
# File lib/howitzer/email.rb, line 100 def html_body message.html_body end
@return [String] who has sent the email data in format: User Name <user@email>
# File lib/howitzer/email.rb, line 112 def mail_from message.mail_from end
Allows to get email MIME attachment
# File lib/howitzer/email.rb, line 136 def mime_part message.mime_part end
@return [String, nil] a plain text of the email message
# File lib/howitzer/email.rb, line 94 def plain_text_body message.plain_text_body end
@return [String] email received time
# File lib/howitzer/email.rb, line 124 def received_time message.received_time end
@return [Array<String>] array of recipients who has received current email
# File lib/howitzer/email.rb, line 118 def recipients message.recipients end
@return [String] a sender user email
# File lib/howitzer/email.rb, line 130 def sender_email message.sender_email end
@return [String, nil] a mail text
# File lib/howitzer/email.rb, line 106 def text message.text end