havenondemand 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ require_relative "../lib/havenondemand.rb"
2
+
3
+ $client = HODClient.new("YOUR_API_KEY", "v1")
4
+ $parser = HODResponseParser.new()
5
+
6
+ # call Haven OnDemand APIs using asynchronous mode
7
+
8
+ def syncCallback(response)
9
+ response = $parser.parse_payload(response)
10
+ if response != nil
11
+ puts response
12
+ else
13
+ errors = $parser.get_last_errors()
14
+ errors.each { |error|
15
+ eCode = error["error"]
16
+ if eCode == ErrorCode::QUEUED
17
+ jobID = error["jobID"]
18
+ sleep(2)
19
+ $client.get_job_status(jobID, method(:syncCallback))
20
+ elsif eCode == ErrorCode::IN_PROGRESS
21
+ jobID = error["jobID"]
22
+ sleep(5)
23
+ $client.get_job_status(jobID, method(:syncCallback))
24
+ else
25
+ puts eCode
26
+ puts error["detail"]
27
+ puts error["reason"]
28
+ end
29
+ }
30
+ end
31
+ end
32
+
33
+ def asyncCallback(response)
34
+ jobID = $parser.parse_jobid(response)
35
+ if jobID != nil
36
+ $client.get_job_status(jobID, method(:syncCallback))
37
+ else
38
+ errors = $parser.get_last_errors()
39
+ errors.each { |error|
40
+ eCode = error["error"]
41
+ puts eCode
42
+ puts error["detail"]
43
+ puts error["reason"]
44
+ }
45
+ end
46
+ end
47
+
48
+ params = {}
49
+ params["file"] = ["pathto/filename1.xxx","pathto/filename2.xxx"]
50
+ params["entity_type"] = ['people_eng','places_eng']
51
+ hodApp = 'extractentities'
52
+ $client.post_request(hodApp, params, true, method(:asyncCallback))
@@ -0,0 +1,23 @@
1
+ require_relative "../lib/havenondemand.rb"
2
+
3
+ $client = HODClient.new("YOUR_API_KEY", "v1")
4
+ $parser = HODResponseParser.new()
5
+
6
+
7
+ params = {}
8
+ params["text"] = "Haven OnDemand is awesome"
9
+ params["language"] = 'eng'
10
+ hodApp = 'analyzesentiment'
11
+ # get_request with return response in asynchronous mode
12
+ r = $client.get_request(hodApp, params, true, nil)
13
+ jobID = $parser.parse_jobid(r)
14
+ r = $client.get_job_result(jobID)
15
+
16
+ # post_request with return response in asynchronous mode
17
+ =begin
18
+ r = $client.post_request(hodApp, params, true, nil)
19
+ jobID = $parser.parse_jobid(r)
20
+ r = $client.get_job_result(jobID)
21
+ =end
22
+
23
+ puts r
@@ -0,0 +1,15 @@
1
+ require_relative "../lib/havenondemand.rb"
2
+
3
+ $client = HODClient.new("YOUR_API_KEY", "v1")
4
+
5
+ params = {}
6
+ params["text"] = "Haven OnDemand is awesome"
7
+ params["language"] = 'eng'
8
+ hodApp = 'analyzesentiment'
9
+ # get_request with return response in synchronous mode
10
+ r = $client.get_request(hodApp, params, false, nil)
11
+
12
+ # post_request with return response in synchronous mode
13
+ #r = $client.post_request(hodApp, params, false, nil)
14
+
15
+ puts r
Binary file
@@ -6,7 +6,7 @@ require 'havenondemand/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "havenondemand"
8
8
  spec.version = Havenondemand::VERSION
9
- spec.authors = ["Phong Vu","Tyler Nappy" ," Martin Zerbib"]
9
+ spec.authors = ["Phong Vu", "Tyler Nappy", " Martin Zerbib"]
10
10
  spec.email = ["phong.vu@hpe.com"]
11
11
  spec.summary = %q{Haven OnDemand Ruby Client}
12
12
  spec.description = %q{}
