Refactored channel backend call and replaced eval with Object.const_get.
This commit is contained in:
parent
e174d7e01a
commit
7d9eed3dfb
2 changed files with 31 additions and 11 deletions
|
@ -7,13 +7,22 @@ class Channel < ApplicationModel
|
||||||
channels = Channel.where( 'active = ? AND area LIKE ?', true, '%::Inbound' )
|
channels = Channel.where( 'active = ? AND area LIKE ?', true, '%::Inbound' )
|
||||||
channels.each { |channel|
|
channels.each { |channel|
|
||||||
begin
|
begin
|
||||||
c = eval 'Channel::' + channel[:adapter].upcase + '.new' # rubocop:disable Lint/Eval
|
# we need to require each channel backend individually otherwise we get a
|
||||||
c.fetch(channel)
|
# 'warning: toplevel constant Twitter referenced by Channel::Twitter' error e.g.
|
||||||
|
# so we have to convert the channel name to the filename via Rails String.underscore
|
||||||
|
# http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
|
||||||
|
require "channel/#{channel[:adapter].underscore}"
|
||||||
|
|
||||||
|
channel_object = Object.const_get("Channel::#{channel[:adapter]}")
|
||||||
|
channel_instance = channel_object.new
|
||||||
|
|
||||||
|
channel_instance.fetch(channel)
|
||||||
|
|
||||||
|
channel_instance.disconnect
|
||||||
rescue => e
|
rescue => e
|
||||||
logger.error "can't use " + 'Channel::' + channel[:adapter].upcase
|
logger.error "Can't use Channel::#{channel[:adapter]}"
|
||||||
logger.error e.inspect
|
logger.error e.inspect
|
||||||
logger.error e.backtrace
|
logger.error e.backtrace
|
||||||
c.disconnect
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,15 +3,26 @@
|
||||||
require 'net/imap'
|
require 'net/imap'
|
||||||
|
|
||||||
module Channel::EmailSend
|
module Channel::EmailSend
|
||||||
def self.send(attr, notification = false)
|
def self.send(article, notification = false)
|
||||||
channel = Channel.find_by( area: 'Email::Outbound', active: true )
|
|
||||||
begin
|
begin
|
||||||
c = eval 'Channel::' + channel[:adapter] + '.new' # rubocop:disable Lint/Eval
|
# we need to require the channel backend individually otherwise we get a
|
||||||
c.send(attr, channel, notification)
|
# 'warning: toplevel constant Twitter referenced by Channel::Twitter' error e.g.
|
||||||
|
# so we have to convert the channel name to the filename via Rails String.underscore
|
||||||
|
# http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
|
||||||
|
require "channel/#{channel[:adapter].underscore}"
|
||||||
|
|
||||||
|
channel_object = Object.const_get("Channel::#{channel[:adapter]}")
|
||||||
|
channel_instance = channel_object.new
|
||||||
|
|
||||||
|
channel_instance.send(article, channel, notification)
|
||||||
|
|
||||||
|
channel_instance.disconnect
|
||||||
rescue => e
|
rescue => e
|
||||||
Rails.logger.error "can't use " + 'Channel::' + channel[:adapter]
|
logger.error "Can't use Channel::#{channel[:adapter]}"
|
||||||
Rails.logger.error e.inspect
|
logger.error e.inspect
|
||||||
Rails.logger.error e.backtrace
|
logger.error e.backtrace
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue