Class: Asterisk::Mailcmd::Email

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmail

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

#astvarsObject

Returns the value of attribute astvars.



10
11
12
# File 'lib/asterisk/mailcmd/email.rb', line 10

def astvars
  @astvars
end

#charsetObject

Returns the value of attribute charset.



10
11
12
# File 'lib/asterisk/mailcmd/email.rb', line 10

def charset
  @charset
end

#delivery_methodObject

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_paramsObject

Returns the value of attribute delivery_params.



10
11
12
# File 'lib/asterisk/mailcmd/email.rb', line 10

def delivery_params
  @delivery_params
end

#htmlpartObject

Returns the value of attribute htmlpart.



10
11
12
# File 'lib/asterisk/mailcmd/email.rb', line 10

def htmlpart
  @htmlpart
end

#mailObject

Returns the value of attribute mail.



10
11
12
# File 'lib/asterisk/mailcmd/email.rb', line 10

def mail
  @mail
end

#rawdataObject

Returns the value of attribute rawdata.



10
11
12
# File 'lib/asterisk/mailcmd/email.rb', line 10

def rawdata
  @rawdata
end

#textpartObject

Returns the value of attribute textpart.



10
11
12
# File 'lib/asterisk/mailcmd/email.rb', line 10

def textpart
  @textpart
end

Class Method Details

.readObject

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

Raises:



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

#deliverObject



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_varsObject

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_charsetObject



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

Returns:

  • (Boolean)


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

Raises:

  • (ArgumentError)


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