semantria_sdk 3.0.70 → 3.8.79
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 +4 -4
- data/lib/semantria.rb +5 -1
- data/lib/semantria/authrequest.rb +137 -135
- data/lib/semantria/jsonserializer.rb +13 -11
- data/lib/semantria/session.rb +455 -432
- data/lib/semantria/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f94ace4447c0a66bf0ca837d811d8b3f561b75e6
|
4
|
+
data.tar.gz: f8dde0dde185ba41e357bad8ad6668cbd8faa379
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ae88c6812d0df02ceb4eaca45882246b17de7cda129de6ce4bb55d18073c6a0db6b68471e559acf1cfc84286d5660508ad44b2795ed36630294bdade21a397d
|
7
|
+
data.tar.gz: f6591933889697d66ddc942d2dc943caf5f1c93d38e66f8c103495ab479eecba9de1c5b25c369eca303800003c41be83ca27cf2ad31711e3f54516f8f8af10f4
|
data/lib/semantria.rb
CHANGED
@@ -8,158 +8,160 @@ require 'digest/sha1'
|
|
8
8
|
require 'net/http'
|
9
9
|
require 'net/https'
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
def authWebRequest(method, url, post_data = nil)
|
30
|
-
nonce = generateNonce()
|
31
|
-
timestamp = generateTimestamp()
|
32
|
-
query = generate_query(method, url, timestamp, nonce)
|
33
|
-
auth_header = generate_auth_header(query, timestamp, nonce)
|
34
|
-
headers = {'Authorization' => auth_header}
|
35
|
-
|
36
|
-
headers['Content-type'] = 'application/x-www-form-urlencoded' if method == 'POST'
|
37
|
-
headers['x-api-version'] = '3'
|
38
|
-
headers['x-app-name'] = @application_name
|
39
|
-
|
40
|
-
headers['Accept-Encoding'] = 'gzip' if @use_compression
|
41
|
-
|
42
|
-
uri = URI.parse(query)
|
43
|
-
conn = Net::HTTP.new(uri.host, 443)
|
44
|
-
conn.use_ssl = true
|
45
|
-
conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
46
|
-
|
47
|
-
path = uri.request_uri #'%s?%s' % [qpath, qquery]
|
48
|
-
request = get_request(method, path, headers, post_data)
|
49
|
-
response = conn.request(request)
|
50
|
-
|
51
|
-
data = nil
|
52
|
-
if response.header['Content-Encoding'].eql? 'gzip'
|
53
|
-
sio = StringIO.new( response.body )
|
54
|
-
gz = Zlib::GzipReader.new( sio )
|
55
|
-
data = gz.read()
|
56
|
-
else
|
57
|
-
data = response.body
|
11
|
+
module Semantria
|
12
|
+
OAUTH_VERSION = '1.0'
|
13
|
+
OAUTH_KEY_PREFIX = "oauth_"
|
14
|
+
OAUTH_CONSUMER_KEY = "oauth_consumer_key"
|
15
|
+
OAUTH_VERSION_KEY = "oauth_version"
|
16
|
+
OAUTH_SIGNATURE_METHOD_KEY = "oauth_signature_method"
|
17
|
+
OAUTH_SIGNATURE_KEY = "oauth_signature"
|
18
|
+
OAUTH_TIMESTAMP_KEY = "oauth_timestamp"
|
19
|
+
OAUTH_NONCE_KEY = "oauth_nonce"
|
20
|
+
|
21
|
+
class AuthRequest
|
22
|
+
# Create a new instance
|
23
|
+
def initialize(consumer_key, consumer_secret, application_name, use_compression = false)
|
24
|
+
@consumer_key = consumer_key
|
25
|
+
@consumer_secret = consumer_secret
|
26
|
+
@application_name = application_name
|
27
|
+
@use_compression = use_compression
|
58
28
|
end
|
59
29
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
30
|
+
def authWebRequest(method, url, post_data = nil)
|
31
|
+
nonce = generateNonce()
|
32
|
+
timestamp = generateTimestamp()
|
33
|
+
query = generate_query(method, url, timestamp, nonce)
|
34
|
+
auth_header = generate_auth_header(query, timestamp, nonce)
|
35
|
+
headers = {'Authorization' => auth_header}
|
36
|
+
|
37
|
+
headers['Content-type'] = 'application/x-www-form-urlencoded' if method == 'POST'
|
38
|
+
headers['x-api-version'] = '3.8'
|
39
|
+
headers['x-app-name'] = @application_name
|
40
|
+
|
41
|
+
headers['Accept-Encoding'] = 'gzip' if @use_compression
|
42
|
+
|
43
|
+
uri = URI.parse(query)
|
44
|
+
conn = Net::HTTP.new(uri.host, 443)
|
45
|
+
conn.use_ssl = true
|
46
|
+
conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
47
|
+
|
48
|
+
path = uri.request_uri #'%s?%s' % [qpath, qquery]
|
49
|
+
request = get_request(method, path, headers, post_data)
|
50
|
+
response = conn.request(request)
|
51
|
+
|
52
|
+
data = nil
|
53
|
+
if response.header['Content-Encoding'].eql? 'gzip'
|
54
|
+
sio = StringIO.new( response.body )
|
55
|
+
gz = Zlib::GzipReader.new( sio )
|
56
|
+
data = gz.read()
|
76
57
|
else
|
77
|
-
|
58
|
+
data = response.body
|
59
|
+
end
|
60
|
+
|
61
|
+
{status: response.code.to_i, reason: response.message, data: data}
|
78
62
|
end
|
79
63
|
|
80
|
-
|
81
|
-
|
82
|
-
|
64
|
+
private
|
65
|
+
# create the http request object for a given http_method and path
|
66
|
+
def get_request(method, path, headers, post_data = nil)
|
67
|
+
request = nil
|
68
|
+
case method
|
69
|
+
when 'POST'
|
70
|
+
request = Net::HTTP::Post.new(path, headers)
|
71
|
+
when 'PUT'
|
72
|
+
request = Net::HTTP::Put.new(path, headers)
|
73
|
+
when 'GET'
|
74
|
+
request = Net::HTTP::Get.new(path, headers)
|
75
|
+
when 'DELETE'
|
76
|
+
request = Net::HTTP::Delete.new(path, headers)
|
77
|
+
else
|
78
|
+
fail ArgumentError, "Don't know how to handle method: :#{method}"
|
79
|
+
end
|
80
|
+
|
81
|
+
unless post_data.nil?
|
82
|
+
request.body = post_data
|
83
|
+
request['Content-Length'] = request.body.length.to_s
|
84
|
+
end
|
85
|
+
|
86
|
+
request
|
83
87
|
end
|
84
88
|
|
85
|
-
|
86
|
-
|
89
|
+
def generate_query(method, url, timestamp, nonce)
|
90
|
+
uri = URI.parse(url)
|
91
|
+
np = get_normalized_parameters(timestamp, nonce)
|
87
92
|
|
88
|
-
|
89
|
-
|
90
|
-
|
93
|
+
if uri.query
|
94
|
+
uri.query = '%s&%s' % [uri.query, np]
|
95
|
+
else
|
96
|
+
uri.query = '%s' % np
|
97
|
+
end
|
91
98
|
|
92
|
-
|
93
|
-
uri.query = '%s&%s' % [uri.query, np]
|
94
|
-
else
|
95
|
-
uri.query = '%s' % np
|
99
|
+
uri.to_s
|
96
100
|
end
|
97
101
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
params.
|
102
|
+
def generate_auth_header(query, timestamp, nonce)
|
103
|
+
md5cs = get_md5_hash(@consumer_secret)
|
104
|
+
esc_query = escape(query)
|
105
|
+
hash = get_sha1(md5cs, esc_query)
|
106
|
+
hash = escape(hash)
|
107
|
+
|
108
|
+
items = Hash.new()
|
109
|
+
items['OAuth realm'] = ''
|
110
|
+
items[OAUTH_VERSION_KEY] = "%s" % OAUTH_VERSION
|
111
|
+
items[OAUTH_TIMESTAMP_KEY] = "%s" % timestamp
|
112
|
+
items[OAUTH_NONCE_KEY] = "%s" % nonce
|
113
|
+
items[OAUTH_SIGNATURE_METHOD_KEY] = "HMAC-SHA1"
|
114
|
+
items[OAUTH_CONSUMER_KEY] = "%s" % @consumer_key
|
115
|
+
items[OAUTH_SIGNATURE_KEY] = "%s" % hash
|
116
|
+
|
117
|
+
params = []
|
118
|
+
items.keys.sort.each do |key|
|
119
|
+
params.push('%s="%s"' % [key, items[key]])
|
120
|
+
end
|
121
|
+
|
122
|
+
params.join(',')
|
119
123
|
end
|
120
124
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
params.
|
125
|
+
def get_normalized_parameters(timestamp, nonce)
|
126
|
+
items = Hash.new()
|
127
|
+
items[OAUTH_VERSION_KEY] = OAUTH_VERSION
|
128
|
+
items[OAUTH_TIMESTAMP_KEY] = timestamp
|
129
|
+
items[OAUTH_NONCE_KEY] = nonce
|
130
|
+
items[OAUTH_SIGNATURE_METHOD_KEY] = "HMAC-SHA1"
|
131
|
+
items[OAUTH_CONSUMER_KEY] = @consumer_key
|
132
|
+
|
133
|
+
params = []
|
134
|
+
for key in items.keys.sort
|
135
|
+
params.push('%s=%s' % [key, items[key]])
|
136
|
+
end
|
137
|
+
|
138
|
+
np = params.join('&')
|
139
|
+
# Encode signature parameters per Oauth Core 1.0 protocol
|
140
|
+
# Spaces must be encoded with "%20" instead of "+"
|
141
|
+
return np.gsub('+', '%20').gsub('%7E', '~')
|
135
142
|
end
|
136
143
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
return np.gsub('+', '%20').gsub('%7E', '~')
|
141
|
-
end
|
142
|
-
|
143
|
-
def get_md5_hash(str)
|
144
|
-
md5hash = Digest::MD5.hexdigest(str)
|
145
|
-
end
|
144
|
+
def get_md5_hash(str)
|
145
|
+
Digest::MD5.hexdigest(str)
|
146
|
+
end
|
146
147
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
148
|
+
def get_sha1(md5cs, query)
|
149
|
+
digest = OpenSSL::Digest.new('sha1')
|
150
|
+
# our composite signing key now has the token secret after the ampersand
|
151
|
+
sha1res = OpenSSL::HMAC.digest(digest, md5cs, query)
|
152
|
+
Base64.encode64(sha1res).chomp.gsub(/\n/, '')
|
153
|
+
end
|
153
154
|
|
154
|
-
|
155
|
-
|
156
|
-
|
155
|
+
def generateTimestamp()
|
156
|
+
Time.now.to_i.to_s
|
157
|
+
end
|
157
158
|
|
158
|
-
|
159
|
-
|
160
|
-
|
159
|
+
def generateNonce(length = 20)
|
160
|
+
rand(10 ** length).to_s.rjust(length, '0')
|
161
|
+
end
|
161
162
|
|
162
|
-
|
163
|
-
|
163
|
+
def escape(s)
|
164
|
+
CGI::escape(s)
|
165
|
+
end
|
164
166
|
end
|
165
|
-
end
|
167
|
+
end
|
@@ -1,18 +1,20 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'json'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
module Semantria
|
5
|
+
class JsonSerializer
|
6
|
+
def gettype
|
7
|
+
'json'
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def serialize(obj, wrapper = nil)
|
11
|
+
str = JSON.generate(obj)
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
str.encoding.name != 'UTF-8' ? str.encode('UTF-8') : str
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
def deserialize(str, handler = nil)
|
17
|
+
JSON.parse(str)
|
18
|
+
end
|
17
19
|
end
|
18
|
-
end
|
20
|
+
end
|
data/lib/semantria/session.rb
CHANGED
@@ -1,564 +1,587 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
#$LOAD_PATH << File.dirname(__FILE__) unless $LOAD_PATH.include?(File.dirname(__FILE__))
|
3
|
-
require_relative 'version'
|
4
|
-
require_relative 'authrequest'
|
5
|
-
require_relative 'jsonserializer'
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
3
|
+
#require_relative 'version'
|
4
|
+
#require_relative 'authrequest'
|
5
|
+
#require_relative 'jsonserializer'
|
6
|
+
|
7
|
+
module Semantria
|
8
|
+
class Session
|
9
|
+
attr_accessor :host
|
10
|
+
|
11
|
+
# Create a new instance
|
12
|
+
def initialize(consumer_key, consumer_secret, application_name = nil, use_compression = false, serializer = nil)
|
13
|
+
@host = 'https://api.semantria.com'
|
14
|
+
@wrapper_name = "Ruby/#{Semantria::VERSION}"
|
15
|
+
@consumer_key = consumer_key
|
16
|
+
@consumer_secret = consumer_secret
|
17
|
+
@use_compression = use_compression
|
18
|
+
|
19
|
+
if application_name.nil?
|
20
|
+
@application_name = @wrapper_name
|
21
|
+
else
|
22
|
+
@application_name = '%s/%s' % [application_name, @wrapper_name]
|
23
|
+
end
|
24
|
+
|
25
|
+
if serializer.nil?
|
26
|
+
#set default json serializer
|
27
|
+
@serializer = JsonSerializer.new()
|
28
|
+
@format = @serializer.gettype()
|
29
|
+
else
|
30
|
+
@serializer = serializer
|
31
|
+
@format = @serializer.gettype()
|
32
|
+
end
|
31
33
|
end
|
32
|
-
end
|
33
34
|
|
34
|
-
|
35
|
-
|
35
|
+
def registerSerializer(serializer)
|
36
|
+
fail 'Serializer can\'t be null' if serializer.nil?
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
@serializer = serializer
|
39
|
+
@format = serializer.gettype()
|
40
|
+
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
def setCallbackHandler(callback)
|
43
|
+
if callback.class < CallbackHandler
|
44
|
+
@callback = callback
|
45
|
+
else
|
46
|
+
fail "Parameter is not subclass of CallbackHandler #{callback}"
|
47
|
+
end
|
46
48
|
end
|
47
|
-
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
def getStatus
|
51
|
+
url = "#{@host}/status.#{@format}"
|
52
|
+
runRequest('GET', url, 'get_status')
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
def getSupportedFeatures(language)
|
56
|
+
if language.nil?
|
57
|
+
url = "#{@host}/features.#{@format}"
|
58
|
+
else
|
59
|
+
url = "#{@host}/features.#{@format}?language=#{language}"
|
60
|
+
end
|
61
|
+
|
62
|
+
runRequest('GET', url, 'get_features')
|
63
|
+
end
|
58
64
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
65
|
+
def getSubscription
|
66
|
+
url = "#{@host}/subscription.#{@format}"
|
67
|
+
runRequest('GET', url, 'get_subscription')
|
68
|
+
end
|
63
69
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end
|
70
|
+
def getStatistics
|
71
|
+
url = "#{@host}/statistics.#{@format}"
|
72
|
+
runRequest('GET', url, 'get_statistics')
|
73
|
+
end
|
69
74
|
|
70
|
-
|
71
|
-
|
72
|
-
|
75
|
+
def getConfigurations
|
76
|
+
url = "#{@host}/configurations.#{@format}"
|
77
|
+
runRequest('GET', url, 'get_configurations') || []
|
78
|
+
end
|
73
79
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
data = @serializer.serialize(items, wrapper)
|
78
|
-
runRequest('POST', url, nil, data)
|
79
|
-
end
|
80
|
+
def addConfigurations(items)
|
81
|
+
updateConfigurations(items)
|
82
|
+
end
|
80
83
|
|
81
|
-
|
82
|
-
|
84
|
+
def updateConfigurations(items)
|
85
|
+
url = "#{@host}/configurations.#{@format}"
|
86
|
+
wrapper = get_type_wrapper('update_configurations')
|
87
|
+
data = @serializer.serialize(items, wrapper)
|
88
|
+
runRequest('POST', url, nil, data)
|
89
|
+
end
|
83
90
|
|
84
|
-
|
85
|
-
|
86
|
-
runRequest('DELETE', url, nil, data)
|
87
|
-
end
|
91
|
+
def deleteConfigurations(items)
|
92
|
+
url = "#{@host}/configurations.#{@format}"
|
88
93
|
|
89
|
-
|
90
|
-
|
91
|
-
url
|
92
|
-
else
|
93
|
-
url = "#@host/blacklist.#@format?config_id=#{config_id}"
|
94
|
+
wrapper = get_type_wrapper('delete_configurations')
|
95
|
+
data = @serializer.serialize(items, wrapper)
|
96
|
+
runRequest('DELETE', url, nil, data)
|
94
97
|
end
|
95
98
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
+
def getBlacklist(config_id = nil)
|
100
|
+
if config_id.nil?
|
101
|
+
url = "#{@host}/blacklist.#{@format}"
|
102
|
+
else
|
103
|
+
url = "#{@host}/blacklist.#{@format}?config_id=#{config_id}"
|
104
|
+
end
|
99
105
|
|
100
|
-
|
101
|
-
|
102
|
-
end
|
106
|
+
runRequest('GET', url, 'get_blacklist') || []
|
107
|
+
end
|
103
108
|
|
104
|
-
|
105
|
-
|
106
|
-
url = "#@host/blacklist.#@format"
|
107
|
-
else
|
108
|
-
url = "#@host/blacklist.#@format?config_id=#{config_id}"
|
109
|
+
def addBlacklist(items, config_id = nil)
|
110
|
+
updateBlacklist(items, config_id)
|
109
111
|
end
|
110
112
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
113
|
+
def updateBlacklist(items, config_id = nil)
|
114
|
+
if config_id.nil?
|
115
|
+
url = "#{@host}/blacklist.#{@format}"
|
116
|
+
else
|
117
|
+
url = "#{@host}/blacklist.#{@format}?config_id=#{config_id}"
|
118
|
+
end
|
115
119
|
|
116
|
-
|
117
|
-
|
118
|
-
url
|
119
|
-
else
|
120
|
-
url = "#@host/blacklist.#@format?config_id=#{config_id}"
|
120
|
+
wrapper = get_type_wrapper('update_blacklist')
|
121
|
+
data = @serializer.serialize(items, wrapper)
|
122
|
+
runRequest('POST', url, nil, data)
|
121
123
|
end
|
122
124
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
125
|
+
def removeBlacklist(items, config_id = nil)
|
126
|
+
if config_id.nil?
|
127
|
+
url = "#{@host}/blacklist.#{@format}"
|
128
|
+
else
|
129
|
+
url = "#{@host}/blacklist.#{@format}?config_id=#{config_id}"
|
130
|
+
end
|
127
131
|
|
128
|
-
|
129
|
-
|
130
|
-
url
|
131
|
-
else
|
132
|
-
url = "#@host/categories.#@format?config_id=#{config_id}"
|
132
|
+
wrapper = get_type_wrapper('remove_blacklist')
|
133
|
+
data = @serializer.serialize(items, wrapper)
|
134
|
+
runRequest('DELETE', url, nil, data)
|
133
135
|
end
|
134
136
|
|
135
|
-
|
136
|
-
|
137
|
-
|
137
|
+
def getCategories(config_id = nil)
|
138
|
+
if config_id.nil?
|
139
|
+
url = "#{@host}/categories.#{@format}"
|
140
|
+
else
|
141
|
+
url = "#{@host}/categories.#{@format}?config_id=#{config_id}"
|
142
|
+
end
|
138
143
|
|
139
|
-
|
140
|
-
|
141
|
-
end
|
144
|
+
runRequest('GET', url, 'get_categories') || []
|
145
|
+
end
|
142
146
|
|
143
|
-
|
144
|
-
|
145
|
-
url = "#@host/categories.#@format"
|
146
|
-
else
|
147
|
-
url = "#@host/categories.#@format?config_id=#{config_id}"
|
147
|
+
def addCategories(items, config_id = nil)
|
148
|
+
updateCategories(items, config_id)
|
148
149
|
end
|
149
150
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
151
|
+
def updateCategories(items, config_id = nil)
|
152
|
+
if config_id.nil?
|
153
|
+
url = "#{@host}/categories.#{@format}"
|
154
|
+
else
|
155
|
+
url = "#{@host}/categories.#{@format}?config_id=#{config_id}"
|
156
|
+
end
|
154
157
|
|
155
|
-
|
156
|
-
|
157
|
-
url
|
158
|
-
else
|
159
|
-
url = "#@host/categories.#@format?config_id=#{config_id}"
|
158
|
+
wrapper = get_type_wrapper('update_categories')
|
159
|
+
data = @serializer.serialize(items, wrapper)
|
160
|
+
runRequest('POST', url, nil, data)
|
160
161
|
end
|
161
162
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
163
|
+
def removeCategories(items, config_id = nil)
|
164
|
+
if config_id.nil?
|
165
|
+
url = "#{@host}/categories.#{@format}"
|
166
|
+
else
|
167
|
+
url = "#{@host}/categories.#{@format}?config_id=#{config_id}"
|
168
|
+
end
|
166
169
|
|
167
|
-
|
168
|
-
|
169
|
-
url
|
170
|
-
else
|
171
|
-
url = "#@host/queries.#@format?config_id=#{config_id}"
|
170
|
+
wrapper = get_type_wrapper('remove_categories')
|
171
|
+
data = @serializer.serialize(items, wrapper)
|
172
|
+
runRequest('DELETE', url, nil, data)
|
172
173
|
end
|
173
174
|
|
174
|
-
|
175
|
-
|
176
|
-
|
175
|
+
def getQueries(config_id = nil)
|
176
|
+
if config_id.nil?
|
177
|
+
url = "#{@host}/queries.#{@format}"
|
178
|
+
else
|
179
|
+
url = "#{@host}/queries.#{@format}?config_id=#{config_id}"
|
180
|
+
end
|
177
181
|
|
178
|
-
|
179
|
-
|
180
|
-
end
|
182
|
+
runRequest('GET', url, 'get_queries') || []
|
183
|
+
end
|
181
184
|
|
182
|
-
|
183
|
-
|
184
|
-
url = "#@host/queries.#@format"
|
185
|
-
else
|
186
|
-
url = "#@host/queries.#@format?config_id=#{config_id}"
|
185
|
+
def addQueries(items, config_id = nil)
|
186
|
+
updateQueries(items, config_id)
|
187
187
|
end
|
188
188
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
189
|
+
def updateQueries(items, config_id = nil)
|
190
|
+
if config_id.nil?
|
191
|
+
url = "#{@host}/queries.#{@format}"
|
192
|
+
else
|
193
|
+
url = "#{@host}/queries.#{@format}?config_id=#{config_id}"
|
194
|
+
end
|
193
195
|
|
194
|
-
|
195
|
-
|
196
|
-
url
|
197
|
-
else
|
198
|
-
url = "#@host/queries.#@format?config_id=#{config_id}"
|
196
|
+
wrapper = get_type_wrapper('update_queries')
|
197
|
+
data = @serializer.serialize(items, wrapper)
|
198
|
+
runRequest('POST', url, nil, data)
|
199
199
|
end
|
200
200
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
201
|
+
def removeQueries(items, config_id = nil)
|
202
|
+
if config_id.nil?
|
203
|
+
url = "#{@host}/queries.#{@format}"
|
204
|
+
else
|
205
|
+
url = "#{@host}/queries.#{@format}?config_id=#{config_id}"
|
206
|
+
end
|
205
207
|
|
206
|
-
|
207
|
-
|
208
|
-
url
|
209
|
-
else
|
210
|
-
url = "#@host/phrases.#@format?config_id=#{config_id}"
|
208
|
+
wrapper = get_type_wrapper('remove_queries')
|
209
|
+
data = @serializer.serialize(items, wrapper)
|
210
|
+
runRequest('DELETE', url, nil, data)
|
211
211
|
end
|
212
212
|
|
213
|
-
|
214
|
-
|
215
|
-
|
213
|
+
def getPhrases(config_id = nil)
|
214
|
+
if config_id.nil?
|
215
|
+
url = "#{@host}/phrases.#{@format}"
|
216
|
+
else
|
217
|
+
url = "#{@host}/phrases.#{@format}?config_id=#{config_id}"
|
218
|
+
end
|
216
219
|
|
217
|
-
|
218
|
-
|
219
|
-
end
|
220
|
+
runRequest('GET', url, 'get_sentiment_phrases') || []
|
221
|
+
end
|
220
222
|
|
221
|
-
|
222
|
-
|
223
|
-
url = "#@host/phrases.#@format"
|
224
|
-
else
|
225
|
-
url = "#@host/phrases.#@format?config_id=#{config_id}"
|
223
|
+
def addPhrases(items, config_id = nil)
|
224
|
+
updatePhrases(items, config_id)
|
226
225
|
end
|
227
226
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
227
|
+
def updatePhrases(items, config_id = nil)
|
228
|
+
if config_id.nil?
|
229
|
+
url = "#{@host}/phrases.#{@format}"
|
230
|
+
else
|
231
|
+
url = "#{@host}/phrases.#{@format}?config_id=#{config_id}"
|
232
|
+
end
|
232
233
|
|
233
|
-
|
234
|
-
|
235
|
-
url
|
236
|
-
else
|
237
|
-
url = "#@host/phrases.#@format?config_id=#{config_id}"
|
234
|
+
wrapper = get_type_wrapper('update_sentiment_phrases')
|
235
|
+
data = @serializer.serialize(items, wrapper)
|
236
|
+
runRequest('POST', url, nil, data)
|
238
237
|
end
|
239
238
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
239
|
+
def removePhrases(items, config_id = nil)
|
240
|
+
if config_id.nil?
|
241
|
+
url = "#{@host}/phrases.#{@format}"
|
242
|
+
else
|
243
|
+
url = "#{@host}/phrases.#{@format}?config_id=#{config_id}"
|
244
|
+
end
|
244
245
|
|
245
|
-
|
246
|
-
|
247
|
-
url
|
248
|
-
else
|
249
|
-
url = "#@host/entities.#@format?config_id=#{config_id}"
|
246
|
+
wrapper = get_type_wrapper('remove_phrases')
|
247
|
+
data = @serializer.serialize(items, wrapper)
|
248
|
+
runRequest('DELETE', url, nil, data)
|
250
249
|
end
|
251
250
|
|
252
|
-
|
253
|
-
|
254
|
-
|
251
|
+
def getEntities(config_id = nil)
|
252
|
+
if config_id.nil?
|
253
|
+
url = "#{@host}/entities.#{@format}"
|
254
|
+
else
|
255
|
+
url = "#{@host}/entities.#{@format}?config_id=#{config_id}"
|
256
|
+
end
|
255
257
|
|
256
|
-
|
257
|
-
|
258
|
-
end
|
258
|
+
runRequest('GET', url, 'get_entities') || []
|
259
|
+
end
|
259
260
|
|
260
|
-
|
261
|
-
|
262
|
-
url = "#@host/entities.#@format"
|
263
|
-
else
|
264
|
-
url = "#@host/entities.#@format?config_id=#{config_id}"
|
261
|
+
def addEntities(items, config_id = nil)
|
262
|
+
updateEntities(items, config_id)
|
265
263
|
end
|
266
264
|
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
265
|
+
def updateEntities(items, config_id = nil)
|
266
|
+
if config_id.nil?
|
267
|
+
url = "#{@host}/entities.#{@format}"
|
268
|
+
else
|
269
|
+
url = "#{@host}/entities.#{@format}?config_id=#{config_id}"
|
270
|
+
end
|
271
271
|
|
272
|
-
|
273
|
-
|
274
|
-
url
|
275
|
-
else
|
276
|
-
url = "#@host/entities.#@format?config_id=#{config_id}"
|
272
|
+
wrapper = get_type_wrapper('update_entities')
|
273
|
+
data = @serializer.serialize(items, wrapper)
|
274
|
+
runRequest('POST', url, nil, data)
|
277
275
|
end
|
278
276
|
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
277
|
+
def removeEntities(items, config_id = nil)
|
278
|
+
if config_id.nil?
|
279
|
+
url = "#{@host}/entities.#{@format}"
|
280
|
+
else
|
281
|
+
url = "#{@host}/entities.#{@format}?config_id=#{config_id}"
|
282
|
+
end
|
283
283
|
|
284
|
-
|
285
|
-
|
286
|
-
url
|
287
|
-
else
|
288
|
-
url = "#@host/document.#@format?config_id=#{config_id}"
|
284
|
+
wrapper = get_type_wrapper('remove_entities')
|
285
|
+
data = @serializer.serialize(items, wrapper)
|
286
|
+
runRequest('DELETE', url, nil, data)
|
289
287
|
end
|
290
288
|
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
else
|
298
|
-
result
|
299
|
-
end
|
300
|
-
end
|
289
|
+
def queueDocument(task, config_id = nil)
|
290
|
+
if config_id.nil?
|
291
|
+
url = "#{@host}/document.#{@format}"
|
292
|
+
else
|
293
|
+
url = "#{@host}/document.#{@format}?config_id=#{config_id}"
|
294
|
+
end
|
301
295
|
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
296
|
+
wrapper = get_type_wrapper('queue_document')
|
297
|
+
data = @serializer.serialize(task, wrapper)
|
298
|
+
result = runRequest('POST', url, 'get_processed_documents', data)
|
299
|
+
if result != nil && result.is_a?(Array)
|
300
|
+
onDocsAutoResponse(result)
|
301
|
+
200
|
302
|
+
else
|
303
|
+
result
|
304
|
+
end
|
307
305
|
end
|
308
306
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
307
|
+
def queueBatch(batch, config_id = nil)
|
308
|
+
if config_id.nil?
|
309
|
+
url = "#{@host}/document/batch.#{@format}"
|
310
|
+
else
|
311
|
+
url = "#{@host}/document/batch.#{@format}?config_id=#{config_id}"
|
312
|
+
end
|
313
|
+
|
314
|
+
wrapper = get_type_wrapper('queue_batch_documents')
|
315
|
+
data = @serializer.serialize(batch, wrapper)
|
316
|
+
result = runRequest('POST', url, 'get_processed_documents', data)
|
317
|
+
if result != nil && result.is_a?(Array)
|
318
|
+
onDocsAutoResponse(result)
|
319
|
+
200
|
320
|
+
else
|
321
|
+
result
|
322
|
+
end
|
317
323
|
end
|
318
|
-
end
|
319
324
|
|
320
|
-
|
321
|
-
|
325
|
+
def getDocument(doc_id, config_id = nil)
|
326
|
+
fail 'Document ID is nil or empty' if doc_id.nil?
|
327
|
+
|
328
|
+
if config_id.nil?
|
329
|
+
url = "#{@host}/document/#{doc_id}.#{@format}"
|
330
|
+
else
|
331
|
+
url = "#{@host}/document/#{doc_id}.#{@format}?config_id=#{config_id}"
|
332
|
+
end
|
322
333
|
|
323
|
-
|
324
|
-
url = "#@host/document/#{doc_id}.#@format"
|
325
|
-
else
|
326
|
-
url = "#@host/document/#{doc_id}.#@format?config_id=#{config_id}"
|
334
|
+
runRequest('GET', url, 'get_document')
|
327
335
|
end
|
328
336
|
|
329
|
-
|
330
|
-
|
337
|
+
def cancelDocument(doc_id, config_id = nil)
|
338
|
+
fail 'Document ID is nil or empty' if doc_id.nil?
|
331
339
|
|
332
|
-
|
333
|
-
|
340
|
+
if config_id.nil?
|
341
|
+
url = "#{@host}/document/#{doc_id}.#{@format}"
|
342
|
+
else
|
343
|
+
url = "#{@host}/document/#{doc_id}.#{@format}?config_id=#{config_id}"
|
344
|
+
end
|
334
345
|
|
335
|
-
|
336
|
-
url = "#@host/document/#{doc_id}.#@format"
|
337
|
-
else
|
338
|
-
url = "#@host/document/#{doc_id}.#@format?config_id=#{config_id}"
|
346
|
+
runRequest('DELETE', url)
|
339
347
|
end
|
340
348
|
|
341
|
-
|
342
|
-
|
349
|
+
def getProcessedDocuments(config_id = nil)
|
350
|
+
if config_id.nil?
|
351
|
+
url = "#{@host}/document/processed.#{@format}"
|
352
|
+
else
|
353
|
+
url = "#{@host}/document/processed.#{@format}?config_id=#{config_id}"
|
354
|
+
end
|
343
355
|
|
344
|
-
|
345
|
-
|
346
|
-
url = "#@host/document/processed.#@format"
|
347
|
-
else
|
348
|
-
url = "#@host/document/processed.#@format?config_id=#{config_id}"
|
356
|
+
result = runRequest('GET', url, 'get_processed_documents')
|
357
|
+
result ||= []
|
349
358
|
end
|
350
359
|
|
351
|
-
|
352
|
-
|
353
|
-
end
|
360
|
+
def getProcessedDocumentsByJobId(job_id)
|
361
|
+
url = "#{@host}/document/processed.#{@format}?job_id=#{job_id}"
|
354
362
|
|
355
|
-
|
356
|
-
|
357
|
-
url = "#@host/collection.#@format"
|
358
|
-
else
|
359
|
-
url = "#@host/collection.#@format?config_id=#{config_id}"
|
363
|
+
result = runRequest('GET', url, 'get_processed_documents_by_job_id')
|
364
|
+
result ||= []
|
360
365
|
end
|
361
366
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
367
|
+
def queueCollection(task, config_id = nil)
|
368
|
+
if config_id.nil?
|
369
|
+
url = "#{@host}/collection.#{@format}"
|
370
|
+
else
|
371
|
+
url = "#{@host}/collection.#{@format}?config_id=#{config_id}"
|
372
|
+
end
|
373
|
+
|
374
|
+
wrapper = get_type_wrapper('queue_collection')
|
375
|
+
data = @serializer.serialize(task, wrapper)
|
376
|
+
result = runRequest('POST', url, 'get_processed_collections', data)
|
377
|
+
if result != nil && result.is_a?(Array)
|
378
|
+
onCollsAutoResponse(result)
|
379
|
+
200
|
380
|
+
else
|
381
|
+
result
|
382
|
+
end
|
370
383
|
end
|
371
|
-
end
|
372
384
|
|
373
|
-
|
374
|
-
|
385
|
+
def getCollection(id, config_id = nil)
|
386
|
+
fail 'Collection ID is nil or empty' if id.nil?
|
387
|
+
|
388
|
+
if config_id.nil?
|
389
|
+
url = "#{@host}/collection/#{id}.#{@format}"
|
390
|
+
else
|
391
|
+
url = "#{@host}/collection/#{id}.#{@format}?config_id=#{config_id}"
|
392
|
+
end
|
375
393
|
|
376
|
-
|
377
|
-
url = "#@host/collection/#{id}.#@format"
|
378
|
-
else
|
379
|
-
url = "#@host/collection/#{id}.#@format?config_id=#{config_id}"
|
394
|
+
runRequest('GET', url, 'get_collection')
|
380
395
|
end
|
381
396
|
|
382
|
-
|
383
|
-
|
397
|
+
def cancelCollection(id, config_id = nil)
|
398
|
+
fail 'Collection ID is nil or empty' if id.nil?
|
384
399
|
|
385
|
-
|
386
|
-
|
400
|
+
if config_id.nil?
|
401
|
+
url = "#{@host}/collection/#{id}.#{@format}"
|
402
|
+
else
|
403
|
+
url = "#{@host}/collection/#{id}.#{@format}?config_id=#{config_id}"
|
404
|
+
end
|
387
405
|
|
388
|
-
|
389
|
-
url = "#@host/collection/#{id}.#@format"
|
390
|
-
else
|
391
|
-
url = "#@host/collection/#{id}.#@format?config_id=#{config_id}"
|
406
|
+
runRequest('DELETE', url)
|
392
407
|
end
|
393
408
|
|
394
|
-
|
395
|
-
|
409
|
+
def getProcessedCollections(config_id = nil)
|
410
|
+
if config_id.nil?
|
411
|
+
url = "#{@host}/collection/processed.#{@format}"
|
412
|
+
else
|
413
|
+
url = "#{@host}/collection/processed.#{@format}?config_id=#{config_id}"
|
414
|
+
end
|
396
415
|
|
397
|
-
|
398
|
-
|
399
|
-
url = "#@host/collection/processed.#@format"
|
400
|
-
else
|
401
|
-
url = "#@host/collection/processed.#@format?config_id=#{config_id}"
|
416
|
+
result = runRequest('GET', url, 'get_processed_collections')
|
417
|
+
result ||= []
|
402
418
|
end
|
403
419
|
|
404
|
-
|
405
|
-
|
406
|
-
end
|
420
|
+
def getProcessedCollectionsByJobId(job_id)
|
421
|
+
url = "#{@host}/collection/processed.#{@format}?job_id=#{job_id}"
|
407
422
|
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
onRequest({'method' => method, 'url' => url, 'message' => post_data})
|
412
|
-
response = request.authWebRequest(method, url, post_data)
|
413
|
-
onResponse({'status' => response[:status], 'reason' => response[:reason], 'message' => response[:data]})
|
423
|
+
result = runRequest('GET', url, 'get_processed_collections_by_job_id')
|
424
|
+
result ||= []
|
425
|
+
end
|
414
426
|
|
415
|
-
|
416
|
-
|
427
|
+
private
|
428
|
+
def runRequest(method, url, type = nil, post_data = nil)
|
429
|
+
request = AuthRequest.new(@consumer_key, @consumer_secret, @application_name, @use_compression)
|
430
|
+
|
431
|
+
onRequest({'method' => method, 'url' => url, 'message' => post_data})
|
432
|
+
|
433
|
+
response = request.authWebRequest(method, url, post_data)
|
434
|
+
|
435
|
+
onResponse({'status' => response[:status], 'reason' => response[:reason], 'message' => response[:data]})
|
417
436
|
|
418
|
-
|
437
|
+
status = response[:status]
|
438
|
+
message = response[:reason]
|
419
439
|
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
resolve_error(status, message)
|
425
|
-
return status
|
426
|
-
end
|
427
|
-
else
|
428
|
-
if status == 200
|
429
|
-
handler = get_type_handler(type)
|
430
|
-
message = @serializer.deserialize(response[:data], handler)
|
431
|
-
return message
|
432
|
-
elsif status == 202
|
433
|
-
if method == 'POST'
|
440
|
+
message = response[:data] unless response[:data].to_s.empty?
|
441
|
+
|
442
|
+
if method == 'DELETE'
|
443
|
+
if status == 200 || status == 202
|
434
444
|
return status
|
435
445
|
else
|
436
|
-
|
446
|
+
resolve_error(status, message)
|
447
|
+
return status
|
437
448
|
end
|
438
449
|
else
|
439
|
-
|
450
|
+
if status == 200
|
451
|
+
handler = get_type_handler(type)
|
452
|
+
message = @serializer.deserialize(response[:data], handler)
|
453
|
+
return message
|
454
|
+
elsif status == 202
|
455
|
+
if method == 'POST'
|
456
|
+
return status
|
457
|
+
else
|
458
|
+
return nil
|
459
|
+
end
|
460
|
+
else
|
461
|
+
resolve_error(status, message)
|
462
|
+
end
|
440
463
|
end
|
441
464
|
end
|
442
|
-
end
|
443
465
|
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
466
|
+
def get_type_handler(type)
|
467
|
+
if @serializer.gettype() == 'json'
|
468
|
+
return nil
|
469
|
+
end
|
470
|
+
|
471
|
+
#only for xml serializer
|
472
|
+
case
|
473
|
+
when type == 'get_status' then return GetStatusHandler.new()
|
474
|
+
when type == 'get_subscription' then return GetSubscriptionHandler.new()
|
475
|
+
when type == 'get_configurations' then return GetConfigurationsHandler.new()
|
476
|
+
when type == 'get_blacklist' then return GetBlacklistHandler.new()
|
477
|
+
when type == 'get_categories' then return GetCategoriesHandler.new()
|
478
|
+
when type == 'get_queries' then return GetQueriesHandler.new()
|
479
|
+
when type == 'get_sentiment_phrases' then return GetSentimentPhrasesHandler.new()
|
480
|
+
when type == 'get_entities' then return GetEntitiesHandler.new()
|
481
|
+
when type == 'get_document' then return GetDocumentHandler.new()
|
482
|
+
when type == 'get_processed_documents' then return GetProcessedDocumentsHandler.new()
|
483
|
+
when type == 'get_collection' then return GetCollectionHandler.new()
|
484
|
+
when type == 'get_processed_collections' then return GetProcessedCollectionsHandler.new()
|
485
|
+
else return nil
|
486
|
+
end
|
464
487
|
end
|
465
|
-
end
|
466
488
|
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
#only for xml serializer
|
473
|
-
#if type == "update_configurations"
|
474
|
-
# return {"root" => "configurations", "added" => "configuration", "removed" => "configuration"}
|
475
|
-
#elsif type == "update_blacklist"
|
476
|
-
# return {"root" => "blacklist", "added" => "item", "removed" => "item"}
|
477
|
-
#elsif type == "update_categories"
|
478
|
-
# return {"root" => "categories", "added" => "category", "removed" => "category", "samples" => "sample"}
|
479
|
-
#elsif type == "update_queries"
|
480
|
-
# return {"root" => "queries", "added" => "query", "removed" => "query"}
|
481
|
-
#elsif type == "update_sentiment_phrases"
|
482
|
-
# return {"root" => "phrases", "added" => "phrase", "removed" => "phrase"}
|
483
|
-
#elsif type == "update_entities"
|
484
|
-
# return {"root" => "entities", "added" => "entity", "removed" => "entity"}
|
485
|
-
#elsif type == "queue_document"
|
486
|
-
# return {"root" => "document"}
|
487
|
-
#elsif type == "queue_batch_documents"
|
488
|
-
# return {"root" => "documents", "item" => "document"}
|
489
|
-
#elsif type == "queue_collection"
|
490
|
-
# return {"root" => "collection", "documents" => "document"}
|
491
|
-
#else
|
492
|
-
# return nil
|
493
|
-
#end
|
494
|
-
end
|
489
|
+
def get_type_wrapper(type)
|
490
|
+
if @serializer.gettype() == 'json'
|
491
|
+
nil
|
492
|
+
end
|
495
493
|
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
494
|
+
#only for xml serializer
|
495
|
+
#if type == "update_configurations"
|
496
|
+
# return {"root" => "configurations", "added" => "configuration", "removed" => "configuration"}
|
497
|
+
#elsif type == "update_blacklist"
|
498
|
+
# return {"root" => "blacklist", "added" => "item", "removed" => "item"}
|
499
|
+
#elsif type == "update_categories"
|
500
|
+
# return {"root" => "categories", "added" => "category", "removed" => "category", "samples" => "sample"}
|
501
|
+
#elsif type == "update_queries"
|
502
|
+
# return {"root" => "queries", "added" => "query", "removed" => "query"}
|
503
|
+
#elsif type == "update_sentiment_phrases"
|
504
|
+
# return {"root" => "phrases", "added" => "phrase", "removed" => "phrase"}
|
505
|
+
#elsif type == "update_entities"
|
506
|
+
# return {"root" => "entities", "added" => "entity", "removed" => "entity"}
|
507
|
+
#elsif type == "queue_document"
|
508
|
+
# return {"root" => "document"}
|
509
|
+
#elsif type == "queue_batch_documents"
|
510
|
+
# return {"root" => "documents", "item" => "document"}
|
511
|
+
#elsif type == "queue_collection"
|
512
|
+
# return {"root" => "collection", "documents" => "document"}
|
513
|
+
#else
|
514
|
+
# return nil
|
515
|
+
#end
|
516
|
+
end
|
517
|
+
|
518
|
+
def resolve_error(status, message = nil)
|
519
|
+
if status == 400 || status == 401 || status == 402 || status == 403 || status == 406 || status == 500
|
520
|
+
onError({'status' => status, 'message' => message})
|
521
|
+
else
|
522
|
+
fail "HTTP error was found with status: #{status} and message: #{message}"
|
523
|
+
end
|
501
524
|
end
|
502
|
-
end
|
503
525
|
|
504
|
-
|
505
|
-
|
506
|
-
|
526
|
+
def onRequest(request)
|
527
|
+
unless @callback.nil?
|
528
|
+
@callback.onRequest(self, request)
|
529
|
+
end
|
507
530
|
end
|
508
|
-
end
|
509
531
|
|
510
|
-
|
511
|
-
|
512
|
-
|
532
|
+
def onResponse(response)
|
533
|
+
unless @callback.nil?
|
534
|
+
@callback.onResponse(self, response)
|
535
|
+
end
|
513
536
|
end
|
514
|
-
end
|
515
537
|
|
516
|
-
|
517
|
-
|
518
|
-
|
538
|
+
def onError(response)
|
539
|
+
unless @callback.nil?
|
540
|
+
@callback.onError(self, response)
|
541
|
+
end
|
519
542
|
end
|
520
|
-
end
|
521
543
|
|
522
|
-
|
523
|
-
|
524
|
-
|
544
|
+
def onDocsAutoResponse(response)
|
545
|
+
unless @callback.nil?
|
546
|
+
@callback.onDocsAutoResponse(self, response)
|
547
|
+
end
|
525
548
|
end
|
526
|
-
end
|
527
549
|
|
528
|
-
|
529
|
-
|
530
|
-
|
550
|
+
def onCollsAutoResponse(response)
|
551
|
+
unless @callback.nil?
|
552
|
+
@callback.onCollsAutoResponse(self, response)
|
553
|
+
end
|
531
554
|
end
|
532
555
|
end
|
533
|
-
end
|
534
556
|
|
535
|
-
class CallbackHandler
|
536
|
-
|
537
|
-
|
538
|
-
|
557
|
+
class CallbackHandler
|
558
|
+
def initialize
|
559
|
+
fail 'Don\'t instantiate me!' if abstract_class?
|
560
|
+
end
|
539
561
|
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
562
|
+
private
|
563
|
+
def abstract_class?
|
564
|
+
self.class == CallbackHandler
|
565
|
+
end
|
544
566
|
|
545
|
-
|
546
|
-
|
547
|
-
|
567
|
+
def onRequest(sender, args)
|
568
|
+
fail 'Abstract method onRequest'
|
569
|
+
end
|
548
570
|
|
549
|
-
|
550
|
-
|
551
|
-
|
571
|
+
def onResponse(sender, args)
|
572
|
+
fail 'Abstract method onResponse'
|
573
|
+
end
|
552
574
|
|
553
|
-
|
554
|
-
|
555
|
-
|
575
|
+
def onError(sender, args)
|
576
|
+
fail 'Abstract method onError'
|
577
|
+
end
|
556
578
|
|
557
|
-
|
558
|
-
|
559
|
-
|
579
|
+
def onDocsAutoResponse(sender, args)
|
580
|
+
fail 'Abstract method onDocsAutoResponse'
|
581
|
+
end
|
560
582
|
|
561
|
-
|
562
|
-
|
583
|
+
def onCollsAutoResponse(sender, args)
|
584
|
+
fail 'Abstract method onCollsAutoResponse'
|
585
|
+
end
|
563
586
|
end
|
564
|
-
end
|
587
|
+
end
|