Class: Poodle::Email
- Inherits:
-
Object
- Object
- Poodle::Email
- Defined in:
- lib/poodle/email.rb
Overview
Email model representing an email to be sent
Instance Attribute Summary collapse
-
#from ⇒ String
readonly
Sender email address.
-
#html ⇒ String?
readonly
HTML content.
-
#subject ⇒ String
readonly
Email subject.
-
#text ⇒ String?
readonly
Plain text content.
-
#to ⇒ String
readonly
Recipient email address.
Instance Method Summary collapse
-
#content_size ⇒ Integer
Get the size of the email content in bytes.
-
#html? ⇒ Boolean
Check if email has HTML content.
-
#initialize(from:, to:, subject:, html: nil, text: nil) ⇒ Email
constructor
Initialize a new Email.
-
#multipart? ⇒ Boolean
Check if email is multipart (has both HTML and text).
-
#text? ⇒ Boolean
Check if email has text content.
-
#to_h ⇒ Hash
Convert email to hash for API request.
-
#to_json(*args) ⇒ String
Convert email to JSON string.
-
#valid_email?(email) ⇒ Boolean
private
Check if an email address is valid.
-
#validate! ⇒ Object
private
Validate email data.
-
#validate_content! ⇒ Object
private
Validate content presence.
-
#validate_content_size! ⇒ Object
private
Validate content size.
-
#validate_email_addresses! ⇒ Object
private
Validate email addresses.
-
#validate_required_fields! ⇒ Object
private
Validate required fields.
Constructor Details
#initialize(from:, to:, subject:, html: nil, text: nil) ⇒ Email
Initialize a new Email
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
#from ⇒ String (readonly)
Returns sender email address.
36 37 38 |
# File 'lib/poodle/email.rb', line 36 def from @from end |
#html ⇒ String? (readonly)
Returns HTML content.
45 46 47 |
# File 'lib/poodle/email.rb', line 45 def html @html end |
#subject ⇒ String (readonly)
Returns email subject.
42 43 44 |
# File 'lib/poodle/email.rb', line 42 def subject @subject end |
#text ⇒ String? (readonly)
Returns plain text content.
48 49 50 |
# File 'lib/poodle/email.rb', line 48 def text @text end |
#to ⇒ String (readonly)
Returns recipient email address.
39 40 41 |
# File 'lib/poodle/email.rb', line 39 def to @to end |
Instance Method Details
#content_size ⇒ Integer
Get the size of the email content in bytes
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
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)
111 112 113 |
# File 'lib/poodle/email.rb', line 111 def multipart? html? && text? end |
#text? ⇒ Boolean
Check if email has text content
104 105 106 |
# File 'lib/poodle/email.rb', line 104 def text? !@text.nil? && !@text.empty? end |
#to_h ⇒ Hash
Convert email to hash for API request
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
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
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
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
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
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
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
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 |