Class: Cloudflare::EmailService::Message
- Inherits:
-
Object
- Object
- Cloudflare::EmailService::Message
- Defined in:
- lib/cloudflare/email_service/message.rb
Overview
Validates and serializes an outbound email into the JSON payload expected by the Cloudflare Email Service “send” endpoint.
Addresses may be given as:
* a String — "user@example.com" or "Display Name <user@example.com>"
* a Hash — { email: "user@example.com", name: "Display Name" }
(:address is accepted as an alias for :email)
* an Array of any of the above (for to / cc / bcc)
Instance Attribute Summary collapse
-
#attachments ⇒ Object
readonly
Returns the value of attribute attachments.
-
#bcc ⇒ Object
readonly
Returns the value of attribute bcc.
-
#cc ⇒ Object
readonly
Returns the value of attribute cc.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#html ⇒ Object
readonly
Returns the value of attribute html.
-
#reply_to ⇒ Object
readonly
Returns the value of attribute reply_to.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
-
#initialize(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil, attachments: nil, headers: nil) ⇒ Message
constructor
A new instance of Message.
-
#to_h ⇒ Hash
The request body, with nil/empty fields omitted.
- #validate! ⇒ self
Constructor Details
#initialize(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil, attachments: nil, headers: nil) ⇒ Message
Returns a new instance of Message.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/cloudflare/email_service/message.rb', line 17 def initialize(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil, attachments: nil, headers: nil) @from = from @to = to @cc = cc @bcc = bcc @reply_to = reply_to @subject = subject @html = html @text = text @attachments = @headers = headers end |
Instance Attribute Details
#attachments ⇒ Object (readonly)
Returns the value of attribute attachments.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def @attachments end |
#bcc ⇒ Object (readonly)
Returns the value of attribute bcc.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def bcc @bcc end |
#cc ⇒ Object (readonly)
Returns the value of attribute cc.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def cc @cc end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def from @from end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def headers @headers end |
#html ⇒ Object (readonly)
Returns the value of attribute html.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def html @html end |
#reply_to ⇒ Object (readonly)
Returns the value of attribute reply_to.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def reply_to @reply_to end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def subject @subject end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def text @text end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
14 15 16 |
# File 'lib/cloudflare/email_service/message.rb', line 14 def to @to end |
Instance Method Details
#to_h ⇒ Hash
Returns the request body, with nil/empty fields omitted.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cloudflare/email_service/message.rb', line 45 def to_h { from: normalize_address(from), to: normalize_recipients(to), subject: subject, cc: optional_recipients(cc), bcc: optional_recipients(bcc), reply_to: optional_address(reply_to), html: presence(html), text: presence(text), headers: presence(headers), attachments: (), }.compact end |
#validate! ⇒ self
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cloudflare/email_service/message.rb', line 33 def validate! raise ValidationError, "from is required" if blank?(from) raise ValidationError, "to is required" if blank?(to) raise ValidationError, "subject is required" if blank?(subject) if blank?(html) && blank?(text) raise ValidationError, "provide html and/or text body content" end self end |