trabajo-afectivo/lib/notification_factory.rb

81 lines
2.2 KiB
Ruby
Raw Normal View History

module NotificationFactory
def self.build(data)
2013-01-03 10:47:39 +00:00
data[:string].gsub!( / \#\{ \s* ( .+? ) \s* \} /x ) { |placeholder|
# store possible callback to work with
# and check if it's valid for execution
original_string = $&
callback = $1
callback_valid = nil
2012-10-02 05:46:08 +00:00
# use quoted text
callback.gsub!( /\A ( \w+ ) \.body \z/x ) { |item|
callback_valid = true
item = item + '.word_wrap( :line_width => 82 ).message_quote.chomp'
}
# use config params
callback.gsub!( /\A config\.( [\w\.]+ ) \z/x ) { |item|
callback_valid = true
name = $1
item = "Setting.get('#{name}')"
}
# use object params
callback.gsub!( /\A ( [\w]+ )( \.[\w\.]+ ) \z/x ) { |item|
object_name = $1
object_method = $2
next if !data[:objects][object_name.to_sym]
callback_valid = true
item = "data[:objects]['#{object_name}'.to_sym]#{object_method}"
}
if callback_valid
# replace value
begin
placeholder = eval callback
rescue Exception => e
Rails.logger.error "Evaluation error caused by callback '#{callback}'"
Rails.logger.error e.inspect
end
else
placeholder = original_string
2013-01-03 10:47:39 +00:00
end
}
# translate
data[:string].gsub!( /i18n\((.+?)\)/ ) { |placeholder|
string = $1
locale = data[:locale] || 'en'
placeholder = Translation.translate( locale, string )
}
return data[:string]
end
2012-10-02 05:46:08 +00:00
def self.send(data)
sender = Setting.get('notification_sender')
<<<<<<< HEAD
2013-01-03 10:47:39 +00:00
Rails.logger.info "NOTICE: SEND NOTIFICATION TO: #{data[:recipient][:email]}"
2014-10-22 21:00:11 +00:00
Channel::EmailSend.new.send(
=======
imap = Channel::IMAP.new
2013-01-03 10:47:39 +00:00
Rails.logger.info "NOTICE: SEND NOTIFICATION TO: #{data[:recipient][:email]}"
message = imap.send(
>>>>>>> 5e22499e44225c831661087b68de154deafef5c6
{
# :in_reply_to => self.in_reply_to,
:from => sender,
:to => data[:recipient][:email],
:subject => data[:subject],
:body => data[:body],
},
true
)
end
end