Class: MmMail::Message
- Inherits:
-
Object
- Object
- MmMail::Message
- Defined in:
- lib/mmmail.rb
Overview
A Message object representing an Email to be passed to a Transport.
Instance Method Summary collapse
-
#[](k) ⇒ String
Allow access of fields by header name or symbolic representation.
-
#[]=(k, v) ⇒ Object
Allow access of fields by header name or symbolic representation.
-
#initialize(opts = {}) ⇒ Message
constructor
Creates a new message with associated fields.
-
#method_missing(sym, *args) ⇒ Object
Override this method to allow any call to obj.meth or obj.meth= to set a header field on this object.
-
#recipients_list ⇒ Array<String>
Returns all the recipients in the To field.
-
#respond_to?(sym) ⇒ Boolean
Override this method to verify if a field has been set.
-
#to_s ⇒ String
Returns the message in its full form as expected by an SMTP server.
-
#valid? ⇒ Boolean
Checks if the message is valid.
Constructor Details
#initialize(opts = {}) ⇒ Message
Creates a new message with associated fields.
144 145 146 147 148 149 150 151 |
# File 'lib/mmmail.rb', line 144 def initialize(opts = {}) defaults = { :from => 'nobody@localhost', :subject => '', :body => '' } @headers = defaults.merge(opts) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
Override this method to allow any call to obj.meth or obj.meth= to set a header field on this object.
178 179 180 181 182 183 184 185 186 |
# File 'lib/mmmail.rb', line 178 def method_missing(sym, *args) if sym.to_s =~ /=$/ self[sym.to_s[0..-2].to_sym] = args.first elsif @headers.has_key?(sym) self[sym] else super end end |
Instance Method Details
#[](k) ⇒ String
Allow access of fields by header name or symbolic representation
161 |
# File 'lib/mmmail.rb', line 161 def [](k) @headers[translate_header_to_sym(k)] end |
#[]=(k, v) ⇒ Object
Allow access of fields by header name or symbolic representation
169 |
# File 'lib/mmmail.rb', line 169 def []=(k, v) @headers[translate_header_to_sym(k)] = v end |
#recipients_list ⇒ Array<String>
Returns all the recipients in the To field.
211 212 213 |
# File 'lib/mmmail.rb', line 211 def recipients_list to.split(/\s*,\s*/) end |
#respond_to?(sym) ⇒ Boolean
Override this method to verify if a field has been set.
192 193 194 195 |
# File 'lib/mmmail.rb', line 192 def respond_to?(sym) return true if super @headers.has_key?(sym) end |
#to_s ⇒ String
Returns the message in its full form as expected by an SMTP server.
200 201 202 |
# File 'lib/mmmail.rb', line 200 def to_s [headers, body].join("\n") end |
#valid? ⇒ Boolean
Checks if the message is valid. Validity is based on having the From, To and Subject fields set. From and To must not be empty.
220 221 222 223 224 225 226 |
# File 'lib/mmmail.rb', line 220 def valid? [:from, :to].each do |field| return false if !self[field] || self[field].empty? end self[:subject] ? true : false end |