Class: Poodle::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/poodle/email.rb

Overview

Email model representing an email to be sent

Examples:

Creating an email

email = Poodle::Email.new(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Hello World",
  html: "<h1>Hello!</h1>",
  text: "Hello!"
)

HTML only email

email = Poodle::Email.new(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Newsletter",
  html: "<h1>Newsletter</h1><p>Content here</p>"
)

Text only email

email = Poodle::Email.new(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Simple notification",
  text: "This is a simple text notification."
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to:, subject:, html: nil, text: nil) ⇒ Email

Initialize a new Email

Parameters:

  • from (String)

    sender email address

  • to (String)

    recipient email address

  • subject (String)

    email subject

  • html (String, nil) (defaults to: nil)

    HTML content

  • text (String, nil) (defaults to: nil)

    plain text content

Raises:



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

def initialize(from:, to:, subject:, html: nil, text: nil)
  @from = from
  @to = to
  @subject = subject
  @html = html
  @text = text

  validate!
  freeze # Make the object immutable
end

Instance Attribute Details

#fromString (readonly)

Returns sender email address.

Returns:

  • (String)

    sender email address



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

def from
  @from
end

#htmlString? (readonly)

Returns HTML content.

Returns:

  • (String, nil)

    HTML content



45
46
47
# File 'lib/poodle/email.rb', line 45

def html
  @html
end

#subjectString (readonly)

Returns email subject.

Returns:

  • (String)

    email subject



42
43
44
# File 'lib/poodle/email.rb', line 42

def subject
  @subject
end

#textString? (readonly)

Returns plain text content.

Returns:

  • (String, nil)

    plain text content



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

def text
  @text
end

#toString (readonly)

Returns recipient email address.

Returns:

  • (String)

    recipient email address



39
40
41
# File 'lib/poodle/email.rb', line 39

def to
  @to
end

Instance Method Details

#content_sizeInteger

Get the size of the email content in bytes

Returns:

  • (Integer)

    total size of HTML and text content



118
119
120
121
122
123
# File 'lib/poodle/email.rb', line 118

def content_size
  size = 0
  size += @html.bytesize if @html
  size += @text.bytesize if @text
  size
end

#html?Boolean

Check if email has HTML content

Returns:

  • (Boolean)

    true if HTML content is present



97
98
99
# File 'lib/poodle/email.rb', line 97

def html?
  !@html.nil? && !@html.empty?
end

#multipart?Boolean

Check if email is multipart (has both HTML and text)

Returns:

  • (Boolean)

    true if both HTML and text content are present



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

def multipart?
  html? && text?
end

#text?Boolean

Check if email has text content

Returns:

  • (Boolean)

    true if text content is present



104
105
106
# File 'lib/poodle/email.rb', line 104

def text?
  !@text.nil? && !@text.empty?
end

#to_hHash

Convert email to hash for API request

Returns:

  • (Hash)

    email data as hash



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/poodle/email.rb', line 73

def to_h
  data = {
    from: @from,
    to: @to,
    subject: @subject
  }

  data[:html] = @html if @html
  data[:text] = @text if @text

  data
end

#to_json(*args) ⇒ String

Convert email to JSON string

Returns:

  • (String)

    email data as JSON



89
90
91
92
# File 'lib/poodle/email.rb', line 89

def to_json(*args)
  require "json"
  to_h.to_json(*args)
end

#valid_email?(email) ⇒ Boolean (private)

Check if an email address is valid

Parameters:

  • email (String)

    the email address to validate

Returns:

  • (Boolean)

    true if the email is valid



180
181
182
183
184
185
186
187
188
# File 'lib/poodle/email.rb', line 180

def valid_email?(email)
  return false if email.nil? || email.empty?

  # Basic email validation using URI::MailTo
  uri = URI::MailTo.build([email, nil])
  uri.to_s == "mailto:#{email}"
rescue URI::InvalidComponentError, ArgumentError
  false
end

#validate!Object (private)

Validate email data

Raises:



130
131
132
133
134
135
# File 'lib/poodle/email.rb', line 130

def validate!
  validate_required_fields!
  validate_email_addresses!
  validate_content!
  validate_content_size!
end

#validate_content!Object (private)

Validate content presence

Raises:



157
158
159
160
161
# File 'lib/poodle/email.rb', line 157

def validate_content!
  return if html? || text?

  raise ValidationError.invalid_content
end

#validate_content_size!Object (private)

Validate content size

Raises:



166
167
168
169
170
171
172
173
174
# File 'lib/poodle/email.rb', line 166

def validate_content_size!
  max_size = Configuration::MAX_CONTENT_SIZE

  raise ValidationError.content_too_large("html", max_size) if @html && @html.bytesize > max_size

  return unless @text && @text.bytesize > max_size

  raise ValidationError.content_too_large("text", max_size)
end

#validate_email_addresses!Object (private)

Validate email addresses

Raises:



149
150
151
152
# File 'lib/poodle/email.rb', line 149

def validate_email_addresses!
  raise ValidationError.invalid_email(@from, field: "from") unless valid_email?(@from)
  raise ValidationError.invalid_email(@to, field: "to") unless valid_email?(@to)
end

#validate_required_fields!Object (private)

Validate required fields

Raises:



140
141
142
143
144
# File 'lib/poodle/email.rb', line 140

def validate_required_fields!
  raise ValidationError.missing_field("from") if @from.nil? || @from.empty?
  raise ValidationError.missing_field("to") if @to.nil? || @to.empty?
  raise ValidationError.missing_field("subject") if @subject.nil? || @subject.empty?
end