module PolylinkApi::Helper

Constants

METHODS
METHODS_RESPONSE_KEY
STATUS_CODES

Public Class Methods

bind_did_default_did(did, vendor_did) click to toggle source
# File lib/polylink_api/helper.rb, line 204
def self.bind_did_default_did(did, vendor_did)
  call(:bindivr_by_vendor_did, vendor_did: did, ivr_did: vendor_did)
end
bind_did_default_phone(did, vendor_phone) click to toggle source
# File lib/polylink_api/helper.rb, line 179
def self.bind_did_default_phone(did, vendor_phone)
  call(:bind_phone_by_vendor_did, vendor_did: did, phone: vendor_phone)
end
call(method, message = {}) click to toggle source
# File lib/polylink_api/helper.rb, line 34
def self.call(method, message = {})
  raise InvalidMethodError, "方法#{method}无法找到" unless METHODS.include? method
  begin
    response = client.call(method, message: message)
  rescue Savon::NoMethodError => e
    raise InvalidMethodError, e.message
  rescue Savon::InvalidResponseError => e
    raise InvalidResponseError, e.message
  ensure
    return get_response(method, response.body)
  end
end
callback_v2c_id(v2c_id) click to toggle source

根据V2C ID回拨

# File lib/polylink_api/helper.rb, line 104
def self.callback_v2c_id(v2c_id)
  call(:call_back_v2_c_id, v2c_id: v2c_id)
end
client() click to toggle source
# File lib/polylink_api/helper.rb, line 27
def self.client
  @client ||= Savon.client(wsdl: PolylinkApi.config.wsdl,
                            log: true,
                      log_level: :debug,
               pretty_print_xml: true)
end
create_v2c_did(v2c_id, vendor_phone, customer_phone, did) click to toggle source

创建V2C DID绑定

# File lib/polylink_api/helper.rb, line 94
def self.create_v2c_did(v2c_id, vendor_phone, customer_phone, did)
  call(:create_v2_c_did, v2c_id: v2c_id, vendor_phone: vendor_phone, customer_phone: customer_phone, did: did)
end
delete_v2c_did(v2c_id) click to toggle source

解除V2C DID绑定

# File lib/polylink_api/helper.rb, line 99
def self.delete_v2c_did(v2c_id)
  call(:delete_v2_c_did, v2c_id: v2c_id)
end
get_cdr_file(file) click to toggle source

下载录音文件

# File lib/polylink_api/helper.rb, line 157
def self.get_cdr_file(file)
  local_file = File.join(PolylinkApi.config.cdrs_path, file)
  remote_file = PolylinkApi.config.download_server + file
  unless File.exists?(local_file)
    FileUtils.mkpath(File.dirname(local_file)) unless Dir.exists?(File.dirname(local_file))
    uri = URI(remote_file)
    Net::HTTP.start(uri.host, uri.port) do |http|
      request = Net::HTTP::Get.new uri
      http.request(request) do |response|
        open(local_file, 'w') do |io|
          response.read_body do |chunk|
            io.write chunk
          end
        end
      end
    end
  end

  local_file
end
get_cdr_from_string(string) click to toggle source
# File lib/polylink_api/helper.rb, line 140
def self.get_cdr_from_string(string)
  cdr = {}
  attributes = string.split(',')
  cdr[:did]            = attributes[0]
  cdr[:v2c_id]         = attributes[1]
  cdr[:device_name]    = attributes[2]
  cdr[:calling_number] = attributes[3]
  cdr[:called_number]  = attributes[4]
  cdr[:starttime]      = attributes[5]
  cdr[:duration]       = attributes[6]
  cdr[:endtime]        = attributes[7]
  cdr[:recordpath]     = attributes[8]
  cdr[:call_status]    = attributes[9]
  return cdr
end
get_cdrs(did, type, begintime, endtime, vendor_phone = nil, customer_phone = nil, page = 1, page_count = 10) click to toggle source

V2C ID通话记录查询

# File lib/polylink_api/helper.rb, line 114
def self.get_cdrs(did, type, begintime, endtime, vendor_phone = nil, customer_phone = nil, page = 1, page_count = 10)
  begintime = begintime.strftime("%Y-%m-%d %H:%M:%S") unless begintime.nil?
  endtime = endtime.strftime("%Y-%m-%d %H:%M:%S") unless endtime.nil?
  response = call(:get_v2_c_cdr, 
                did:             did, 
                type:            type, 
                begintime:       begintime, 
                endtime:         endtime, 
                vendor_phone:    vendor_phone, 
                customer_phone:  customer_phone,
                page:            page,
                page_count:      page_count
              )
  get_cdrs_from_string response
end
get_cdrs_from_string(string) click to toggle source
# File lib/polylink_api/helper.rb, line 130
def self.get_cdrs_from_string(string)
  cdrs = []
  unless string.nil?
    string.split(';').each do |cdr|
      cdrs << get_cdr_from_string(cdr)
    end
  end
  cdrs
