Class: Asterisk::Mailcmd::Email
- Inherits:
-
Object
- Object
- Asterisk::Mailcmd::Email
- Defined in:
- lib/asterisk/mailcmd/email.rb
Instance Attribute Summary collapse
-
#astvars ⇒ Object
Returns the value of attribute astvars.
-
#charset ⇒ Object
Returns the value of attribute charset.
-
#delivery_method ⇒ Object
Returns the value of attribute delivery_method.
-
#delivery_params ⇒ Object
Returns the value of attribute delivery_params.
-
#htmlpart ⇒ Object
Returns the value of attribute htmlpart.
-
#mail ⇒ Object
Returns the value of attribute mail.
-
#rawdata ⇒ Object
Returns the value of attribute rawdata.
-
#textpart ⇒ Object
Returns the value of attribute textpart.
Class Method Summary collapse
-
.read ⇒ Object
Asterisk will send generated text to STDIN Read it and store in instance variable.
- .set_and_send(params) ⇒ Object
Instance Method Summary collapse
- #deliver ⇒ Object
-
#extract_vars ⇒ Object
extract variables from email body.
- #get_charset ⇒ Object
- #html_tmpl(tmpl_text) ⇒ Object
-
#initialize ⇒ Email
constructor
A new instance of Email.
-
#raw_valid? ⇒ Boolean
Valid raw email must endin with full stop (period) on new line.
- #set_date(date = nil) ⇒ Object
- #set_delivery(method = :sendmail, params = {}) ⇒ Object
- #text_tmpl(tmpl_text) ⇒ Object
- #tmpl(tmpl_text, type = :html) ⇒ Object
Constructor Details
#initialize ⇒ Email
Returns a new instance of Email.
47 48 49 50 51 |
# File 'lib/asterisk/mailcmd/email.rb', line 47 def initialize @rawdata = $stdin.read @mail = Mail.read_from_string(@rawdata) set_delivery end |
Instance Attribute Details
#astvars ⇒ Object
Returns the value of attribute astvars.
10 11 12 |
# File 'lib/asterisk/mailcmd/email.rb', line 10 def astvars @astvars end |
#charset ⇒ Object
Returns the value of attribute charset.
10 11 12 |
# File 'lib/asterisk/mailcmd/email.rb', line 10 def charset @charset end |
#delivery_method ⇒ Object
Returns the value of attribute delivery_method.
10 11 12 |
# File 'lib/asterisk/mailcmd/email.rb', line 10 def delivery_method @delivery_method end |
#delivery_params ⇒ Object
Returns the value of attribute delivery_params.
10 11 12 |
# File 'lib/asterisk/mailcmd/email.rb', line 10 def delivery_params @delivery_params end |
#htmlpart ⇒ Object
Returns the value of attribute htmlpart.
10 11 12 |
# File 'lib/asterisk/mailcmd/email.rb', line 10 def htmlpart @htmlpart end |
#mail ⇒ Object
Returns the value of attribute mail.
10 11 12 |
# File 'lib/asterisk/mailcmd/email.rb', line 10 def mail @mail end |
#rawdata ⇒ Object
Returns the value of attribute rawdata.
10 11 12 |
# File 'lib/asterisk/mailcmd/email.rb', line 10 def rawdata @rawdata end |
#textpart ⇒ Object
Returns the value of attribute textpart.
10 11 12 |
# File 'lib/asterisk/mailcmd/email.rb', line 10 def textpart @textpart end |
Class Method Details
.read ⇒ Object
Asterisk will send generated text to STDIN Read it and store in instance variable
22 23 24 |
# File 'lib/asterisk/mailcmd/email.rb', line 22 def read Email.new end |
.set_and_send(params) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/asterisk/mailcmd/email.rb', line 26 def set_and_send params email = Email.read email.extract_vars email.set_date params[:date] unless params[:date].nil? email.charset = params[:charset] unless params[:charset].nil? # set templates raise InvalidTxtTmpl, "Text template parameter required" if params[:text_tmpl].nil? raise InvalidHtmlTmpl, "HTML template parameter required" if params[:html_tmpl].nil? Settings.read params[:text_tmpl], :text Settings.read params[:html_tmpl] email.html_tmpl Settings.html_tmpl email.text_tmpl Settings.text_tmpl # delivery if params[:delivery_method] delivery_params = params[:delivery_params] || {} email.set_delivery params[:delivery_method], delivery_params end email.deliver end |
Instance Method Details
#deliver ⇒ Object
103 104 105 106 |
# File 'lib/asterisk/mailcmd/email.rb', line 103 def deliver @mail.delivery_method @delivery_method, @delivery_params @mail.deliver end |
#extract_vars ⇒ Object
extract variables from email body
60 61 62 63 64 65 66 67 |
# File 'lib/asterisk/mailcmd/email.rb', line 60 def extract_vars @astvars = Hash.new @mail.text_part.body.decoded.lines do |line| if /^(?<name>[^:]+):(?<val>.*)$/ =~ line.chomp @astvars[name.to_sym] = val end end end |
#get_charset ⇒ Object
113 114 115 |
# File 'lib/asterisk/mailcmd/email.rb', line 113 def get_charset @charset || 'UTF-8' end |
#html_tmpl(tmpl_text) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/asterisk/mailcmd/email.rb', line 76 def html_tmpl tmpl_text tmpl tmpl_text, :html data = @htmlpart cnt_type = 'text/html; charset='<<get_charset @mail.html_part = Mail::Part.new do content_type cnt_type body data end end |
#raw_valid? ⇒ Boolean
Valid raw email must endin with full stop (period) on new line
55 56 57 |
# File 'lib/asterisk/mailcmd/email.rb', line 55 def raw_valid? @rawdata.split.pop.eql? ?. end |
#set_date(date = nil) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/asterisk/mailcmd/email.rb', line 94 def set_date date = nil if date.nil? @mail.date Time.now else raise ArgumentError, "Invalid date" unless date.is_a? Time @mail.date date end end |
#set_delivery(method = :sendmail, params = {}) ⇒ Object
108 109 110 111 |
# File 'lib/asterisk/mailcmd/email.rb', line 108 def set_delivery method = :sendmail, params = {} @delivery_method = method @delivery_params = params end |
#text_tmpl(tmpl_text) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/asterisk/mailcmd/email.rb', line 86 def text_tmpl tmpl_text tmpl tmpl_text, :text data = @textpart @mail.text_part = Mail::Part.new do body data end end |
#tmpl(tmpl_text, type = :html) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/asterisk/mailcmd/email.rb', line 69 def tmpl tmpl_text, type = :html tmpl = ERB.new(tmpl_text) raise ArgumentError, "No Asterisk variables set" if @astvars.nil? instance_variable_set "@#{type}part", tmpl.result(binding) rescue raise ArgumentError, "Invalid type #{type.to_sym}" end |