@@ -58,15 +58,15 @@ class AlchemyAPI
58
58
@@ENDPOINTS [ 'language' ] [ 'url' ] = '/url/URLGetLanguage'
59
59
@@ENDPOINTS [ 'language' ] [ 'text' ] = '/text/TextGetLanguage'
60
60
@@ENDPOINTS [ 'language' ] [ 'html' ] = '/html/HTMLGetLanguage'
61
- @@ENDPOINTS [ 'text_clean ' ] = { }
62
- @@ENDPOINTS [ 'text_clean ' ] [ 'url' ] = '/url/URLGetText'
63
- @@ENDPOINTS [ 'text_clean ' ] [ 'html' ] = '/html/HTMLGetText'
61
+ @@ENDPOINTS [ 'text ' ] = { }
62
+ @@ENDPOINTS [ 'text ' ] [ 'url' ] = '/url/URLGetText'
63
+ @@ENDPOINTS [ 'text ' ] [ 'html' ] = '/html/HTMLGetText'
64
64
@@ENDPOINTS [ 'text_raw' ] = { }
65
65
@@ENDPOINTS [ 'text_raw' ] [ 'url' ] = '/url/URLGetRawText'
66
66
@@ENDPOINTS [ 'text_raw' ] [ 'html' ] = '/html/HTMLGetRawText'
67
- @@ENDPOINTS [ 'text_title ' ] = { }
68
- @@ENDPOINTS [ 'text_title ' ] [ 'url' ] = '/url/URLGetTitle'
69
- @@ENDPOINTS [ 'text_title ' ] [ 'html' ] = '/html/HTMLGetTitle'
67
+ @@ENDPOINTS [ 'title ' ] = { }
68
+ @@ENDPOINTS [ 'title ' ] [ 'url' ] = '/url/URLGetTitle'
69
+ @@ENDPOINTS [ 'title ' ] [ 'html' ] = '/html/HTMLGetTitle'
70
70
@@ENDPOINTS [ 'feeds' ] = { }
71
71
@@ENDPOINTS [ 'feeds' ] [ 'url' ] = '/url/URLGetFeedLinks'
72
72
@@ENDPOINTS [ 'feeds' ] [ 'html' ] = '/html/HTMLGetFeedLinks'
@@ -86,7 +86,7 @@ def initialize()
86
86
if key . empty?
87
87
#The key file should't be blank
88
88
puts 'The api_key.txt file appears to be blank, please copy/paste your API key in the file: api_key.txt'
89
- puts 'If you do not have an API Key from please register for one at: http://www.alchemyapi.com/api/register.html'
89
+ puts 'If you do not have an API Key from AlchemyAPI please register for one at: http://www.alchemyapi.com/api/register.html'
90
90
Process . exit ( 1 )
91
91
end
92
92
@@ -100,7 +100,7 @@ def initialize()
100
100
rescue => err
101
101
#The file doesn't exist, so show the message and create the file.
102
102
puts 'API Key not found! Please copy/paste your API key into the file: api_key.txt'
103
- puts 'If you do not have an API Key from please register for one at: http://www.alchemyapi.com/api/register.html'
103
+ puts 'If you do not have an API Key from AlchemyAPI please register for one at: http://www.alchemyapi.com/api/register.html'
104
104
105
105
#create a blank file to hold the key
106
106
File . open ( "api_key.txt" , "w" ) { }
@@ -133,7 +133,7 @@ def sentiment(flavor, data, options = {})
133
133
end
134
134
135
135
#Add the URL encoded data to the options and analyze
136
- options [ flavor ] = URI . escape ( data )
136
+ options [ flavor ] = data
137
137
return analyze ( @@ENDPOINTS [ 'sentiment' ] [ flavor ] , options )
138
138
end
139
139
@@ -154,8 +154,8 @@ def sentiment(flavor, data, options = {})
154
154
# OUTPUT:
155
155
# The response, already converted from JSON to a Ruby object.
156
156
#
157
- def sentiment_targeted ( flavor , data , target , options = { } )
158
- unless targeted == ''
157
+ def sentiment_targeted ( flavor , data , target , options = { } )
158
+ if target == '' || target == nil
159
159
return { 'status' => 'ERROR' , 'statusMessage' => 'targeted sentiment requires a non-null target' }
160
160
end
161
161
@@ -164,7 +164,7 @@ def sentiment_targeted(flavor, data,target, options = {})
164
164
end
165
165
166
166
#Add the URL encoded data and the target to the options and analyze
167
- options [ flavor ] = URI . escape ( data )
167
+ options [ flavor ] = data
168
168
options [ 'target' ] = target
169
169
return analyze ( @@ENDPOINTS [ 'sentiment_targeted' ] [ flavor ] , options )
170
170
end
@@ -197,7 +197,7 @@ def entities(flavor, data, options = {})
197
197
end
198
198
199
199
#Add the URL encoded data to the options and analyze
200
- options [ flavor ] = URI . escape ( data )
200
+ options [ flavor ] = data
201
201
return analyze ( @@ENDPOINTS [ 'entities' ] [ flavor ] , options )
202
202
end
203
203
@@ -223,7 +223,7 @@ def author(flavor, data, options = {})
223
223
end
224
224
225
225
#Add the URL encoded data to the options and analyze
226
- options [ flavor ] = URI . escape ( data )
226
+ options [ flavor ] = data
227
227
return analyze ( @@ENDPOINTS [ 'author' ] [ flavor ] , options )
228
228
end
229
229
@@ -252,7 +252,7 @@ def keywords(flavor, data, options = {})
252
252
end
253
253
254
254
#Add the URL encoded data to the options and analyze
255
- options [ flavor ] = URI . escape ( data )
255
+ options [ flavor ] = data
256
256
return analyze ( @@ENDPOINTS [ 'keywords' ] [ flavor ] , options )
257
257
end
258
258
@@ -275,7 +275,7 @@ def concepts(flavor, data, options = {})
275
275
end
276
276
277
277
#Add the URL encoded data to the options and analyze
278
- options [ flavor ] = URI . escape ( data )
278
+ options [ flavor ] = data
279
279
return analyze ( @@ENDPOINTS [ 'concepts' ] [ flavor ] , options )
280
280
end
281
281
@@ -301,7 +301,7 @@ def category(flavor, data, options = {})
301
301
end
302
302
303
303
#Add the URL encoded data to the options and analyze
304
- options [ flavor ] = URI . escape ( data )
304
+ options [ flavor ] = data
305
305
return analyze ( @@ENDPOINTS [ 'category' ] [ flavor ] , options )
306
306
end
307
307
@@ -336,7 +336,7 @@ def relations(flavor, data, options = {})
336
336
end
337
337
338
338
#Add the URL encoded data to the options and analyze
339
- options [ flavor ] = URI . escape ( data )
339
+ options [ flavor ] = data
340
340
return analyze ( @@ENDPOINTS [ 'relations' ] [ flavor ] , options )
341
341
end
342
342
@@ -362,7 +362,7 @@ def language(flavor, data, options = {})
362
362
end
363
363
364
364
#Add the URL encoded data to the options and analyze
365
- options [ flavor ] = URI . escape ( data )
365
+ options [ flavor ] = data
366
366
return analyze ( @@ENDPOINTS [ 'language' ] [ flavor ] , options )
367
367
end
368
368
@@ -383,14 +383,14 @@ def language(flavor, data, options = {})
383
383
# OUTPUT:
384
384
# The response, already converted from JSON to a Ruby object.
385
385
#
386
- def text_clean ( flavor , data , options = { } )
387
- unless @@ENDPOINTS [ 'text_clean ' ] . key? ( flavor )
386
+ def text ( flavor , data , options = { } )
387
+ unless @@ENDPOINTS [ 'text ' ] . key? ( flavor )
388
388
return { 'status' => 'ERROR' , 'statusInfo' => 'clean text extraction for ' + flavor + ' not available' }
389
389
end
390
390
391
391
#Add the URL encoded data to the options and analyze
392
- options [ flavor ] = URI . escape ( data )
393
- return analyze ( @@ENDPOINTS [ 'text_clean ' ] [ flavor ] , options )
392
+ options [ flavor ] = data
393
+ return analyze ( @@ENDPOINTS [ 'text ' ] [ flavor ] , options )
394
394
end
395
395
396
396
@@ -415,7 +415,7 @@ def text_raw(flavor, data, options = {})
415
415
end
416
416
417
417
#Add the URL encoded data to the options and analyze
418
- options [ flavor ] = URI . escape ( data )
418
+ options [ flavor ] = data
419
419
return analyze ( @@ENDPOINTS [ 'text_raw' ] [ flavor ] , options )
420
420
end
421
421
@@ -435,14 +435,14 @@ def text_raw(flavor, data, options = {})
435
435
# OUTPUT:
436
436
# The response, already converted from JSON to a Ruby object.
437
437
#
438
- def text_title ( flavor , data , options = { } )
439
- unless @@ENDPOINTS [ 'text_title ' ] . key? ( flavor )
438
+ def title ( flavor , data , options = { } )
439
+ unless @@ENDPOINTS [ 'title ' ] . key? ( flavor )
440
440
return { 'status' => 'ERROR' , 'statusInfo' => 'title extraction for ' + flavor + ' not available' }
441
441
end
442
442
443
443
#Add the URL encoded data to the options and analyze
444
- options [ flavor ] = URI . escape ( data )
445
- return analyze ( @@ENDPOINTS [ 'text_raw ' ] [ flavor ] , options )
444
+ options [ flavor ] = data
445
+ return analyze ( @@ENDPOINTS [ 'title ' ] [ flavor ] , options )
446
446
end
447
447
448
448
@@ -467,7 +467,7 @@ def microformats(flavor, data, options = {})
467
467
end
468
468
469
469
#Add the URL encoded data to the options and analyze
470
- options [ flavor ] = URI . escape ( data )
470
+ options [ flavor ] = data
471
471
return analyze ( @@ENDPOINTS [ 'microformats' ] [ flavor ] , options )
472
472
end
473
473
@@ -493,7 +493,7 @@ def feeds(flavor, data, options = {})
493
493
end
494
494
495
495
#Add the URL encoded data to the options and analyze
496
- options [ flavor ] = URI . escape ( data )
496
+ options [ flavor ] = data
497
497
return analyze ( @@ENDPOINTS [ 'feeds' ] [ flavor ] , options )
498
498
end
499
499
@@ -516,19 +516,13 @@ def analyze(url, options)
516
516
url = @@BASE_URL + url
517
517
518
518
#Add the API key and set the output mode to JSON
519
- url += '?apikey=' + @apiKey + '&outputMode=json'
520
-
521
- #Add the additional options
522
- options . each do | key , value |
523
- url += '&' + key + '=' + value . to_s ( )
524
- end
525
-
519
+ options [ 'apikey' ] = @apiKey
520
+ options [ 'outputMode' ] = 'json'
521
+
526
522
#Fire off the HTTP request
527
- url = URI . parse ( url )
528
- req = Net ::HTTP ::Post . new ( url . to_s )
529
- req [ 'Accept-Encoding' ] = "identity"
530
- res = Net ::HTTP . start ( url . host , url . port ) { |http | http . request ( req ) }
523
+ res = Net ::HTTP ::post_form ( URI . parse ( url ) , options )
531
524
525
+ #parse and return the response
532
526
return JSON . parse ( res . body )
533
527
end
534
528
end
0 commit comments