polylink_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed8bc5490bb5d26c7bc0377cdc53262ce1b921ca
4
+ data.tar.gz: d7e88cc8075d3eced923c9d00675ef7117d5bb73
5
+ SHA512:
6
+ metadata.gz: aee42ed958adcea2f4b626353ecee9027d39c45444feb9bb2d4c81bc7ec6586c6a79da070bec13d9e4082cf8b6123a9fcf33805091bfe96cc8b7f3582b4e36ea
7
+ data.tar.gz: 3ea46a728c97c37458a80b6f40aecf5545ba73bf62d50943ba9adc0c6846398d7762a7af5ca0ee9472576f78dec647d88faa5db70c0c7c3fc4bcc6b200cd28ff
data/.gitignore ADDED
@@ -0,0 +1,54 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ /.config
24
+ /coverage/
25
+ /InstalledFiles
26
+ /pkg/
27
+ /spec/reports/
28
+ /test/tmp/
29
+ /test/version_tmp/
30
+ /tmp/
31
+
32
+ ## Specific to RubyMotion:
33
+ .dat*
34
+ .repl_history
35
+ build/
36
+
37
+ ## Documentation cache and generated files:
38
+ /.yardoc/
39
+ /_yardoc/
40
+ /doc/
41
+ /rdoc/
42
+
43
+ ## Environment normalisation:
44
+ /.bundle/
45
+ /lib/bundler/man/
46
+
47
+ # for a library or gem, you might want to ignore these files since the code is
48
+ # intended to run in multiple environments; otherwise, check them in:
49
+ # Gemfile.lock
50
+ # .ruby-version
51
+ # .ruby-gemset
52
+
53
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
54
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ addons:
5
+ code_climate:
6
+ repo_token: bc5e9fca001fc6e1f32959b65d0e01af3670a4352a7449f533a2ce9f6aa1ae7d
7
+ services:
8
+ - mongodb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in polylink_api.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :rspec, cmd: 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 刘泽磊
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 liuzelei
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ 聚领科技接口
2
+ ============
3
+
4
+ [![Travis](https://travis-ci.org/liuzelei/polylink_api.svg?branch=master)](https://travis-ci.org/liuzelei/polylink_api)
5
+ [![Code Climate](https://codeclimate.com/github/liuzelei/polylink_api.png)](https://codeclimate.com/github/liuzelei/polylink_api)
6
+ [![Test Coverage](https://codeclimate.com/github/liuzelei/polylink_api/coverage.png)](https://codeclimate.com/github/liuzelei/polylink_api)
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,27 @@
1
+ module PolylinkApi
2
+ class Configuration
3
+ def wsdl
4
+ @wsdl ||= "path/to/wsdl"
5
+ end
6
+
7
+ def wsdl=(wsdl)
8
+ @wsdl = wsdl
9
+ end
10
+
11
+ def download_server
12
+ @download_server ||= "path/to/downlad"
13
+ end
14
+
15
+ def download_server=(download_server)
16
+ @download_server = download_server
17
+ end
18
+
19
+ def cdrs_path
20
+ @cdrs_path ||= "tmp/polylink/cdrs"
21
+ end
22
+
23
+ def cdrs_path=(cdrs_path)
24
+ @cdrs_path = cdrs_path
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ require 'rexml/document'
2
+ require 'net/http'
3
+ require 'fileutils'
4
+
5
+ module PolylinkApi
6
+ class Did
7
+ attr_reader :number, :announcement_file
8
+
9
+ def initialize(number)
10
+ @number = number
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,219 @@
1
+ require 'savon'
2
+ require 'rexml/document'
3
+ require 'net/http'
4
+ require 'fileutils'
5
+
6
+ module PolylinkApi
7
+ module Helper
8
+
9
+ METHODS = [ :create_v2_c_did,
10
+ :delete_v2_c_did,
11
+ :get_v2_cid,
12
+ :call_back_v2_c_id,
13
+ :get_v2_c_cdr,
14
+ :get_cdr,
15
+ :upload_did_voice,
16
+ :bind_phone_by_vendor_did,
17
+ :delete_phone_by_vendor_did,
18
+ :deleteivr_by_vendor_did,
19
+ :bindivr_by_vendor_did,
20
+ :get_vendor_did_phone_list,
21
+ :get_vendor_did_ivr_did_list,
22
+ :getivr_did_list
23
+ ]
24
+
25
+ METHODS_RESPONSE_KEY = { get_v2_cid: "V2CID", get_v2_c_cdr: "CDR", get_vendor_did_phone_list: "List" }
26
+ STATUS_CODES = [ "500", "501", "502", "510", "511", "516", "517", "518", "519", "600" ]
27
+
28
+ def self.client
29
+ @client ||= Savon.client(wsdl: PolylinkApi.config.wsdl,
30
+ log: true,
31
+ log_level: :debug,
32
+ pretty_print_xml: true)
33
+ end
34
+
35
+ def self.call(method, message = {})
36
+ raise InvalidMethodError, "方法#{method}无法找到" unless METHODS.include? method
37
+ begin
38
+ response = client.call(method, message: message)
39
+ rescue Savon::NoMethodError => e
40
+ raise InvalidMethodError, e.message
41
+ rescue Savon::InvalidResponseError => e
42
+ raise InvalidResponseError, e.message
43
+ ensure
44
+ return get_response(method, response.body)
45
+ end
46
+
47
+ # log = PolylinkApi::Models::Log.new
48
+ # PolylinkApi.logger.debug response.body
49
+ # log.method = method
50
+ # log.params = message
51
+ # log.response = response.hash
52
+ # log.save
53
+ end
54
+
55
+ def self.get_response(method, response_body){}
56
+ response_sym = "#{method}_response".to_sym
57
+ raise InvalidResponseError, "没有返回正确的数据" if response_body.nil? || response_body[response_sym].nil?
58
+ result = response_body[response_sym][:out]
59
+ code = get_response_code(result)
60
+
61
+ if STATUS_CODES.include? code
62
+ case code
63
+ when "501" then raise AuthenticationError, "无法登陆远程服务器"
64
+ when "502" then raise InvalidParameterError, "参数格式错误"
65
+ when "510" then raise InvalidDataError, "开始时间不能大于结束时间"
66
+ when "511" then raise InvalidDataError, "查询类型和开始结束时间不匹配"
67
+ when "516" then raise InvalidDataError, "V2CID已存在"
68
+ when "517" then raise InvalidDataError, "DID不存在"
69
+ when "518" then raise InvalidDataError, "V2CID不存在"
70
+ when "519" then raise InvalidDataError, "DID不存在"
71
+ when "600" then raise InvalidResponseError, "远程服务器发生未知错误"
72
+ end
73
+ return get_response_data(method, result)
74
+ else
75
+ raise InvalidResponseError, "无法解析服务器响应"
76
+ end
77
+ end
78
+
79
+ def self.get_response_code(response_body_out)
80
+ code = response_body_out
81
+ code = response_body_out[/<Result>(\d+)<\/Result>/, 1] if response_include_data?(response_body_out)
82
+ return code
83
+ end
84
+
85
+ def self.response_include_data?(response_body_out)
86
+ return false if STATUS_CODES.include? response_body_out
87
+ return true if STATUS_CODES.include?(response_body_out[/<Result>(\d+)<\/Result>/, 1])
88
+ return false
89
+ end
90
+
91
+ def self.get_response_data(method, response_body_out)
92
+ if response_include_data? response_body_out
93
+ key = METHODS_RESPONSE_KEY[method]
94
+ patten = Regexp.new("<#{key}>(.+)<\/#{key}>")
95
+ return response_body_out[patten, 1]
96
+ else
97
+ return nil
98
+ end
99
+ end
100
+
101
+ def self.create_v2c_did(v2c_id, vendor_phone, customer_phone, did)
102
+ call(:create_v2_c_did, v2c_id: v2c_id, vendor_phone: vendor_phone, customer_phone: customer_phone, did: did)
103
+ end
104
+
105
+ def self.delete_v2c_did(v2c_id)
106
+ call(:delete_v2_c_did, v2c_id: v2c_id)
107
+ end
108
+
109
+ def self.callback_v2c_id(v2c_id)
110
+ call(:call_back_v2_c_id, v2c_id: v2c_id)
111
+ end
112
+
113
+ def self.get_v2c_id(vendor_phone, customer_phone, did)
114
+ call(:get_v2_cid, vendor_phone: vendor_phone, customer_phone: customer_phone, did: did)
115
+ end
116
+
117
+ def self.get_records(did, type, begintime, endtime, vendor_phone = nil, customer_phone = nil, page = 1, page_count = 10)
118
+ begintime = begintime.strftime("%Y-%m-%d %H:%M:%S") unless begintime.nil?
119
+ endtime = endtime.strftime("%Y-%m-%d %H:%M:%S") unless endtime.nil?
120
+ response = call(:get_v2_c_cdr,
121
+ did: did,
122
+ type: type,
123
+ begintime: begintime,
124
+ endtime: endtime,
125
+ vendor_phone: vendor_phone,
126
+ customer_phone: customer_phone,
127
+ page: page,
128
+ page_count: page_count
129
+ )
130
+ get_cdrs_from_string response
131
+ end
132
+
133
+ def self.get_cdrs_from_string(string)
134
+ cdrs = []
135
+ unless string.nil?
136
+ string.split(';').each do |cdr|
137
+ cdrs << get_cdr_from_string(cdr)
138
+ end
139
+ end
140
+ cdrs
141
+ end
142
+
143
+ def self.get_record_file(file)
144
+ local_file = File.join(PolylinkApi.config.cdrs_path, file)
145
+ remote_file = PolylinkApi.config.download_server + file
146
+ unless File.exists?(local_file)
147
+ FileUtils.mkpath(File.dirname(local_file)) unless Dir.exists?(File.dirname(local_file))
148
+ uri = URI(remote_file)
149
+ Net::HTTP.start(uri.host, uri.port) do |http|
150
+ request = Net::HTTP::Get.new uri
151
+ http.request(request) do |response|
152
+ open(local_file, 'w') do |io|
153
+ response.read_body do |chunk|
154
+ io.write chunk
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
160
+
161
+ local_file
162
+ end
163
+
164
+ def self.get_cdr_from_string(string)
165
+ cdr = {}
166
+ attributes = string.split(',')
167
+ cdr[:did] = attributes[0]
168
+ cdr[:v2c_id] = attributes[1]
169
+ cdr[:device_name] = attributes[2]
170
+ cdr[:calling_number] = attributes[3]
171
+ cdr[:called_number] = attributes[4]
172
+ cdr[:starttime] = attributes[5]
173
+ cdr[:duration] = attributes[6]
174
+ cdr[:endtime] = attributes[7]
175
+ cdr[:recordpath] = attributes[8]
176
+ cdr[:call_status] = attributes[9]
177
+ return cdr
178
+ end
179
+
180
+ def self.bind_did_default_phone(did, vendor_phone)
181
+ call(:bind_phone_by_vendor_did, vendor_did: did, phone: vendor_phone)
182
+ end
183
+
184
+ def self.unbind_did_default_phone(did)
185
+ call(:delete_phone_by_vendor_did, vendor_did: did)
186
+ end
187
+
188
+ def self.get_did_default_phones_list
189
+ response = call(:get_vendor_did_phone_list)
190
+ get_did_phones_from_string response
191
+ end
192
+
193
+ def self.get_did_phones_from_string(string)
194
+ did_phones_list = []
195
+ string.split(';').each do |item|
196
+ did_phone = {}
197
+ attributes = item.split(',')
198
+ did_phone[:did] = attributes[0]
199
+ did_phone[:phone] = attributes[1]
200
+ did_phones_list << did_phone
201
+ end
202
+ return did_phones_list
203
+ end
204
+
205
+ def self.set_did_announcement(did, file)
206
+ if File.exists? file
207
+ data = File.read file
208
+ call(:upload_did_voice, vendor_did: did, filestr: Base64.encode64(data))
209
+ else
210
+ raise InvalidDataError, "找不到文件#{file}"
211
+ end
212
+ end
213
+
214
+ def self.remove_did_announcement(did)
215
+ call(:upload_did_voice, vendor_did: did, filestr: "")
216
+ end
217
+
218
+ end
219
+ end
@@ -0,0 +1,17 @@
1
+ require "mongoid"
2
+
3
+ module PolylinkApi
4
+ module Models
5
+ class Log
6
+ include Mongoid::Document
7
+ field :method, type: String
8
+ field :params, type: Hash
9
+ field :response, type: Hash
10
+ field :logged_at, type: DateTime
11
+
12
+ before_create do
13
+ self.logged_at = Time.now
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module PolylinkApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require "polylink_api/version"
2
+ require "polylink_api/configuration"
3
+ require "polylink_api/helper"
4
+ require "polylink_api/did"
5
+ require "polylink_api/models/log"
6
+ require "logger"
7
+ require "mongoid"
8
+
9
+ module PolylinkApi
10
+ Error = Class.new(RuntimeError)
11
+ InvalidResponseError = Class.new(Error)
12
+ AuthenticationError = Class.new(Error)
13
+ InvalidMethodError = Class.new(Error)
14
+ InvalidParameterError = Class.new(Error)
15
+ InvalidDataError = Class.new(InvalidParameterError)
16
+ UnknownError = Class.new(Error)
17
+
18
+ def self.setup
19
+ yield config
20
+ end
21
+
22
+ def self.config
23
+ @config ||= Configuration.new
24
+ end
25
+
26
+ def self.logger
27
+ @logger ||= Logger.new(STDOUT)
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'polylink_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "polylink_api"
8
+ spec.version = PolylinkApi::VERSION
9
+ spec.authors = ["liuzelei"]
10
+ spec.email = ["liuzelei@gmail.com"]
11
+ spec.summary = '跟聚领对接的接口'
12
+ spec.description = '基本上采用webservice方式'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "guard"
25
+ spec.add_development_dependency "guard-rspec"
26
+ spec.add_development_dependency "codeclimate-test-reporter"
27
+ spec.add_dependency "savon", "~> 2.8.0"
28
+ spec.add_dependency "mongoid", "~> 4.0.0"
29
+ end
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: polylink
5
+ hosts:
6
+ - localhost:27017
Binary file