end
get_did_default_dids_list() click to toggle source
# File lib/polylink_api/helper.rb, line 212
def self.get_did_default_dids_list
  response = call(:get_vendor_did_ivr_did_list)
  get_did_dids_from_string response
end
get_did_default_phones_list() click to toggle source
# File lib/polylink_api/helper.rb, line 187
def self.get_did_default_phones_list
  response = call(:get_vendor_did_phone_list)
  get_did_phones_from_string response
end
get_did_dids_from_string(string) click to toggle source
# File lib/polylink_api/helper.rb, line 217
def self.get_did_dids_from_string(string)
  did_dids_list = []
  string.split(';').each do |item|
    did_did = {}
    attributes = item.split(',')
    did_did[:did] = attributes[0]
    did_did[:default_did] = attributes[1]
    did_dids_list << did_did
  end
  return did_dids_list
end
get_did_phones_from_string(string) click to toggle source
# File lib/polylink_api/helper.rb, line 192
def self.get_did_phones_from_string(string)
  did_phones_list = []
  string.split(';').each do |item|
    did_phone = {}
    attributes = item.split(',')
    did_phone[:did]   = attributes[0]
    did_phone[:default_phone] = attributes[1]
    did_phones_list << did_phone
  end
  return did_phones_list
end
get_dids() click to toggle source
# File lib/polylink_api/helper.rb, line 229
def self.get_dids
  response = call(:getivr_did_list)
  return response.split(';')
end
get_response(method, response_body) click to toggle source
# File lib/polylink_api/helper.rb, line 47
def self.get_response(method, response_body){}
  response_sym = "#{method}_response".to_sym
  raise InvalidResponseError, "没有返回正确的数据" if response_body.nil? || response_body[response_sym].nil?
  result = response_body[response_sym][:out]
  code = get_response_code(result)

  if STATUS_CODES.include? code
    case code
      when "501" then raise AuthenticationError, "无法登陆远程服务器"
      when "502" then raise InvalidParameterError, "参数格式错误"
      when "510" then raise InvalidParameterError, "开始时间不能大于结束时间"
      when "511" then raise InvalidParameterError, "查询类型和开始结束时间不匹配"
      when "516" then raise AlreadyExistedError, "V2CID已存在"
      when "517" then raise NotExistError, "DID不存在"
      when "518" then raise NotExistError, "V2CID不存在"
      when "519" then raise NotExistError, "虚拟总机DID不存在"
      when "600" then raise InvalidResponseError, "远程服务器发生未知错误"
    end
    return get_response_data(method, result)
  else
    raise InvalidResponseError, "无法解析服务器响应"
  end
end
get_response_code(response_body_out) click to toggle source
# File lib/polylink_api/helper.rb, line 71
def self.get_response_code(response_body_out)
  code = response_body_out
  code = response_body_out[/<Result>(\d+)<\/Result>/, 1] if response_include_data?(response_body_out)
  return code
end
get_response_data(method, response_body_out) click to toggle source
# File lib/polylink_api/helper.rb, line 83
def self.get_response_data(method, response_body_out)
  if response_include_data? response_body_out
    key = METHODS_RESPONSE_KEY[method]
    patten = Regexp.new("<#{key}>(.+)<\/#{key}>")
    return response_body_out[patten, 1]
  else
    return nil
  end
end
get_v2c_id(vendor_phone, customer_phone, did) click to toggle source

获取V2C ID

# File lib/polylink_api/helper.rb, line 109
def self.get_v2c_id(vendor_phone, customer_phone, did)
  call(:get_v2_cid, vendor_phone: vendor_phone, customer_phone: customer_phone, did: did)
end
remove_did_announcement(did) click to toggle source
# File lib/polylink_api/helper.rb, line 243
def self.remove_did_announcement(did)
  call(:upload_did_voice, vendor_did: did, filestr: "")
end
response_include_data?(response_body_out) click to toggle source
# File lib/polylink_api/helper.rb, line 77
def self.response_include_data?(response_body_out)
  return false if STATUS_CODES.include? response_body_out
  return true if STATUS_CODES.include?(response_body_out[/<Result>(\d+)<\/Result>/, 1])
  return false
end
set_did_announcement(did, file) click to toggle source
# File lib/polylink_api/helper.rb, line 234
def self.set_did_announcement(did, file)
  if File.exists? file
    data = File.read file
    call(:upload_did_voice, vendor_did: did, filestr: Base64.encode64(data))
  else
    raise InvalidDataError, "找不到文件#{file}"
  end
end
unbind_did_default_did(did) click to toggle source
# File lib/polylink_api/helper.rb, line 208
def self.unbind_did_default_did(did)
  call(:deleteivr_by_vendor_did, vendor_did: did)
end
unbind_did_default_phone(did) click to toggle source
# File lib/polylink_api/helper.rb, line 183
def self.unbind_did_default_phone(did)
  call(:delete_phone_by_vendor_did, vendor_did: did)
end