Class: Howitzer::Email

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/howitzer/email.rb

Overview

This class describes single email

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Email

Returns a new instance of Email.



88
89
90
# File 'lib/howitzer/email.rb', line 88

def initialize(message)
  @message = message
end

Class Attribute Details

.adapter_nameObject (readonly)

Returns the value of attribute adapter_name.



21
22
23
# File 'lib/howitzer/email.rb', line 21

def adapter_name
  @adapter_name
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



9
10
11
# File 'lib/howitzer/email.rb', line 9

def message
  @message
end

Class Method Details

.adapter<MailAdapters::Abstract>

Returns a mail adapter class.

Returns:



13
14
15
16
17
18
# File 'lib/howitzer/email.rb', line 13

def self.adapter
  return @adapter if @adapter

  self.adapter = Howitzer.mail_adapter.to_sym
  @adapter
end

.adapter=(adapter_name) ⇒ Object

Specifies a mail adapter

Parameters:

  • adapter_name (String, Symbol)

    an email adapter name

Raises:



61
62
63
64
65
66
67
68
69
70
# 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

.find_by_recipient(recipient, params = {}) ⇒ Email

Searches a mail by a recepient

Parameters:

  • recipient (String)

    recepient’s email address

  • params (Hash) (defaults to: {})

    placeholders with appropriate values

Returns:

  • (Email)

    an instance of the email message

Raises:

See Also:



79
80
81
82
83
84
85
86
# 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

.subject(value) ⇒ Object

DSL method to specify a subject pattern directly in an email class

Examples:

class WelcomeEmail < Howitzer::Email
  subject 'Welcome on board :name'
end

WelcomeEmail.find_by_recipient('[email protected]', name: 'John')

Parameters:

  • value (String)

    an email subject with optional placeholders (strings started with : symbol)



35
36
37
38
# File 'lib/howitzer/email.rb', line 35

def subject(value)
  define_singleton_method(:subject_value) { value }
  private_class_method :subject_value
end

.wait_time(value) ⇒ Object

DSL method to specify a custom wait email time directly in an email class

Examples:

class WelcomeEmail < Howitzer::Email
  wait_time 10.minutes
end

Parameters:

  • value (Integer)

    an wait time for a particular email. If it is ommitted, default Howitzer.mail_wait_time will be used.



49
50
51
52
# 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

Instance Method Details

#html_bodyString?

Returns a html body of the email message.

Returns:

  • (String, nil)

    a html body of the email message



100
101
102
# File 'lib/howitzer/email.rb', line 100

def html_body
  message.html_body
end

#mail_fromString

Returns who has sent the email data in format: User Name <user@email>.

Returns:

  • (String)

    who has sent the email data in format: User Name <user@email>



112
113
114
# File 'lib/howitzer/email.rb', line 112

def mail_from
  message.mail_from
end

#mime_partObject

Allows to get email MIME attachment



136
137
138
# File 'lib/howitzer/email.rb', line 136

def mime_part
  message.mime_part
end

#plain_text_bodyString?

Returns a plain text of the email message.

Returns:

  • (String, nil)

    a plain text of the email message



94
95
96
# File 'lib/howitzer/email.rb', line 94

def plain_text_body
  message.plain_text_body
end

#received_timeString

Returns email received time.

Returns:

  • (String)

    email received time



124
125
126
# File 'lib/howitzer/email.rb', line 124

def received_time
  message.received_time
end

#recipientsArray<String>

Returns array of recipients who has received current email.

Returns:

  • (Array<String>)

    array of recipients who has received current email



118
119
120
# File 'lib/howitzer/email.rb', line 118

def recipients
  message.recipients
end

#sender_emailString

Returns a sender user email.

Returns:

  • (String)

    a sender user email



130
131
132
# File 'lib/howitzer/email.rb', line 130

def sender_email
  message.sender_email
end

#textString?

Returns a mail text.

Returns:

  • (String, nil)

    a mail text



106
107
108
# File 'lib/howitzer/email.rb', line 106

def text
  message.text
end