Module: PolylinkApi::Helper

Defined in:
lib/polylink_api/helper.rb

Constant Summary collapse

METHODS =
[ :create_v2_c_did, 
            :delete_v2_c_did, 
            :get_v2_cid,
            :call_back_v2_c_id, 
            :get_v2_c_cdr,
            :upload_did_voice,
            :bind_phone_by_vendor_did,
            :delete_phone_by_vendor_did,
            :deleteivr_by_vendor_did,
            :bindivr_by_vendor_did,
            :get_vendor_did_phone_list,
            :get_vendor_did_ivr_did_list,
            :getivr_did_list
]
METHODS_RESPONSE_KEY =
{ get_v2_cid: "V2CID", get_v2_c_cdr: "CDR", get_vendor_did_phone_list: "List", getivr_did_list: "List", get_vendor_did_ivr_did_list: "List" }
STATUS_CODES =
[ "500", "501", "502", "510", "511", "516", "517", "518", "519", "600" ]

Class Method Summary collapse

Class Method Details

.bind_did_default_did(did, vendor_did) ⇒ Object



204
205
206
# 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) ⇒ Object



179
180
181
# 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 = {}) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object

根据V2C ID回拨



104
105
106
# 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

.clientObject



27
28
29
30
31
32
# 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) ⇒ Object

创建V2C DID绑定



94
95
96
# 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) ⇒ Object

解除V2C DID绑定



99
100
101
# 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) ⇒ Object

下载录音文件



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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) ⇒ Object

V2C ID通话记录查询



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object



130
131
132
133
134
135
136
137
138
# 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_listObject



212
213
214
215
# 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_listObject



187
188
189
190
# 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) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# 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) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# 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_didsObject



229
230
231
232
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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 InvalidDataError, "开始时间不能大于结束时间"
      when "511" then raise InvalidDataError, "查询类型和开始结束时间不匹配"
      when "516" then raise InvalidDataError, "V2CID已存在"
      when "517" then raise InvalidDataError, "DID不存在"
      when "518" then raise InvalidDataError, "V2CID不存在"
      when "519" then raise InvalidDataError, "虚拟总机DID不存在"
      when "600" then raise InvalidResponseError, "远程服务器发生未知错误"
    end
    return get_response_data(method, result)
  else
    raise InvalidResponseError, "无法解析服务器响应"
  end
end

.get_response_code(response_body_out) ⇒ Object



71
72
73
74
75
# 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) ⇒ Object



83
84
85
86
87
88
89
90
91
# 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) ⇒ Object

获取V2C ID



109
110
111
# 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) ⇒ Object



243
244
245
# 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) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# 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) ⇒ Object



234
235
236
237
238
239
240
241
# 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) ⇒ Object



208
209
210
# 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) ⇒ Object



183
184
185
# 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