notification_factory.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. module NotificationFactory
  2. def self.build(data)
  3. data[:string].gsub!( /\#\{(.+?)\}/ ) { |s|
  4. # use quoted text
  5. callback = $1
  6. callback.gsub!( /\.body$/ ) { |item|
  7. item = item + '.word_wrap( :line_width => 82 ).message_quote.chomp'
  8. }
  9. # use config params
  10. callback.gsub!( /^config\.(.+?)$/ ) { |item|
  11. name = $1
  12. item = "Setting.get('#{$1}')"
  13. }
  14. # use object params
  15. callback.gsub!( /^(.+?)(\..+?)$/ ) { |item|
  16. object_name = $1
  17. object_method = $2
  18. replace = nil
  19. if data[:objects][object_name.to_sym]
  20. replace = "data[:objects]['#{object_name}'.to_sym]#{object_method}"
  21. else
  22. replace = $1 + $2
  23. end
  24. item = replace
  25. }
  26. # replace value
  27. begin
  28. s = eval callback
  29. rescue Exception => e
  30. Rails.logger.error "can't eval #{callback}"
  31. Rails.logger.error e.inspect
  32. end
  33. }
  34. # translate
  35. data[:string].gsub!( /i18n\((.+?)\)/ ) { |s|
  36. string = $1
  37. locale = data[:locale] || 'en'
  38. s = Translation.translate( locale, string )
  39. }
  40. return data[:string]
  41. end
  42. def self.send(data)
  43. sender = Setting.get('notification_sender')
  44. a = Channel::IMAP.new
  45. Rails.logger.info "NOTICE: SEND NOTIFICATION TO: #{data[:recipient][:email]}"
  46. message = a.send(
  47. {
  48. # :in_reply_to => self.in_reply_to,
  49. :from => sender,
  50. :to => data[:recipient][:email],
  51. :subject => data[:subject],
  52. :body => data[:body],
  53. },
  54. true
  55. )
  56. end
  57. end