Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.

Commit 8db1b81

Browse files
author
Steve Herschleb
committed
fix some bugs, add tests, send POST data correctly, rename a couple of functions
1 parent 6ffa187 commit 8db1b81

File tree

2 files changed

+223
-41
lines changed

2 files changed

+223
-41
lines changed

alchemyapi.rb

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ class AlchemyAPI
5858
@@ENDPOINTS['language']['url'] = '/url/URLGetLanguage'
5959
@@ENDPOINTS['language']['text'] = '/text/TextGetLanguage'
6060
@@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'
6464
@@ENDPOINTS['text_raw'] = {}
6565
@@ENDPOINTS['text_raw']['url'] = '/url/URLGetRawText'
6666
@@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'
7070
@@ENDPOINTS['feeds'] = {}
7171
@@ENDPOINTS['feeds']['url'] = '/url/URLGetFeedLinks'
7272
@@ENDPOINTS['feeds']['html'] = '/html/HTMLGetFeedLinks'
@@ -86,7 +86,7 @@ def initialize()
8686
if key.empty?
8787
#The key file should't be blank
8888
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'
9090
Process.exit(1)
9191
end
9292

@@ -100,7 +100,7 @@ def initialize()
100100
rescue => err
101101
#The file doesn't exist, so show the message and create the file.
102102
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'
104104

105105
#create a blank file to hold the key
106106
File.open("api_key.txt", "w") {}
@@ -133,7 +133,7 @@ def sentiment(flavor, data, options = {})
133133
end
134134

135135
#Add the URL encoded data to the options and analyze
136-
options[flavor] = URI.escape(data)
136+
options[flavor] = data
137137
return analyze(@@ENDPOINTS['sentiment'][flavor], options)
138138
end
139139

@@ -154,8 +154,8 @@ def sentiment(flavor, data, options = {})
154154
# OUTPUT:
155155
# The response, already converted from JSON to a Ruby object.
156156
#
157-
def sentiment_targeted(flavor, data,target, options = {})
158-
unless targeted == ''
157+
def sentiment_targeted(flavor, data, target, options = {})
158+
if target == '' || target == nil
159159
return { 'status'=>'ERROR', 'statusMessage'=>'targeted sentiment requires a non-null target' }
160160
end
161161

@@ -164,7 +164,7 @@ def sentiment_targeted(flavor, data,target, options = {})
164164
end
165165

166166
#Add the URL encoded data and the target to the options and analyze
167-
options[flavor] = URI.escape(data)
167+
options[flavor] = data
168168
options['target'] = target
169169
return analyze(@@ENDPOINTS['sentiment_targeted'][flavor], options)
170170
end
@@ -197,7 +197,7 @@ def entities(flavor, data, options = {})
197197
end
198198

199199
#Add the URL encoded data to the options and analyze
200-
options[flavor] = URI.escape(data)
200+
options[flavor] = data
201201
return analyze(@@ENDPOINTS['entities'][flavor], options)
202202
end
203203

@@ -223,7 +223,7 @@ def author(flavor, data, options = {})
223223
end
224224

225225
#Add the URL encoded data to the options and analyze
226-
options[flavor] = URI.escape(data)
226+
options[flavor] = data
227227
return analyze(@@ENDPOINTS['author'][flavor], options)
228228
end
229229

@@ -252,7 +252,7 @@ def keywords(flavor, data, options = {})
252252
end
253253

254254
#Add the URL encoded data to the options and analyze
255-
options[flavor] = URI.escape(data)
255+
options[flavor] = data
256256
return analyze(@@ENDPOINTS['keywords'][flavor], options)
257257
end
258258

@@ -275,7 +275,7 @@ def concepts(flavor, data, options = {})
275275
end
276276

277277
#Add the URL encoded data to the options and analyze
278-
options[flavor] = URI.escape(data)
278+
options[flavor] = data
279279
return analyze(@@ENDPOINTS['concepts'][flavor], options)
280280
end
281281

@@ -301,7 +301,7 @@ def category(flavor, data, options = {})
301301
end
302302

303303
#Add the URL encoded data to the options and analyze
304-
options[flavor] = URI.escape(data)
304+
options[flavor] = data
305305
return analyze(@@ENDPOINTS['category'][flavor], options)
306306
end
307307

@@ -336,7 +336,7 @@ def relations(flavor, data, options = {})
336336
end
337337

338338
#Add the URL encoded data to the options and analyze
339-
options[flavor] = URI.escape(data)
339+
options[flavor] = data
340340
return analyze(@@ENDPOINTS['relations'][flavor], options)
341341
end
342342

@@ -362,7 +362,7 @@ def language(flavor, data, options = {})
362362
end
363363

364364
#Add the URL encoded data to the options and analyze
365-
options[flavor] = URI.escape(data)
365+
options[flavor] = data
366366
return analyze(@@ENDPOINTS['language'][flavor], options)
367367
end
368368

@@ -383,14 +383,14 @@ def language(flavor, data, options = {})
383383
# OUTPUT:
384384
# The response, already converted from JSON to a Ruby object.
385385
#
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)
388388
return { 'status'=>'ERROR', 'statusInfo'=>'clean text extraction for ' + flavor + ' not available' }
389389
end
390390

391391
#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)
394394
end
395395

396396

@@ -415,7 +415,7 @@ def text_raw(flavor, data, options = {})
415415
end
416416

417417
#Add the URL encoded data to the options and analyze
418-
options[flavor] = URI.escape(data)
418+
options[flavor] = data
419419
return analyze(@@ENDPOINTS['text_raw'][flavor], options)
420420
end
421421

@@ -435,14 +435,14 @@ def text_raw(flavor, data, options = {})
435435
# OUTPUT:
436436
# The response, already converted from JSON to a Ruby object.
437437
#
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)
440440
return { 'status'=>'ERROR', 'statusInfo'=>'title extraction for ' + flavor + ' not available' }
441441
end
442442

443443
#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)
446446
end
447447

448448

@@ -467,7 +467,7 @@ def microformats(flavor, data, options = {})
467467
end
468468

469469
#Add the URL encoded data to the options and analyze
470-
options[flavor] = URI.escape(data)
470+
options[flavor] = data
471471
return analyze(@@ENDPOINTS['microformats'][flavor], options)
472472
end
473473

@@ -493,7 +493,7 @@ def feeds(flavor, data, options = {})
493493
end
494494

495495
#Add the URL encoded data to the options and analyze
496-
options[flavor] = URI.escape(data)
496+
options[flavor] = data
497497
return analyze(@@ENDPOINTS['feeds'][flavor], options)
498498
end
499499

@@ -516,19 +516,13 @@ def analyze(url, options)
516516
url = @@BASE_URL + url
517517

518518
#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+
526522
#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)
531524

525+
#parse and return the response
532526
return JSON.parse(res.body)
533527
end
534528
end

0 commit comments

Comments
 (0)