havenondemand 1.2.1 → 1.3.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.
@@ -1,3 +1,3 @@
1
1
  module Havenondemand
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -0,0 +1,275 @@
1
+ require 'json'
2
+ require 'Unirest'
3
+
4
+ class HODClient
5
+ def initialize(apikey, version="v1")
6
+ # Instance variables
7
+ if apikey=='http://api.havenondemand.com' || apikey=='http://api.havenondemand.com/' || apikey=='https://api.havenondemand.com' || apikey=='https://api.havenondemand.com/'
8
+ raise ArgumentError, "Using an outdated wrapper constructor method. No need to include API URL.\nInclude as such:\n client = HODClient(API_KEY, VERSION)\n where version is optional"
9
+ end
10
+ @apikey = apikey
11
+ @ver = version
12
+ @hodAppBase = "https://api.havenondemand.com/1/api/";
13
+ @hodJobResultBase = "https://api.havenondemand.com/1/job/result/";
14
+ @hodJobStatusBase = "https://api.havenondemand.com/1/job/status/";
15
+ @hodCombineAsync = "async/executecombination";
16
+ @hodCombineSync = "sync/executecombination";
17
+ @timeoutVal = 120
18
+ end
19
+
20
+
21
+ def get_job_status(jobID,callback)
22
+ data={"apikey"=>@apikey}
23
+ response=Unirest.post "#{@hodJobStatusBase}#{jobID}",
24
+ headers:{ "Accept" => "application/json" },
25
+ parameters:data
26
+ if response.code == 200
27
+ if callback != nil
28
+ callback.call(response.body)
29
+ else
30
+ return response.body
31
+ end
32
+ else
33
+ puts "Error: #{response.body}"
34
+ err = create_error_object(response.body["error"],response.body["reason"],response.body["detail"],"")
35
+ if callback != nil
36
+ callback.call(err)
37
+ else
38
+ return err
39
+ end
40
+ end
41
+ end
42
+
43
+ def get_job_result(jobID,callback)
44
+ data={"apikey"=>@apikey}
45
+ response=Unirest.post "#{@hodJobResultBase}#{jobID}",
46
+ headers:{ "Accept" => "application/json" },
47
+ parameters:data
48
+ if response.code == 200
49
+ if callback != nil
50
+ callback.call(response.body)
51
+ else
52
+ return response.body
53
+ end
54
+ else
55
+ err = create_error_object(response.body["error"],response.body["reason"],response.body["detail"],"")
56
+ if callback != nil
57
+ callback.call(err)
58
+ else
59
+ return err
60
+ end
61
+ end
62
+ end
63
+
64
+ def get_request(hodApp, params, async=false,callback)
65
+ url=''
66
+ if async == true
67
+ url = "#{@hodAppBase}async/#{hodApp}/#{@ver}?apikey=#{@apikey}"
68
+ else
69
+ url = "#{@hodAppBase}sync/#{hodApp}/#{@ver}?apikey=#{@apikey}"
70
+ end
71
+ params.each do |key, value|
72
+ if "#{key}" == "file"
73
+ raise ArgumentError, "File upload must be used with PostRequest method"
74
+ else
75
+ if value.kind_of?(Array)
76
+ value.each { |x|
77
+ p = "&#{key}=#{x}"
78
+ url = [url, p].join()
79
+ }
80
+ else
81
+ p = "&#{key}=#{value}"
82
+ url = [url, p].join()
83
+ end
84
+ end
85
+ end
86
+ Unirest.timeout(@timeoutVal)
87
+
88
+ response=Unirest.get ("#{url}")
89
+ if response.code == 200
90
+ if callback != nil
91
+ callback.call(response.body)
92
+ else
93
+ return response.body
94
+ end
95
+ else
96
+ puts "Error: #{response.body}"
97
+ err = create_error_object(response.body["error"],response.body["reason"],response.body["detail"],"")
98
+ if callback != nil
99
+ callback.call(err)
100
+ else
101
+ return err
102
+ end
103
+ end
104
+ end
105
+
106
+ def post_request(hodApp, params, async=true, callback)
107
+ endPoint = ''
108
+ if async == true
109
+ endPoint = "#{@hodAppBase}async/#{hodApp}/#{@ver}"
110
+ else
111
+ endPoint = "#{@hodAppBase}sync/#{hodApp}/#{@ver}"
112
+ end
113
+ data = {}
114
+ data.compare_by_identity
115
+ data["apikey"]=@apikey
116
+ params.each do |key, value|
117
+ if "#{key}" == "file"
118
+ if value.kind_of?(Array)
119
+ value.each { |file|
120
+ data["file"] = File.new(file, 'rb')
121
+ }
122
+ else
123
+ data["file"] = File.new(value, 'rb')
124
+ end
125
+ else
126
+ if value.kind_of?(Array)
127
+ value.each { |x|
128
+ data["#{key}"] = x
129
+ }
130
+ else
131
+ data["#{key}"] = value
132
+ end
133
+ end
134
+ end
135
+ Unirest.timeout(@timeoutVal)
136
+ response=Unirest.post endPoint,
137
+ headers:{ "Accept" => "application/json", "Content-Type" => "application/json"},
138
+ parameters:data
139
+
140
+ if response.code == 200
141
+ if callback != nil
142
+ callback.call(response.body)
143
+ else
144
+ return response.body
145
+ end
146
+ else
147
+ puts "Error: #{response.body}"
148
+ err = create_error_object(response.body["error"],response.body["reason"],response.body["detail"],"")
149
+ if callback != nil
150
+ callback.call(err)
151
+ else
152
+ return err
153
+ end
154
+ end
155
+ end
156
+
157
+ def get_request_combination(hodApp, params, async=false, callback)
158
+ url=''
159
+ if async == true
160
+ url = "#{@hodAppBase}#{@hodCombineAsync}/#{@ver}"
161
+ else
162
+ url = "#{@hodAppBase}#{@hodCombineSync}/#{@ver}"
163
+ end
164
+ url = [url, "?apikey=#{@apikey}&combination=#{hodApp}"].join()
165
+ params.each do |key, value|
166
+ if "#{key}" == "file"
167
+ raise ArgumentError, "File upload must be used with post_combination method"
168
+ else
169
+ if valid_json?(value)
170
+ raise ArgumentError, "JSON input must be used with post_combination method"
171
+ else
172
+ p = "\"{\"name\":\"#{key}\",\"value\":\"#{value}\"}\""
173
+ url = [url, "&parameters=",p].join()
174
+ end
175
+ end
176
+ end
177
+ Unirest.timeout(@timeoutVal)
178
+
179
+ response=Unirest.get ("#{url}")
180
+ if response.code == 200
181
+ if callback != nil
182
+ callback.call(response.body)
183
+ else
184
+ return response.body
185
+ end
186
+ else
187
+ puts "Error: #{response.body}"
188
+ err = create_error_object(response.body["error"],response.body["reason"],response.body["detail"],"")
189
+ if callback != nil
190
+ callback.call(err)
191
+ else
192
+ return err
193
+ end
194
+ end
195
+ end
196
+
197
+ def post_request_combination(combinationName, params, async=false, callback)
198
+ endPoint=''
199
+ if async == true
200
+ endPoint = "#{@hodAppBase}#{@hodCombineAsync}/#{@ver}"
201
+ else
202
+ endPoint = "#{@hodAppBase}#{@hodCombineSync}/#{@ver}"
203
+ end
204
+ data = {}
205
+ data.compare_by_identity
206
+ data["apikey"]=@apikey
207
+ data["combination"] = combinationName
208
+
209
+ params.each do |key, value|
210
+ if "#{key}" == "file"
211
+ if value.kind_of?(Array)
212
+ for index in 0 ... value.size
213
+ value[index].each_pair {|kk,vv|
214
+ data["file_parameters"] = kk
215
+ data["file"] = File.new(vv, 'rb')
216
+ }
217
+ end
218
+ else
219
+ value.each_pair {|kk,vv|
220
+ data["file_parameters"] = kk
221
+ data["file"] = File.new(vv, 'rb')
222
+ }
223
+ end
224
+ else
225
+ if valid_json?(value)
226
+ p = "{\"name\":\"#{key}\",\"value\":#{value}}"
227
+ data["parameters"] = p
228
+ else
229
+ p = "{\"name\":\"#{key}\",\"value\":\"#{value}\"}"
230
+ data["parameters"] = p
231
+ end
232
+ end
233
+ end
234
+
235
+ Unirest.timeout(@timeoutVal)
236
+ response=Unirest.post endPoint,
237
+ headers:{ "Accept" => "application/json", "Content-Type" => "application/json"},
238
+ parameters:data
239
+
240
+ if response.code == 200
241
+ if callback != nil
242
+ callback.call(response.body)
243
+ else
244
+ return response.body
245
+ end
246
+ else
247
+ puts "Error: #{response.body}"
248
+ err = create_error_object(response.body["error"],response.body["reason"],response.body["detail"],"")
249
+ if callback != nil
250
+ callback.call(err)
251
+ else
252
+ return err
253
+ end
254
+ end
255
+ end
256
+
257
+ private
258
+ # internal utilitiy method
259
+ def valid_json?(json)
260
+ begin
261
+ JSON.parse(json)
262
+ return true
263
+ rescue JSON::ParserError => e
264
+ return false
265
+ end
266
+ end
267
+ def create_error_object(error,reason,detail,jobID)
268
+ errorObj = {}
269
+ errorObj["error"] = error
270
+ errorObj["reason"] = reason
271
+ errorObj["detail"] = detail
272
+ errorObj["jobID"] = jobID
273
+ return errorObj
274
+ end
275
+ end
@@ -0,0 +1,13 @@
1
+ class ErrorCode
2
+ TIMEOUT = 1600
3
+ IN_PROGRESS = 1610
4
+ QUEUED = 1620
5
+ NONSTANDARD_RESPONSE = 1630
6
+ INVALID_PARAM = 1640
7
+ INVALID_HOD_RESPONSE = 1650
8
+ UNKNOWN_ERROR = 1660
9
+ HTTP_ERROR = 1670
10
+ CONNECTION_ERROR = 1680
11
+ IO_ERROR = 1690
12
+ HOD_CLIENT_BUSY = 1700
13
+ end
@@ -0,0 +1,88 @@
1
+ require File.join(File.dirname(__FILE__), "/hoderrorcode.rb")
2
+
3
+ class HODResponseParser
4
+ @errors
5
+ def initialize()
6
+ @errors = []
7
+ end
8
+
9
+ def parse_jobid(response)
10
+ @errors = []
11
+ puts response
12
+ if response.has_key? "jobID" and response["jobID"].length > 0
13
+ return response["jobID"]
14
+ else
15
+ if response.has_key? "error" and response.has_key? "reason"
16
+ detail = ""
17
+ if response.has_key? "detail"
18
+ detail = response["detail"]
19
+ create_error_object(response["error"], response["reason"],detail,"")
20
+ end
21
+ else
22
+ create_error_object(ErrorCode::INVALID_HOD_RESPONSE, "Invalid HOD response","","")
23
+ end
24
+ return nil
25
+ end
26
+ end
27
+ #
28
+ def parse_payload(response)
29
+ @errors = []
30
+ if response.has_key? "actions"
31
+ actions = response["actions"]
32
+ status = actions[0]["status"]
33
+ if status == "queued"
34
+ create_error_object(ErrorCode::QUEUED, "Task is queued","", response["jobID"])
35
+ return nil
36
+ elsif status == "in progress"
37
+ create_error_object(ErrorCode::IN_PROGRESS, "Task is in progress","", response["jobID"])
38
+ return nil
39
+ elsif status == "failed"
40
+ errors = actions[0]["errors"]
41
+ if errors.kind_of?(Array)
42
+ errors.each { |error|
43
+ detail = ""
44
+ if error.has_key? "detail"
45
+ detail = error["detail"]
46
+ end
47
+ create_error_object(error["error"], error["reason"],detail,error["jobID"])
48
+ }
49
+ else
50
+ detail = ""
51
+ if errors.has_key? "detail"
52
+ detail = errors["detail"]
53
+ end
54
+ create_error_object(errors["error"], errors["reason"],detail,errors["jobID"])
55
+ end
56
+ return nil
57
+ else
58
+ return actions[0]["result"]
59
+ end
60
+ else
61
+ # must make sure this is an error message. Not just an error key from a good result
62
+ if response.has_key? "error" and response.has_key? "reason"
63
+ detail = ""
64
+ if response.has_key? "detail"
65
+ detail = response["detail"]
66
+ create_error_object(response["error"], response["reason"],detail,"")
67
+ return nil
68
+ end
69
+ else
70
+ return response
71
+ end
72
+ end
73
+ end
74
+
75
+ def get_last_errors()
76
+ return @errors
77
+ end
78
+ #
79
+ private
80
+ def create_error_object(error,reason,detail,jobID)
81
+ errorObj = {}
82
+ errorObj["error"] = error
83
+ errorObj["reason"] = reason
84
+ errorObj["detail"] = detail
85
+ errorObj["jobID"] = jobID
86
+ @errors.push(errorObj)
87
+ end
88
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: havenondemand
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phong Vu
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-12-06 00:00:00.000000000 Z
13
+ date: 2016-12-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: unirest
@@ -66,11 +66,21 @@ files:
66
66
  - LICENSE.txt
