sipgate: DRY up the Controller

This commit is contained in:
Julian Kornberger 2016-11-29 20:57:03 +01:00
parent e0b70cb24f
commit d0e3810a20

View file

@ -4,14 +4,11 @@ require 'builder'
class Integration::SipgateController < ApplicationController class Integration::SipgateController < ApplicationController
before_filter :check_configured
# notify about inbound call / block inbound call # notify about inbound call / block inbound call
def in def in
http_log_config facility: 'sipgate.io'
return if !configured?
if params['event'] == 'newCall' if params['event'] == 'newCall'
config = Setting.get('sipgate_config')
config_inbound = config[:inbound] || {} config_inbound = config[:inbound] || {}
block_caller_ids = config_inbound[:block_caller_ids] || [] block_caller_ids = config_inbound[:block_caller_ids] || []
@ -46,10 +43,6 @@ class Integration::SipgateController < ApplicationController
# set caller id of outbound call # set caller id of outbound call
def out def out
http_log_config facility: 'sipgate.io'
return if !configured?
config = Setting.get('sipgate_config')
config_outbound = config[:outbound][:routing_table] config_outbound = config[:outbound][:routing_table]
default_caller_id = config[:outbound][:default_caller_id] default_caller_id = config[:outbound][:default_caller_id]
@ -89,25 +82,29 @@ class Integration::SipgateController < ApplicationController
private private
def configured? def check_configured
http_log_config facility: 'sipgate.io'
if !Setting.get('sipgate_integration') if !Setting.get('sipgate_integration')
xml_error('Feature is disable, please contact your admin to enable it!') xml_error('Feature is disable, please contact your admin to enable it!')
return false return
end end
config = Setting.get('sipgate_config')
if !config || !config[:inbound] || !config[:outbound] if !config || !config[:inbound] || !config[:outbound]
xml_error('Feature not configured, please contact your admin!') xml_error('Feature not configured, please contact your admin!')
return false return
end end
true end
def config
@config ||= Setting.get('sipgate_config')
end end
def update_log(params) def update_log(params)
user = params['user'] user = params['user']
if params['user'] && params['user'].class == Array if Array === user
user = params['user'].join(', ') user = user.join(', ')
end end
from_comment = nil from_comment = nil
to_comment = nil to_comment = nil
preferences = nil preferences = nil
@ -119,12 +116,10 @@ class Integration::SipgateController < ApplicationController
to_comment, preferences = update_log_item('to') to_comment, preferences = update_log_item('to')
end end
comment = nil comment = params['cause']
if params['cause']
comment = params['cause']
end
if params['event'] == 'newCall' case params['event']
when 'newCall':
Cti::Log.create( Cti::Log.create(
direction: params['direction'], direction: params['direction'],
from: params['from'], from: params['from'],
@ -136,7 +131,7 @@ class Integration::SipgateController < ApplicationController
state: params['event'], state: params['event'],
preferences: preferences, preferences: preferences,
) )
elsif params['event'] == 'answer' when 'answer':
log = Cti::Log.find_by(call_id: params['callId']) log = Cti::Log.find_by(call_id: params['callId'])
raise "No such call_id #{params['callId']}" if !log raise "No such call_id #{params['callId']}" if !log
log.state = 'answer' log.state = 'answer'
@ -146,7 +141,7 @@ class Integration::SipgateController < ApplicationController
end end
log.comment = comment log.comment = comment
log.save log.save
elsif params['event'] == 'hangup' when 'hangup':
log = Cti::Log.find_by(call_id: params['callId']) log = Cti::Log.find_by(call_id: params['callId'])
raise "No such call_id #{params['callId']}" if !log raise "No such call_id #{params['callId']}" if !log
if params['direction'] == 'in' && log.state == 'newCall' if params['direction'] == 'in' && log.state == 'newCall'