lib/openwfe/participants/enoparticipants.rb in openwferu-0.9.10.653 vs lib/openwfe/participants/enoparticipants.rb in openwferu-0.9.11

- old
+ new

@@ -45,12 +45,107 @@ module OpenWFE # - # This participant is used to send an email notification + # MailParticipant is a simplification of EmailNotificationParticipant. # + # It's initialized in this way : + # + # p = MailParticipant.new( + # :smtp_server => "mailhost.ourcompany.co.jp", + # :from_address => "bpms@our.ourcompany.co.jp", + # :template => "Dear ${f:name}, you've got mail") + # + # or + # + # p = MailParticipant.new( + # :smtp_server => "mailhost.ourcompany.co.jp", + # :smtp_port => 25, + # :from_address => "bpms@our.ourcompany.co.jp" + # ) do |workitem| + # s = "" + # s << "Dear #{workitem.name},\n" + # s << "\n" + # s << "it's #{Time.new.to_s} and you've got mail" + # s + # end + # + # When passing the mail template as a block, you have + # four possible arities : + # { |workitem| ... } + # { |participant_expression, workitem| ... } + # { |participant_expression, participant_instance, workitem| ... } + # { ... } + # + # The smtp server and host default to "127.0.0.1" / 25. + # + class MailParticipant + include LocalParticipant + + def initialize (params, &block) + + @smtp_server = params[:smtp_server] + @smtp_port = params[:smtp_port] + @from_address = params[:from_address] + @template = params[:template] + + @block_template = block + + # some defaults + + @smtp_server = "127.0.0.1" unless @smtp_server + @smtp_port = 25 unless @smtp_port + end + + # + # The method called each time a workitem reaches this participant + # + def consume (workitem) + + fe = get_flow_expression(workitem) + to_address = workitem.email_target + + # + # 1. Expand variables + + msg = if @block_template + #@block_template.call(fe, self, workitem) + call_block @block_template, workitem + elsif @template + template = if @template.kind_of? File + @template.readlines + else + @template.to_s + end + OpenWFE::dosub(template, fe, workitem) + else + "(no template given)" + end + + puts "msg >>>\n#{msg}<<<" + + # + # 2. Send message + + Net::SMTP.start(@smtp_server, @smtp_port) do |smtp| + smtp.send_message(msg, @from_address, to_address) + end + + # + # 3. Reply to engine + + reply_to_engine(workitem) + end + end + + # + # This participant is used to send an email notification. + # + # It's perhaps better to use MailParticipant which is simpler to + # initialiaze. This class is anyway an extension of MailParticipant. + # # @engine.register_participant( # 'eno', # EmailNotificationParticipant.new( # "googlemail.l.google.com", # 25, @@ -111,64 +206,25 @@ # end # # Note that the template integrates the subject and requires then a double # newline before the message body. # - class EmailNotificationParticipant - include LocalParticipant + class EmailNotificationParticipant < MailParticipant # # Create a new email notification participant. Requires # a mail server, port, a from address, and a mail message template # def initialize ( smtp_server, smtp_port, from_address, template=nil, &block) - @smtp_server = smtp_server - @smtp_port = smtp_port - @from_address = from_address - @template = template + params = {} + params[:smtp_server] = smtp_server + params[:smtp_port] = smtp_port + params[:from_address] = from_address - @block_template = block - end + params[:template] = template if template - # - # The method called each time a workitem reaches this participant - # - def consume (workitem) - - fe = get_flow_expression(workitem) - to_address = workitem.email_target - - # - # 1. Expand variables - - msg = if @block_template - @block_template.call(fe, self, workitem) - elsif @template - template = if @template.kind_of? File - @template.readlines - else - @template.to_s - end - OpenWFE::dosub(template, fe, workitem) - else - "(no template given)" - end - - puts "msg >>>\n#{msg}<<<" - - # - # 2. Send message - - Net::SMTP.start(@smtp_server, @smtp_port) do |smtp| - smtp.send_message(msg, @from_address, to_address) - end - - # - # 3. Reply to engine - - reply_to_engine(workitem) + super(params, &block) end - end end