67
67
  - README.md
68
68
  - Rakefile
69
- - examples/examples.rb
70
- - examples/testhelper.rb
69
+ - examples/attendant_test.mp3
70
+ - examples/call_post_request_combination.rb
71
+ - examples/callback_async.rb
72
+ - examples/callback_sync.rb
73
+ - examples/post_file.rb
74
+ - examples/post_multiple_files.rb
75
+ - examples/returnresponse_async.rb
76
+ - examples/returnresponse_sync.rb
77
+ - examples/review.jpg
71
78
  - havenondemand.gemspec
72
79
  - lib/havenondemand.rb
73
80
  - lib/havenondemand/version.rb
81
+ - lib/hodclient.rb
82
+ - lib/hoderrorcode.rb
83
+ - lib/hodresponseparser.rb
74
84
  homepage: https://github.com/HP-Haven-OnDemand/havenondemand-ruby
75
85
  licenses:
76
86
  - MIT
@@ -1,67 +0,0 @@
1
- require_relative "../lib/havenondemand.rb"
2
-
3
- $apikey="yourapikey"
4
-
5
-
6
-
7
- def test_post
8
- client= HODClient.new("http://api.idolondemand.com",$apikey)
9
- r=client.post("querytextindex",{:text=>"hello",:absolute_max_result=>1000})
10
- puts "\n",r.json()["documents"][0]["reference"],"\n"
11
- end
12
-
13
-
14
- def test_post_async
15
- client= HODClient.new("http://api.idolondemand.com",$apikey)
16
- r=client.post("querytextindex",{:text=>"hello"},async=true)
17
- #returns jobid
18
- puts r.jobID
19
- # will return status of call, queued or finished
20
- puts r.status().json()
21
- # Will wait until result to return
22
- puts r.result().json()
23
- end
24
-
25
-
26
-
27
- def test_indexing(index="mytestindex")
28
- client= HODClient.new("http://api.idolondemand.com",$apikey)
29
- index=client.getIndex("myrssdb")
30
-
31
- doc={:title=>"title",:reference=>"ref",:content=>"content"}
32
- index.pushDoc(doc)
33
- puts index.commit().json()
34
- end
35
-
36
-
37
-
38
- def test_createIndex
39
- client= HODClient.new("http://api.idolondemand.com",$apikey)
40
- index=client.createIndex("mytestindex",flavor="explorer")
41
- puts index.json()
42
- end
43
-
44
-
45
-
46
- def test_deleteIndex
47
- client= HODClient.new("http://api.idolondemand.com",$apikey)
48
- puts client.deleteIndex("mytestindex")
49
- end
50
-
51
- def test_createConnector
52
- client= HODClient.new("http://api.idolondemand.com",$apikey)
53
- conn=HODConnector.new("mytestconnector",client)
54
- puts conn.create(type="web",config={ "url" => "http://www.idolondemand.com" })
55
- puts conn.delete()
56
- end
57
-
58
- def test_sentiment
59
-
60
- client= HODClient.new("http://api.idolondemand.com",$apikey)
61
- r=client.post('analyzesentiment',{'text'=>'I like cats'})
62
- puts r.json()
63
- end
64
-
65
- test_sentiment()
66
-
67
- #test_createIndex()