trabajo-afectivo/app/models/channel.rb

110 lines
3 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Channel < ApplicationModel
store :options
2015-08-28 00:53:14 +00:00
=begin
fetch all accounts
Channel.fetch
=end
2012-04-10 14:06:46 +00:00
def self.fetch
2015-08-28 00:53:14 +00:00
channels = Channel.where('active = ? AND area LIKE ?', true, '%::Account')
channels.each(&:fetch)
2012-04-10 14:06:46 +00:00
end
2015-08-28 00:53:14 +00:00
=begin
fetch one account
channel = Channel.where(area: 'Email::Account').first
channel.fetch
=end
def fetch
adapter = options[:adapter]
adapter_options = options
if options[:options]
adapter_options = options[:options]
elsif options[:inbound] && options[:inbound][:adapter]
adapter = options[:inbound][:adapter]
adapter_options = options[:inbound][:options]
end
begin
# we need to require each channel backend individually otherwise we get a
# 'warning: toplevel constant Twitter referenced by Channel::Driver::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/driver/#{adapter.to_filename}"
driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
driver_instance = driver_class.new
driver_instance.fetch(adapter_options, self)
self.status_in = 'ok'
self.last_log_in = ''
save
rescue => e
error = "Can't use Channel::Driver::#{adapter.to_classname}: #{e.inspect}"
logger.error error
logger.error e.backtrace
self.status_in = 'error'
self.last_log_in = error
save
end
end
=begin
send via account
channel = Channel.where(area: 'Email::Account').first
channel.deliver(mail_params, notification)
=end
def deliver(mail_params, notification = false)
adapter = options[:adapter]
adapter_options = options
if options[:options]
adapter_options = options[:options]
elsif options[:outbound] && options[:outbound][:adapter]
adapter = options[:outbound][:adapter]
adapter_options = options[:outbound][:options]
end
begin
# we need to require each channel backend individually otherwise we get a
# 'warning: toplevel constant Twitter referenced by Channel::Driver::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/driver/#{adapter.to_filename}"
driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
driver_instance = driver_class.new
driver_instance.send(adapter_options, mail_params, notification)
self.status_out = 'ok'
self.last_log_out = ''
save
rescue => e
error = "Can't use Channel::Driver::#{adapter.to_classname}: #{e.inspect}"
logger.error error
logger.error e.backtrace
self.status_out = 'error'
self.last_log_out = error
save
end
end
end