@@ -1,394 +1,2 @@
1
-
2
-
3
- require 'Unirest'
4
- require 'json'
5
- #require 'httpclient'
6
- #require 'byebug'
7
-
8
-
9
-
10
-
11
-
12
-
13
- class HODError < StandardError
14
-
15
- end
16
-
17
-
18
-
19
- class HODResponse
20
-
21
- attr_accessor :response
22
-
23
- def initialize(response)
24
- #@query=query
25
-
26
- @response=response
27
- end
28
-
29
- def json()
30
- @response.body
31
- end
32
-
33
- end
34
-
35
- class HODAsyncResponse < HODResponse
36
-
37
- attr_accessor :response
38
- attr_accessor :jobID
39
- def initialize(response,client)
40
- #@query=query
41
- @response=response
42
- @client=client
43
- @jobID =response.body["jobID"]
44
- end
45
-
46
- def status()
47
- @client.getStatus(@jobID)
48
- end
49
-
50
- def result()
51
- @client.getResult(@jobID)
52
- end
53
-
54
- end
55
-
56
-
57
-
58
- class HODClient
59
- @@version=1
60
- @@apidefault="v1"
61
- def initialize(apikey, version="v1")
62
- # Instance variables
63
- if apikey=='http://api.havenondemand.com' || apikey=='http://api.havenondemand.com/' || apikey=='https://api.havenondemand.com' || apikey=='https://api.havenondemand.com/'
64
- 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"
65
- end
66
- @@apidefault=version
67
- @url = "https://api.havenondemand.com"
68
- @apikey = apikey
69
-
70
- end
71
-
72
-
73
- def getStatus(jobID)
74
- data={"apikey"=>@apikey}
75
- response=Unirest.post "#{@url}/#{@@version}/job/status/#{jobID}",
76
- headers:{ "Accept" => "application/json" },
77
- parameters:data
78
- return HODResponse.new(response)
79
-
80
- end
81
-
82
- def getResult(jobID)
83
- data={"apikey"=>@apikey}
84
- response=Unirest.post "#{@url}/#{@@version}/job/result/#{jobID}",
85
- headers:{ "Accept" => "application/json" },
86
- parameters:data
87
- return HODResponse.new(response)
88
- end
89
-
90
- def deleteIndex(index)
91
- if index.class.name=="HODIndex"
92
- index=index.name
93
- end
94
-
95
- data=Hash.new
96
- data[:index]=index
97
-
98
- delete=post("deletetextindex",data)
99
- confirm=delete.json()["confirm"]
100
- data[:confirm]=confirm
101
- delete=post("deletetextindex",data)
102
- return delete
103
- end
104
-
105
- def deleteConnector(connector)
106
- data=connectorUtil(connector)
107
- delete=post("deleteconnector",data)
108
- return delete
109
- end
110
-
111
-
112
- def connectorUtil(connector)
113
- if index.class.name=="HODConnector"
114
- connector=connector.name
115
- end
116
- data=Hash.new
117
- data[:connector]=connector
118
- return data
119
- end
120
-
121
-
122
- def startConnector(connector)
123
- data=connectorUtil(connector)
124
- return post("startconnector",data)
125
- end
126
-
127
- def retrieveConnectorConfig(connector)
128
- data=connectorUtil(connector)
129
- return post("retrieveconfig",data)
130
- end
131
-
132
- def connectorstatus(connector)
133
- data=connectorUtil(connector)
134
- return post("connectorstatus",data)
135
- end
136
-
137
- def post_combination(combination, data=Hash.new, async=false)
138
- handler="executecombination"
139
- data[:apikey]=@apikey
140
- syncpath="sync"
141
- syncpath="async" if async
142
- data[:combination] = combination
143
- data[:parameters] = data[:parameters].to_json
144
- Unirest.timeout(30)
145
-
146
- response=Unirest.post "#{@url}/#{@@version}/api/#{syncpath}/#{handler}/#{@@apidefault}",
147
- headers:{ "Accept" => "application/json", "Content-Type" => "application/json"},
148
- parameters:data
149
- if response.code == 200
150
-
151
- if async
152
- return HODAsyncResponse.new(response,self)
153
- end
154
- return HODResponse.new(response)
155
- else
156
- puts response.body
157
- #puts data[:json].encoding.name
158
- raise HODError.new "Error #{response.body["error"]} -- #{response.body["reason"]}"
159
- end
160
-
161
- end
162
-
163
- def post(handler, data=Hash.new, async=false)
164
- data[:apikey]=@apikey
165
- syncpath="sync"
166
- if async
167
- syncpath="async"
168
- end
169
- Unirest.timeout(30)
170
- response=Unirest.post "#{@url}/#{@@version}/api/#{syncpath}/#{handler}/#{@@apidefault}",
171
- headers:{ "Accept" => "application/json", "Content-Type" => "application/json"},
172
- parameters:data
173
- if response.code == 200
174
-
175
- if async
176
- return HODAsyncResponse.new(response,self)
177
- end
178
- return HODResponse.new(response)
179
- else
180
- puts response.body
181
- #puts data[:json].encoding.name
182
- raise HODError.new "Error #{response.body["error"]} -- #{response.body["reason"]}"
183
- end
184
- end
185
-
186
-
187
-
188
- def getIndex(name)
189
- #indexes=self.listIndexes()
190
-
191
- index=HODIndex.new(name,client:self)
192
- #puts (index in indexes)
193
-
194
- end
195
-
196
-
197
-
198
- def createIndex(name,flavor="standard",parametric_fields=[],index_fields=[])
199
- data=Hash.new
200
- data[:index]=name
201
- data[:flavor]=flavor
202
- data[:parametric_fields]=parametric_fields
203
- data[:index_fields]=index_fields
204
- self.post("createtextindex",data)
205
- return HODIndex.new(name,client:self)
206
- end
207
-
208
- def listIndexes()
209
- r=post("listindex")
210
-
211
- indexes=r["index"].map { |index| HODIndex.new(index["index"],index["flavor"],index["type"],client:self)}
212
-
213
- end
214
-
215
-
216
- def addDoc(doc, index)
217
- self.addDocs([doc],index)
218
- end
219
-
220
- def addDocs(docs, index,async=false)
221
-
222
-
223
- # puts docs
224
- # puts docs.length
225
- #jsondocs= docs.map { |doc| doc.data}
226
- jsondocs=docs
227
- #puts jsondocs
228
- docs=Hash.new
229
- docs[:documents]=jsondocs
230
-
231
- #puts docs.to_json
232
- docs=docs.to_json
233
- puts docs.length
234
- #puts docs
235
- #docs=render :json => JSON::dump(docs)
236
- data={json:docs,index:index}
237
-
238
- return self.post("addtotextindex",data,async)
239
- end
240
-
241
- end
242
-
243
- class HODConnector
244
-
245
- attr_reader :client
246
- attr_reader :name
247
-
248
-
249
- def initialize(name,client=nil)
250
- @name=name
251
- @client=client
252
- end
253
-
254
- def create(type="web",config=Hash.new, destination="", schedule="",description="")
255
- config("addtotextindex",type,config,destination,schedule,description)
256
- end
257
-
258
-
259
- def update(type="web",config=Hash.new, destination="", schedule="",description="")
260
- config("addtotextindex",type,config,destination,schedule,description)
261
- end
262
-
263
- def config(method,type="",config="", destination="", schedule="",description="")
264
- data=Hash.new
265
- data[:connector]=@name
266
- if type!=""
267
- data[:type]=type
268
- end
269
- if config!=""
270
- data[:config]=JSON.dump config
271
- end
272
- if destination!=""
273
- destination={"action"=>"addtotextindex", "index" => destination }
274
- data[:destination]=JSON.dump destination
275
- end
276
- if schedule != ""
277
- data[:schedule]=JSON.dump schedule
278
- end
279
- data[:description]=description
280
- result=@client.post(method,data)
281
- puts result
282
- end
283
-
284
- def delete()
285
- @client.deleteConnector(@name)
286
- end
287
-
288
- def config()
289
- @client.retrieveConnectorConfig(@name)
290
- end
291
-
292
- def status()
293
- @client.connectorStatus(@name)
294
- end
295
-
296
- def ==(other_object)
297
- comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && @name == other_object.name)
298
- end
299
- end
300
-
301
-
302
-
303
-
304
-
305
- class HODIndex
306
-
307
- attr_reader :client
308
- attr_reader :name
309
-
310
-
311
- def initialize(name,flavor="standard",type="normal",client:nil)
312
- @name=name
313
-
314
- @client=client
315
- @docs=[]
316
- end
317
-
318
- def query(text,data=Hash.new)
319
- data[:database_match]=@name
320
-
321
- data[:text]=text
322
- result=@client.post("querytextindex",data)
323
- result["documents"].map!{|doc| HODDoc.new(doc) }
324
- return result
325
- end
326
- def size()
327
- return @docs.length
328
- end
329
- def pushDoc(doc)
330
- @docs.push(doc)
331
-
332
- end
333
-
334
- def commit(async=false)
335
- # docs={document:@docs}
336
- # data={json:docs.to_json,index:@name}
337
- # puts docs.to_json
338
-
339
- #r=@client.post("addtotextindex",data)
340
- #@docs=[]
341
- response=addDocs(@docs,async)
342
- @docs=[]
343
- return response
344
- end
345
-
346
- def addDoc(doc)
347
- docs=Hash.new
348
-
349
- # docs[:document]=[doc.data]
350
- # data={json:docs.to_json,index:@name}
351
- # @client.postasync("addtotextindex",data)
352
- puts @client.addDoc(doc,@name)
353
- end
354
-
355
-
356
- def addDocs(docs,async=false)
357
-
358
- # docs[:document]=[doc.data]
359
- # data={json:docs.to_json,index:@name}
360
- # @client.postasync("addtotextindex",data)
361
-
362
- return @client.addDocs(docs,@name,async)
363
- end
364
-
365
- def delete()
366
- clientcheck()
367
- @client.deleteIndex(self)
368
- end
369
-
370
- def ==(other_object)
371
- comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && @name == other_object.name)
372
- end
373
- end
374
-
375
-
376
-
377
- class HODDoc
378
-
379
- attr_accessor :data
380
- attr_accessor :sentiment
381
- attr_accessor :entities
382
- def initialize(data)
383
- #@query=query
384
- @entities=Hash.new
385
- @data=data
386
- end
387
-
388
- def to_json(options={})
389
- return render :json => JSON::dump(@data)
390
- end
391
-
392
-
393
-
394
- end
1
+ require File.join(File.dirname(__FILE__), "hodclient.rb")
2
+ require File.join(File.dirname(__FILE__), "hodresponseparser.rb")