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


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

def initialize(message)
  @message = message
end

Class Attribute Details

.adapter_nameObject (readonly)

Returns the value of attribute adapter_name


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

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
# 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:


60
61
62
63
64
65
66
67
68
69
# File 'lib/howitzer/email.rb', line 60

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:


78
79
80
81
82
83
84
85
# File 'lib/howitzer/email.rb', line 78

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)


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

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.


48
49
50
51
# File 'lib/howitzer/email.rb', line 48

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


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

def html_body
  message.html_body
end

#mail_fromString

Returns who has sent the email data in format: User Name <[email protected]>

Returns:


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

def mail_from
  message.mail_from
end

#mime_partObject

Allows to get email MIME attachment


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

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


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

def plain_text_body
  message.plain_text_body
end

#received_timeString

Returns email received time

Returns:

  • (String)

    email received time


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

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


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

def recipients
  message.recipients
end

#sender_emailString

Returns a sender user email

Returns:

  • (String)

    a sender user email


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

def sender_email
  message.sender_email
end

#textString?

Returns a mail text

Returns:

  • (String, nil)

    a mail text


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

def text
  message.text
end