alchemy-api-rb-gem 0.2
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 +7 -0
- data/dog.jpg +0 -0
- data/lib/alchemyapi.rb +652 -0
- data/tests/test_alchemyapi.rb +258 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f37f93f69daee2305f5deeb42dd884729d8d45cd
|
4
|
+
data.tar.gz: 40614dfad6f1c7155e034a644540b32950d446a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5dce6cfe12c8abc35569f7884f5920bed2e150f77fc745701516587a4e58caab588185f5adb23ceabbeab3fb1759fc284c6da8af4bb0bf75ee9015301db19212
|
7
|
+
data.tar.gz: 0369c4d6b359ea79ea94eecde89f34c55af0642d5bd059f03fd12ff49d80676aad59b9a53100b9b54ef30e40db727fe353f1f7198d3e9a0674bedde9a613a3be
|
data/dog.jpg
ADDED
Binary file
|
data/lib/alchemyapi.rb
ADDED
@@ -0,0 +1,652 @@
|
|
1
|
+
|
2
|
+
# Copyright 2013 AlchemyAPI
|
3
|
+
# Copyright 2014 Piero Dotti
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'rubygems'
|
18
|
+
require 'uri'
|
19
|
+
require 'json'
|
20
|
+
require 'faraday'
|
21
|
+
|
22
|
+
class AlchemyAPI
|
23
|
+
# Setup the endpoints
|
24
|
+
@@ENDPOINTS = {}
|
25
|
+
@@ENDPOINTS['sentiment'] = {}
|
26
|
+
@@ENDPOINTS['sentiment']['url'] = '/url/URLGetTextSentiment'
|
27
|
+
@@ENDPOINTS['sentiment']['text'] = '/text/TextGetTextSentiment'
|
28
|
+
@@ENDPOINTS['sentiment']['html'] = '/html/HTMLGetTextSentiment'
|
29
|
+
@@ENDPOINTS['sentiment_targeted'] = {}
|
30
|
+
@@ENDPOINTS['sentiment_targeted']['url'] = '/url/URLGetTargetedSentiment'
|
31
|
+
@@ENDPOINTS['sentiment_targeted']['text'] = '/text/TextGetTargetedSentiment'
|
32
|
+
@@ENDPOINTS['sentiment_targeted']['html'] = '/html/HTMLGetTargetedSentiment'
|
33
|
+
@@ENDPOINTS['author'] = {}
|
34
|
+
@@ENDPOINTS['author']['url'] = '/url/URLGetAuthor'
|
35
|
+
@@ENDPOINTS['author']['html'] = '/html/HTMLGetAuthor'
|
36
|
+
@@ENDPOINTS['keywords'] = {}
|
37
|
+
@@ENDPOINTS['keywords']['url'] = '/url/URLGetRankedKeywords'
|
38
|
+
@@ENDPOINTS['keywords']['text'] = '/text/TextGetRankedKeywords'
|
39
|
+
@@ENDPOINTS['keywords']['html'] = '/html/HTMLGetRankedKeywords'
|
40
|
+
@@ENDPOINTS['concepts'] = {}
|
41
|
+
@@ENDPOINTS['concepts']['url'] = '/url/URLGetRankedConcepts'
|
42
|
+
@@ENDPOINTS['concepts']['text'] = '/text/TextGetRankedConcepts'
|
43
|
+
@@ENDPOINTS['concepts']['html'] = '/html/HTMLGetRankedConcepts'
|
44
|
+
@@ENDPOINTS['entities'] = {}
|
45
|
+
@@ENDPOINTS['entities']['url'] = '/url/URLGetRankedNamedEntities'
|
46
|
+
@@ENDPOINTS['entities']['text'] = '/text/TextGetRankedNamedEntities'
|
47
|
+
@@ENDPOINTS['entities']['html'] = '/html/HTMLGetRankedNamedEntities'
|
48
|
+
@@ENDPOINTS['category'] = {}
|
49
|
+
@@ENDPOINTS['category']['url'] = '/url/URLGetCategory'
|
50
|
+
@@ENDPOINTS['category']['text'] = '/text/TextGetCategory'
|
51
|
+
@@ENDPOINTS['category']['html'] = '/html/HTMLGetCategory'
|
52
|
+
@@ENDPOINTS['relations'] = {}
|
53
|
+
@@ENDPOINTS['relations']['url'] = '/url/URLGetRelations'
|
54
|
+
@@ENDPOINTS['relations']['text'] = '/text/TextGetRelations'
|
55
|
+
@@ENDPOINTS['relations']['html'] = '/html/HTMLGetRelations'
|
56
|
+
@@ENDPOINTS['language'] = {}
|
57
|
+
@@ENDPOINTS['language']['url'] = '/url/URLGetLanguage'
|
58
|
+
@@ENDPOINTS['language']['text'] = '/text/TextGetLanguage'
|
59
|
+
@@ENDPOINTS['language']['html'] = '/html/HTMLGetLanguage'
|
60
|
+
@@ENDPOINTS['text'] = {}
|
61
|
+
@@ENDPOINTS['text']['url'] = '/url/URLGetText'
|
62
|
+
@@ENDPOINTS['text']['html'] = '/html/HTMLGetText'
|
63
|
+
@@ENDPOINTS['text_raw'] = {}
|
64
|
+
@@ENDPOINTS['text_raw']['url'] = '/url/URLGetRawText'
|
65
|
+
@@ENDPOINTS['text_raw']['html'] = '/html/HTMLGetRawText'
|
66
|
+
@@ENDPOINTS['title'] = {}
|
67
|
+
@@ENDPOINTS['title']['url'] = '/url/URLGetTitle'
|
68
|
+
@@ENDPOINTS['title']['html'] = '/html/HTMLGetTitle'
|
69
|
+
@@ENDPOINTS['feeds'] = {}
|
70
|
+
@@ENDPOINTS['feeds']['url'] = '/url/URLGetFeedLinks'
|
71
|
+
@@ENDPOINTS['feeds']['html'] = '/html/HTMLGetFeedLinks'
|
72
|
+
@@ENDPOINTS['microformats'] = {}
|
73
|
+
@@ENDPOINTS['microformats']['url'] = '/url/URLGetMicroformatData'
|
74
|
+
@@ENDPOINTS['microformats']['html'] = '/html/HTMLGetMicroformatData'
|
75
|
+
@@ENDPOINTS['taxonomy'] = {}
|
76
|
+
@@ENDPOINTS['taxonomy']['url'] = '/url/URLGetRankedTaxonomy'
|
77
|
+
@@ENDPOINTS['taxonomy']['text'] = '/text/TextGetRankedTaxonomy'
|
78
|
+
@@ENDPOINTS['taxonomy']['html'] = '/html/HTMLGetRankedTaxonomy'
|
79
|
+
@@ENDPOINTS['combined'] = {}
|
80
|
+
@@ENDPOINTS['combined']['url'] = '/url/URLGetCombinedData'
|
81
|
+
@@ENDPOINTS['combined']['text'] = '/text/TextGetCombinedData'
|
82
|
+
@@ENDPOINTS['image_extract'] = {}
|
83
|
+
@@ENDPOINTS['image_extract']['url'] = '/url/URLGetImage'
|
84
|
+
@@ENDPOINTS['image_tag'] = {}
|
85
|
+
@@ENDPOINTS['image_tag']['url'] = '/url/URLGetRankedImageKeywords'
|
86
|
+
@@ENDPOINTS['image_tag']['image'] = '/image/ImageGetRankedImageKeywords'
|
87
|
+
|
88
|
+
@@BASE_URL = 'http://access.alchemyapi.com/calls'
|
89
|
+
|
90
|
+
def initialize(key)
|
91
|
+
|
92
|
+
if key.is_a?(String) && key.size == 40
|
93
|
+
@apiKey = key
|
94
|
+
else
|
95
|
+
raise 'Invalid key for AlchemyAPI.'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Calculates the sentiment for text, a URL or HTML.
|
100
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/
|
101
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/
|
102
|
+
#
|
103
|
+
# INPUT:
|
104
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
105
|
+
# data -> the data to analyze, either the text, the url or html code.
|
106
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
107
|
+
#
|
108
|
+
# Available Options:
|
109
|
+
# showSourceText -> 0: disabled (default), 1: enabled
|
110
|
+
#
|
111
|
+
# OUTPUT:
|
112
|
+
# The response, already converted from JSON to a Ruby object.
|
113
|
+
#
|
114
|
+
def sentiment(flavor, data, options = {})
|
115
|
+
unless @@ENDPOINTS['sentiment'].key?(flavor)
|
116
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'sentiment analysis for ' + flavor + ' not available' }
|
117
|
+
end
|
118
|
+
|
119
|
+
# Add the URL encoded data to the options and analyze
|
120
|
+
options[flavor] = data
|
121
|
+
return analyze(@@ENDPOINTS['sentiment'][flavor], options)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Calculates the targeted sentiment for text, a URL or HTML.
|
125
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/
|
126
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/
|
127
|
+
#
|
128
|
+
# INPUT:
|
129
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
130
|
+
# data -> the data to analyze, either the text, the url or html code.
|
131
|
+
# target -> the word or phrase to run sentiment analysis on.
|
132
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
133
|
+
#
|
134
|
+
# Available Options:
|
135
|
+
# showSourceText -> 0: disabled, 1: enabled
|
136
|
+
#
|
137
|
+
# OUTPUT:
|
138
|
+
# The response, already converted from JSON to a Ruby object.
|
139
|
+
#
|
140
|
+
def sentiment_targeted(flavor, data, target, options = {})
|
141
|
+
if target == '' || target.nil?
|
142
|
+
return { 'status' => 'ERROR', 'statusMessage' => 'targeted sentiment requires a non-null target' }
|
143
|
+
end
|
144
|
+
|
145
|
+
unless @@ENDPOINTS['sentiment_targeted'].key?(flavor)
|
146
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'targeted sentiment analysis for ' + flavor + ' not available' }
|
147
|
+
end
|
148
|
+
|
149
|
+
# Add the URL encoded data and the target to the options and analyze
|
150
|
+
options[flavor] = data
|
151
|
+
options['target'] = target
|
152
|
+
return analyze(@@ENDPOINTS['sentiment_targeted'][flavor], options)
|
153
|
+
end
|
154
|
+
|
155
|
+
# Extracts the entities for text, a URL or HTML.
|
156
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/entity-extraction/
|
157
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/entity-extraction/
|
158
|
+
#
|
159
|
+
# INPUT:
|
160
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
161
|
+
# data -> the data to analyze, either the text, the url or html code.
|
162
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
163
|
+
#
|
164
|
+
# Available Options:
|
165
|
+
# disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default)
|
166
|
+
# linkedData -> include linked data on disambiguated entities. 0: disabled, 1: enabled (default)
|
167
|
+
# coreference -> resolve coreferences (i.e. the pronouns that correspond to named entities). 0: disabled, 1: enabled (default)
|
168
|
+
# quotations -> extract quotations by entities. 0: disabled (default), 1: enabled.
|
169
|
+
# sentiment -> analyze sentiment for each entity. 0: disabled (default), 1: enabled. Requires 1 additional API transction if enabled.
|
170
|
+
# showSourceText -> 0: disabled (default), 1: enabled
|
171
|
+
# maxRetrieve -> the maximum number of entities to retrieve (default: 50)
|
172
|
+
#
|
173
|
+
# OUTPUT:
|
174
|
+
# The response, already converted from JSON to a Ruby object.
|
175
|
+
#
|
176
|
+
def entities(flavor, data, options = {})
|
177
|
+
unless @@ENDPOINTS['entities'].key?(flavor)
|
178
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'entity extraction for ' + flavor + ' not available' }
|
179
|
+
end
|
180
|
+
|
181
|
+
# Add the URL encoded data to the options and analyze
|
182
|
+
options[flavor] = data
|
183
|
+
return analyze(@@ENDPOINTS['entities'][flavor], options)
|
184
|
+
end
|
185
|
+
|
186
|
+
# Extracts the author from a URL or HTML.
|
187
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/author-extraction/
|
188
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/author-extraction/
|
189
|
+
#
|
190
|
+
# INPUT:
|
191
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
192
|
+
# data -> the data to analyze, either the text, the url or html code.
|
193
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
194
|
+
#
|
195
|
+
# Available Options:
|
196
|
+
# none
|
197
|
+
#
|
198
|
+
# OUTPUT:
|
199
|
+
# The response, already converted from JSON to a Ruby object.
|
200
|
+
#
|
201
|
+
def author(flavor, data, options = {})
|
202
|
+
unless @@ENDPOINTS['author'].key?(flavor)
|
203
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'author extraction for ' + flavor + ' not available' }
|
204
|
+
end
|
205
|
+
|
206
|
+
# Add the URL encoded data to the options and analyze
|
207
|
+
options[flavor] = data
|
208
|
+
return analyze(@@ENDPOINTS['author'][flavor], options)
|
209
|
+
end
|
210
|
+
|
211
|
+
# Extracts the keywords from text, a URL or HTML.
|
212
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/keyword-extraction/
|
213
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/keyword-extraction/
|
214
|
+
#
|
215
|
+
# INPUT:
|
216
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
217
|
+
# data -> the data to analyze, either the text, the url or html code.
|
218
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
219
|
+
#
|
220
|
+
# Available Options:
|
221
|
+
# keywordExtractMode -> normal (default), strict
|
222
|
+
# sentiment -> analyze sentiment for each keyword. 0: disabled (default), 1: enabled. Requires 1 additional API transaction if enabled.
|
223
|
+
# showSourceText -> 0: disabled (default), 1: enabled.
|
224
|
+
# maxRetrieve -> the max number of keywords returned (default: 50)
|
225
|
+
#
|
226
|
+
# OUTPUT:
|
227
|
+
# The response, already converted from JSON to a Ruby object.
|
228
|
+
#
|
229
|
+
def keywords(flavor, data, options = {})
|
230
|
+
unless @@ENDPOINTS['keywords'].key?(flavor)
|
231
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'keyword extraction for ' + flavor + ' not available' }
|
232
|
+
end
|
233
|
+
|
234
|
+
# Add the URL encoded data to the options and analyze
|
235
|
+
options[flavor] = data
|
236
|
+
return analyze(@@ENDPOINTS['keywords'][flavor], options)
|
237
|
+
end
|
238
|
+
|
239
|
+
# Tags the concepts for text, a URL or HTML.
|
240
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/concept-tagging/
|
241
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/concept-tagging/
|
242
|
+
#
|
243
|
+
# Available Options:
|
244
|
+
# maxRetrieve -> the maximum number of concepts to retrieve (default: 8)
|
245
|
+
# linkedData -> include linked data, 0: disabled, 1: enabled (default)
|
246
|
+
# showSourceText -> 0:disabled (default), 1: enabled
|
247
|
+
#
|
248
|
+
# OUTPUT:
|
249
|
+
# The response, already converted from JSON to a Ruby object.
|
250
|
+
#
|
251
|
+
def concepts(flavor, data, options = {})
|
252
|
+
unless @@ENDPOINTS['concepts'].key?(flavor)
|
253
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'concept tagging for ' + flavor + ' not available' }
|
254
|
+
end
|
255
|
+
|
256
|
+
# Add the URL encoded data to the options and analyze
|
257
|
+
options[flavor] = data
|
258
|
+
return analyze(@@ENDPOINTS['concepts'][flavor], options)
|
259
|
+
end
|
260
|
+
|
261
|
+
# Categorizes the text for text, a URL or HTML.
|
262
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/text-categorization/
|
263
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/text-categorization/
|
264
|
+
#
|
265
|
+
# INPUT:
|
266
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
267
|
+
# data -> the data to analyze, either the text, the url or html code.
|
268
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
269
|
+
#
|
270
|
+
# Available Options:
|
271
|
+
# showSourceText -> 0: disabled (default), 1: enabled
|
272
|
+
#
|
273
|
+
# OUTPUT:
|
274
|
+
# The response, already converted from JSON to a Ruby object.
|
275
|
+
#
|
276
|
+
def category(flavor, data, options = {})
|
277
|
+
unless @@ENDPOINTS['category'].key?(flavor)
|
278
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'text categorization for ' + flavor + ' not available' }
|
279
|
+
end
|
280
|
+
|
281
|
+
# Add the URL encoded data to the options and analyze
|
282
|
+
options[flavor] = data
|
283
|
+
return analyze(@@ENDPOINTS['category'][flavor], options)
|
284
|
+
end
|
285
|
+
|
286
|
+
# Extracts the relations for text, a URL or HTML.
|
287
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/relation-extraction/
|
288
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/relation-extraction/
|
289
|
+
#
|
290
|
+
# INPUT:
|
291
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
292
|
+
# data -> the data to analyze, either the text, the url or html code.
|
293
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
294
|
+
#
|
295
|
+
# Available Options:
|
296
|
+
# sentiment -> 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled.
|
297
|
+
# keywords -> extract keywords from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled.
|
298
|
+
# entities -> extract entities from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled.
|
299
|
+
# requireEntities -> only extract relations that have entities. 0: disabled (default), 1: enabled.
|
300
|
+
# sentimentExcludeEntities -> exclude full entity name in sentiment analysis. 0: disabled, 1: enabled (default)
|
301
|
+
# disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default)
|
302
|
+
# linkedData -> include linked data with disambiguated entities. 0: disabled, 1: enabled (default).
|
303
|
+
# coreference -> resolve entity coreferences. 0: disabled, 1: enabled (default)
|
304
|
+
# showSourceText -> 0: disabled (default), 1: enabled.
|
305
|
+
# maxRetrieve -> the maximum number of relations to extract (default: 50, max: 100)
|
306
|
+
#
|
307
|
+
# OUTPUT:
|
308
|
+
# The response, already converted from JSON to a Ruby object.
|
309
|
+
#
|
310
|
+
def relations(flavor, data, options = {})
|
311
|
+
unless @@ENDPOINTS['relations'].key?(flavor)
|
312
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'relation extraction for ' + flavor + ' not available' }
|
313
|
+
end
|
314
|
+
|
315
|
+
# Add the URL encoded data to the options and analyze
|
316
|
+
options[flavor] = data
|
317
|
+
return analyze(@@ENDPOINTS['relations'][flavor], options)
|
318
|
+
end
|
319
|
+
|
320
|
+
# Detects the language for text, a URL or HTML.
|
321
|
+
# For an overview, please refer to: http://www.alchemyapi.com/api/language-detection/
|
322
|
+
# For the docs, please refer to: http://www.alchemyapi.com/products/features/language-detection/
|
323
|
+
#
|
324
|
+
# INPUT:
|
325
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
326
|
+
# data -> the data to analyze, either the text, the url or html code.
|
327
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
328
|
+
#
|
329
|
+
# Available Options:
|
330
|
+
# none
|
331
|
+
#
|
332
|
+
# OUTPUT:
|
333
|
+
# The response, already converted from JSON to a Ruby object.
|
334
|
+
#
|
335
|
+
def language(flavor, data, options = {})
|
336
|
+
unless @@ENDPOINTS['language'].key?(flavor)
|
337
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'language detection for ' + flavor + ' not available' }
|
338
|
+
end
|
339
|
+
|
340
|
+
# Add the URL encoded data to the options and analyze
|
341
|
+
options[flavor] = data
|
342
|
+
return analyze(@@ENDPOINTS['language'][flavor], options)
|
343
|
+
end
|
344
|
+
|
345
|
+
# Extracts the cleaned text (removes ads, navigation, etc.) for text, a URL or HTML.
|
346
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/
|
347
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/
|
348
|
+
#
|
349
|
+
# INPUT:
|
350
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
351
|
+
# data -> the data to analyze, either the text, the url or html code.
|
352
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
353
|
+
#
|
354
|
+
# Available Options:
|
355
|
+
# useMetadata -> utilize meta description data, 0: disabled, 1: enabled (default)
|
356
|
+
# extractLinks -> include links, 0: disabled (default), 1: enabled.
|
357
|
+
#
|
358
|
+
# OUTPUT:
|
359
|
+
# The response, already converted from JSON to a Ruby object.
|
360
|
+
#
|
361
|
+
def text(flavor, data, options = {})
|
362
|
+
unless @@ENDPOINTS['text'].key?(flavor)
|
363
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'clean text extraction for ' + flavor + ' not available' }
|
364
|
+
end
|
365
|
+
|
366
|
+
# Add the URL encoded data to the options and analyze
|
367
|
+
options[flavor] = data
|
368
|
+
return analyze(@@ENDPOINTS['text'][flavor], options)
|
369
|
+
end
|
370
|
+
|
371
|
+
# Extracts the raw text (includes ads, navigation, etc.) for a URL or HTML.
|
372
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/
|
373
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/
|
374
|
+
#
|
375
|
+
# INPUT:
|
376
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
377
|
+
# data -> the data to analyze, either the text, the url or html code.
|
378
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
379
|
+
#
|
380
|
+
# Available Options:
|
381
|
+
# none
|
382
|
+
#
|
383
|
+
# OUTPUT:
|
384
|
+
# The response, already converted from JSON to a Ruby object.
|
385
|
+
#
|
386
|
+
def text_raw(flavor, data, options = {})
|
387
|
+
unless @@ENDPOINTS['text_raw'].key?(flavor)
|
388
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'raw text extraction for ' + flavor + ' not available' }
|
389
|
+
end
|
390
|
+
|
391
|
+
# Add the URL encoded data to the options and analyze
|
392
|
+
options[flavor] = data
|
393
|
+
return analyze(@@ENDPOINTS['text_raw'][flavor], options)
|
394
|
+
end
|
395
|
+
|
396
|
+
# Extracts the title for a URL or HTML.
|
397
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/
|
398
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/
|
399
|
+
#
|
400
|
+
# INPUT:
|
401
|
+
# flavor -> which version of the call, i.e. text, url or html.
|
402
|
+
# data -> the data to analyze, either the text, the url or html code.
|
403
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
404
|
+
#
|
405
|
+
# Available Options:
|
406
|
+
# useMetadata -> utilize title info embedded in meta data, 0: disabled, 1: enabled (default)
|
407
|
+
|
408
|
+
# OUTPUT:
|
409
|
+
# The response, already converted from JSON to a Ruby object.
|
410
|
+
#
|
411
|
+
def title(flavor, data, options = {})
|
412
|
+
unless @@ENDPOINTS['title'].key?(flavor)
|
413
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'title extraction for ' + flavor + ' not available' }
|
414
|
+
end
|
415
|
+
|
416
|
+
# Add the URL encoded data to the options and analyze
|
417
|
+
options[flavor] = data
|
418
|
+
return analyze(@@ENDPOINTS['title'][flavor], options)
|
419
|
+
end
|
420
|
+
|
421
|
+
# Parses the microformats for a URL or HTML.
|
422
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/microformats-parsing/
|
423
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/microformats-parsing/
|
424
|
+
#
|
425
|
+
# INPUT:
|
426
|
+
# flavor -> which version of the call, i.e. url or html.
|
427
|
+
# data -> the data to analyze, either the the url or html code.
|
428
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
429
|
+
#
|
430
|
+
# Available Options:
|
431
|
+
# none
|
432
|
+
#
|
433
|
+
# OUTPUT:
|
434
|
+
# The response, already converted from JSON to a Ruby object.
|
435
|
+
#
|
436
|
+
def microformats(flavor, data, options = {})
|
437
|
+
unless @@ENDPOINTS['microformats'].key?(flavor)
|
438
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'microformats parsing for ' + flavor + ' not available' }
|
439
|
+
end
|
440
|
+
|
441
|
+
# Add the URL encoded data to the options and analyze
|
442
|
+
options[flavor] = data
|
443
|
+
return analyze(@@ENDPOINTS['microformats'][flavor], options)
|
444
|
+
end
|
445
|
+
|
446
|
+
# Detects the RSS/ATOM feeds for a URL or HTML.
|
447
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/feed-detection/
|
448
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/feed-detection/
|
449
|
+
#
|
450
|
+
# INPUT:
|
451
|
+
# flavor -> which version of the call, i.e. url or html.
|
452
|
+
# data -> the data to analyze, either the the url or html code.
|
453
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
454
|
+
#
|
455
|
+
# Available Options:
|
456
|
+
# none
|
457
|
+
#
|
458
|
+
# OUTPUT:
|
459
|
+
# The response, already converted from JSON to a Ruby object.
|
460
|
+
#
|
461
|
+
def feeds(flavor, data, options = {})
|
462
|
+
unless @@ENDPOINTS['feeds'].key?(flavor)
|
463
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'feed detection for ' + flavor + ' not available' }
|
464
|
+
end
|
465
|
+
|
466
|
+
# Add the URL encoded data to the options and analyze
|
467
|
+
options[flavor] = data
|
468
|
+
return analyze(@@ENDPOINTS['feeds'][flavor], options)
|
469
|
+
end
|
470
|
+
|
471
|
+
# Categorizes the text for a URL, text or HTML.
|
472
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/text-categorization/
|
473
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/taxonomy/
|
474
|
+
#
|
475
|
+
# INPUT:
|
476
|
+
# flavor -> which version of the call, i.e. url, text or html.
|
477
|
+
# data -> the data to analyze, either the the url, text or html code.
|
478
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
479
|
+
#
|
480
|
+
# Available Options:
|
481
|
+
# showSourceText -> 0: disabled (default), 1: enabled.
|
482
|
+
#
|
483
|
+
# OUTPUT:
|
484
|
+
# The response, already converted from JSON to a Ruby object.
|
485
|
+
#
|
486
|
+
def taxonomy(flavor, data, options = {})
|
487
|
+
unless @@ENDPOINTS['taxonomy'].key?(flavor)
|
488
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'Taxonomy info for ' + flavor + ' not available' }
|
489
|
+
end
|
490
|
+
|
491
|
+
# Add the URL encoded data to the options and analyze
|
492
|
+
options[flavor] = data
|
493
|
+
return analyze(@@ENDPOINTS['taxonomy'][flavor], options)
|
494
|
+
end
|
495
|
+
|
496
|
+
# Combined call (see options below for available extractions) for a URL or text.
|
497
|
+
#
|
498
|
+
# INPUT:
|
499
|
+
# flavor -> which version of the call, i.e. url or text.
|
500
|
+
# data -> the data to analyze, either the the url or text.
|
501
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
502
|
+
#
|
503
|
+
# Available Options:
|
504
|
+
# extract -> VALUE,VALUE,VALUE,... (possible VALUEs: page-image,entity,keyword,title,author,taxonomy,concept,relation,doc-sentiment)
|
505
|
+
# extractMode -> (only applies when 'page-image' VALUE passed to 'extract' option)
|
506
|
+
# trust-metadata: less CPU-intensive, less accurate
|
507
|
+
# always-infer: more CPU-intensive, more accurate
|
508
|
+
# disambiguate -> whether to disambiguate detected entities, 0: disabled, 1: enabled (default)
|
509
|
+
# linkedData -> whether to include Linked Data content links with disambiguated entities, 0: disabled, 1: enabled (default). disambiguate must be enabled to use this.
|
510
|
+
# coreference -> whether to he/she/etc coreferences into detected entities, 0: disabled, 1: enabled (default)
|
511
|
+
# quotations -> whether to enable quotations extraction, 0: disabled (default), 1: enabled
|
512
|
+
# sentiment -> whether to enable entity-level sentiment analysis, 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled.
|
513
|
+
# showSourceText -> 0: disabled (default), 1: enabled.
|
514
|
+
# maxRetrieve -> maximum number of named entities to extract (default: 50)
|
515
|
+
#
|
516
|
+
# OUTPUT:
|
517
|
+
# The response, already converted from JSON to a Ruby object.
|
518
|
+
#
|
519
|
+
def combined(flavor, data, options = {})
|
520
|
+
unless @@ENDPOINTS['combined'].key?(flavor)
|
521
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'Combined data for ' + flavor + ' not available' }
|
522
|
+
end
|
523
|
+
|
524
|
+
# Add the URL encoded data to the options and analyze
|
525
|
+
options[flavor] = data
|
526
|
+
return analyze(@@ENDPOINTS['combined'][flavor], options)
|
527
|
+
end
|
528
|
+
|
529
|
+
# Extract image from a URL.
|
530
|
+
#
|
531
|
+
# INPUT:
|
532
|
+
# flavor -> which version of the call, i.e. url.
|
533
|
+
# data -> the data to analyze, i.e. the url.
|
534
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
535
|
+
#
|
536
|
+
# Available Options:
|
537
|
+
# extractMode -> trust-metadata: less CPU-intensive and less accurate, always-infer: more CPU-intensive and more accurate
|
538
|
+
#
|
539
|
+
# OUTPUT:
|
540
|
+
# The response, already converted from JSON to a Ruby object.
|
541
|
+
#
|
542
|
+
def image_extract(flavor, data, options = {})
|
543
|
+
unless @@ENDPOINTS['image_extract'].key?(flavor)
|
544
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'Image for ' + flavor + ' not available' }
|
545
|
+
end
|
546
|
+
|
547
|
+
# Add the URL encoded data to the options and analyze
|
548
|
+
options[flavor] = data
|
549
|
+
return analyze(@@ENDPOINTS['image_extract'][flavor], options)
|
550
|
+
end
|
551
|
+
|
552
|
+
# Tag image from a URL or raw image data.
|
553
|
+
# For an overview, please refer to: http://www.alchemyapi.com/products/features/image-tagging/
|
554
|
+
# For the docs, please refer to: http://www.alchemyapi.com/api/image-tagging/
|
555
|
+
#
|
556
|
+
# INPUT:
|
557
|
+
# flavor -> which version of the call, i.e. url or image.
|
558
|
+
# data -> the data to analyze, the url
|
559
|
+
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
|
560
|
+
#
|
561
|
+
# Available Options:
|
562
|
+
# extractMode -> trust-metadata: less CPU-intensive and less accurate, always-infer: more CPU-intensive and more accurate
|
563
|
+
# (image flavor only)
|
564
|
+
# imagePostMode -> how you will post the image
|
565
|
+
# raw: pass an unencoded image file using POST
|
566
|
+
#
|
567
|
+
# OUTPUT:
|
568
|
+
# The response, already converted from JSON to a Ruby object.
|
569
|
+
#
|
570
|
+
def image_tag(flavor, data, options = {}, image = '')
|
571
|
+
unless @@ENDPOINTS['image_tag'].key?(flavor)
|
572
|
+
return { 'status' => 'ERROR', 'statusInfo' => 'Image tagging for ' + flavor + ' not available' }
|
573
|
+
end
|
574
|
+
|
575
|
+
# Add the URL encoded data to the options and analyze
|
576
|
+
unless data.empty?
|
577
|
+
options[flavor] = data
|
578
|
+
end
|
579
|
+
return analyze_image(@@ENDPOINTS['image_tag'][flavor], options, image)
|
580
|
+
end
|
581
|
+
|
582
|
+
private
|
583
|
+
|
584
|
+
# HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface.
|
585
|
+
# It makes the call, then converts the returned JSON string into a Ruby object.
|
586
|
+
#
|
587
|
+
# INPUT:
|
588
|
+
# url -> the full URI encoded url
|
589
|
+
#
|
590
|
+
# OUTPUT:
|
591
|
+
# The response, already converted from JSON to a Ruby object.
|
592
|
+
#
|
593
|
+
def analyze(url, options)
|
594
|
+
# Insert the base URL
|
595
|
+
url = @@BASE_URL + url
|
596
|
+
|
597
|
+
# Add the API key and set the output mode to JSON
|
598
|
+
options['apikey'] = @apiKey
|
599
|
+
options['outputMode'] = 'json'
|
600
|
+
|
601
|
+
uri = URI.parse(url)
|
602
|
+
|
603
|
+
# Force Faraday using excon, to avoid Zlib::BufError: buffer error
|
604
|
+
Faraday.default_adapter = :excon
|
605
|
+
|
606
|
+
request = Faraday.new(url: "#{uri.scheme}://#{uri.host}")
|
607
|
+
|
608
|
+
# Fire off the HTTP request
|
609
|
+
res = request.post uri.request_uri, options
|
610
|
+
|
611
|
+
# parse and return the response
|
612
|
+
return JSON.parse(res.body)
|
613
|
+
end
|
614
|
+
|
615
|
+
# HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface.
|
616
|
+
# It makes the call, then converts the returned JSON string into a Ruby object.
|
617
|
+
#
|
618
|
+
# INPUT:
|
619
|
+
# url -> the full URI encoded url
|
620
|
+
# body -> the raw binary image data
|
621
|
+
#
|
622
|
+
# OUTPUT:
|
623
|
+
# The response, already converted from JSON to a Ruby object.
|
624
|
+
#
|
625
|
+
def analyze_image(url, options, body)
|
626
|
+
# Insert the base URL
|
627
|
+
url = @@BASE_URL + url
|
628
|
+
|
629
|
+
# Add the API key and set the output mode to JSON
|
630
|
+
options['apikey'] = @apiKey
|
631
|
+
options['outputMode'] = 'json'
|
632
|
+
|
633
|
+
url += '?'
|
634
|
+
options.each { |h, v|
|
635
|
+
url += h + '=' + v + '&'
|
636
|
+
}
|
637
|
+
|
638
|
+
# Parse URL
|
639
|
+
uri = URI.parse(url)
|
640
|
+
|
641
|
+
# Force Faraday using excon, to avoid Zlib::BufError: buffer error
|
642
|
+
Faraday.default_adapter = :excon
|
643
|
+
|
644
|
+
request = Faraday.new(url: "#{uri.scheme}://#{uri.host}")
|
645
|
+
|
646
|
+
# Fire off the HTTP request
|
647
|
+
res = request.post uri.request_uri, body.to_s
|
648
|
+
|
649
|
+
# parse and return the response
|
650
|
+
return JSON.parse(res.body)
|
651
|
+
end
|
652
|
+
end
|
@@ -0,0 +1,258 @@
|
|
1
|
+
|
2
|
+
# Copyright 2013 AlchemyAPI
|
3
|
+
# Copyright 2014 Piero Dotti
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'alchemyapi'
|
18
|
+
|
19
|
+
if RUBY_VERSION < '2.0'
|
20
|
+
require 'test/unit'
|
21
|
+
class MyTest < Test::Unit::TestCase
|
22
|
+
end
|
23
|
+
else
|
24
|
+
require 'minitest/autorun'
|
25
|
+
class MyTest < MiniTest::Test
|
26
|
+
end
|
27
|
+
end
|
28
|
+
class AlchemyAPITest < MyTest
|
29
|
+
@@alchemyapi = AlchemyAPI.new(IO.read('alchemy.key'))
|
30
|
+
@@test_text = 'Bob broke my heart, and then made up this silly sentence to test the Ruby SDK'
|
31
|
+
@@test_html = '<html><head><title>The best SDK Test | AlchemyAPI</title></head><body><h1>Hello World!</h1><p>My favorite language is Ruby</p></body></html>'
|
32
|
+
@@test_url = 'http://www.nytimes.com/2013/07/13/us/politics/a-day-of-friction-notable-even-for-a-fractious-congress.html?_r=0'
|
33
|
+
@@test_image = File.binread('dog.jpg')
|
34
|
+
|
35
|
+
def test_entities
|
36
|
+
puts 'Checking entities . . . '
|
37
|
+
response = @@alchemyapi.entities('text', @@test_text)
|
38
|
+
assert_equal response['status'], 'OK'
|
39
|
+
response = @@alchemyapi.entities('html', @@test_html)
|
40
|
+
assert_equal response['status'], 'OK'
|
41
|
+
response = @@alchemyapi.entities('url', @@test_url)
|
42
|
+
assert_equal response['status'], 'OK'
|
43
|
+
response = @@alchemyapi.entities('random', @@test_text)
|
44
|
+
assert_equal response['status'], 'ERROR' # invalid flavor
|
45
|
+
puts 'Entity tests complete'
|
46
|
+
puts ''
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_keywords
|
50
|
+
puts 'Checking keywords . . . '
|
51
|
+
response = @@alchemyapi.keywords('text', @@test_text)
|
52
|
+
assert_equal response['status'], 'OK'
|
53
|
+
response = @@alchemyapi.keywords('html', @@test_html)
|
54
|
+
assert_equal response['status'], 'OK'
|
55
|
+
response = @@alchemyapi.keywords('url', @@test_url)
|
56
|
+
assert_equal response['status'], 'OK'
|
57
|
+
response = @@alchemyapi.keywords('random', @@test_text)
|
58
|
+
assert_equal response['status'], 'ERROR' # invalid flavor
|
59
|
+
puts 'Keyword tests complete'
|
60
|
+
puts ''
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_concepts
|
64
|
+
puts 'Checking concepts . . . '
|
65
|
+
response = @@alchemyapi.concepts('text', @@test_text)
|
66
|
+
assert_equal response['status'], 'OK'
|
67
|
+
response = @@alchemyapi.concepts('html', @@test_html)
|
68
|
+
assert_equal response['status'], 'OK'
|
69
|
+
response = @@alchemyapi.concepts('url', @@test_url)
|
70
|
+
assert_equal response['status'], 'OK'
|
71
|
+
response = @@alchemyapi.concepts('random', @@test_text)
|
72
|
+
assert_equal response['status'], 'ERROR' # invalid flavor
|
73
|
+
puts 'Concept tests complete'
|
74
|
+
puts ''
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_sentiment
|
78
|
+
puts 'Checking sentiment . . . '
|
79
|
+
response = @@alchemyapi.sentiment('text', @@test_text)
|
80
|
+
assert_equal response['status'], 'OK'
|
81
|
+
response = @@alchemyapi.sentiment('html', @@test_html)
|
82
|
+
assert_equal response['status'], 'OK'
|
83
|
+
response = @@alchemyapi.sentiment('url', @@test_url)
|
84
|
+
assert_equal response['status'], 'OK'
|
85
|
+
response = @@alchemyapi.sentiment('random', @@test_text)
|
86
|
+
assert_equal response['status'], 'ERROR' # invalid flavor
|
87
|
+
puts 'Sentiment tests complete'
|
88
|
+
puts ''
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_sentiment_targeted
|
92
|
+
puts 'Checking targeted sentiment . . . '
|
93
|
+
response = @@alchemyapi.sentiment_targeted('text', @@test_text, 'heart')
|
94
|
+
assert_equal response['status'], 'OK'
|
95
|
+
response = @@alchemyapi.sentiment_targeted('html', @@test_html, 'language')
|
96
|
+
assert_equal response['status'], 'OK'
|
97
|
+
response = @@alchemyapi.sentiment_targeted('url', @@test_url, 'Congress')
|
98
|
+
assert_equal response['status'], 'OK'
|
99
|
+
response = @@alchemyapi.sentiment_targeted('text', @@test_text, nil)
|
100
|
+
assert_equal response['status'], 'ERROR' # invalid target
|
101
|
+
response = @@alchemyapi.sentiment_targeted('random', @@test_url, 'Congress')
|
102
|
+
assert_equal response['status'], 'ERROR' # invalid flavor
|
103
|
+
puts 'Targeted sentiment tests complete'
|
104
|
+
puts ''
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_text
|
108
|
+
puts 'Checking clean text . . . '
|
109
|
+
response = @@alchemyapi.text('text', @@test_text)
|
110
|
+
assert_equal response['status'], 'ERROR' # only valid for HTML and URL content
|
111
|
+
response = @@alchemyapi.text('html', @@test_html)
|
112
|
+
assert_equal response['status'], 'OK'
|
113
|
+
response = @@alchemyapi.text('url', @@test_url)
|
114
|
+
assert_equal response['status'], 'OK'
|
115
|
+
puts 'Clean text tests complete'
|
116
|
+
puts ''
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_text_raw
|
120
|
+
puts 'Checking raw text . . . '
|
121
|
+
response = @@alchemyapi.text_raw('text', @@test_text)
|
122
|
+
assert_equal response['status'], 'ERROR' # only valid for HTML and URL content
|
123
|
+
response = @@alchemyapi.text_raw('html', @@test_html)
|
124
|
+
assert_equal response['status'], 'OK'
|
125
|
+
response = @@alchemyapi.text_raw('url', @@test_url)
|
126
|
+
assert_equal response['status'], 'OK'
|
127
|
+
puts 'Raw text tests complete'
|
128
|
+
puts ''
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_text_author
|
132
|
+
puts 'Checking author. . . '
|
133
|
+
response = @@alchemyapi.author('text', @@test_text)
|
134
|
+
assert_equal response['status'], 'ERROR' # only valid for HTML and URL content
|
135
|
+
response = @@alchemyapi.author('html', @@test_html)
|
136
|
+
assert_equal response['status'], 'ERROR' # there is no author information in the test HTML
|
137
|
+
response = @@alchemyapi.author('url', @@test_url)
|
138
|
+
assert_equal response['status'], 'OK'
|
139
|
+
puts 'Author tests complete'
|
140
|
+
puts ''
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_title
|
144
|
+
puts 'Checking title . . . '
|
145
|
+
response = @@alchemyapi.title('text', @@test_text)
|
146
|
+
assert_equal response['status'], 'ERROR' # only valid for HTML and URL content
|
147
|
+
response = @@alchemyapi.title('html', @@test_html)
|
148
|
+
assert_equal response['status'], 'OK'
|
149
|
+
response = @@alchemyapi.title('url', @@test_url)
|
150
|
+
assert_equal response['status'], 'OK'
|
151
|
+
puts 'Title tests complete'
|
152
|
+
puts ''
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_relations
|
156
|
+
puts 'Checking relations . . . '
|
157
|
+
response = @@alchemyapi.relations('text', @@test_text)
|
158
|
+
assert_equal response['status'], 'OK'
|
159
|
+
response = @@alchemyapi.relations('html', @@test_html)
|
160
|
+
assert_equal response['status'], 'OK'
|
161
|
+
response = @@alchemyapi.relations('url', @@test_url)
|
162
|
+
assert_equal response['status'], 'OK'
|
163
|
+
response = @@alchemyapi.relations('random', @@test_text)
|
164
|
+
assert_equal response['status'], 'ERROR' # invalid flavor
|
165
|
+
puts 'Relations tests complete'
|
166
|
+
puts ''
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_category
|
170
|
+
puts 'Checking category . . . '
|
171
|
+
response = @@alchemyapi.category('text', @@test_text)
|
172
|
+
assert_equal response['status'], 'OK'
|
173
|
+
response = @@alchemyapi.category('html', @@test_html, { 'url' => 'test' })
|
174
|
+
assert_equal response['status'], 'OK'
|
175
|
+
response = @@alchemyapi.category('url', @@test_url)
|
176
|
+
assert_equal response['status'], 'OK'
|
177
|
+
response = @@alchemyapi.category('random', @@test_text)
|
178
|
+
assert_equal response['status'], 'ERROR' # invalid flavor
|
179
|
+
puts 'Category tests complete'
|
180
|
+
puts ''
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_feeds
|
184
|
+
puts 'Checking feeds . . . '
|
185
|
+
response = @@alchemyapi.feeds('text', @@test_text)
|
186
|
+
assert_equal response['status'], 'ERROR' # only valid for HTML and URL content
|
187
|
+
response = @@alchemyapi.feeds('html', @@test_html, { 'url' => 'test' })
|
188
|
+
assert_equal response['status'], 'OK'
|
189
|
+
response = @@alchemyapi.feeds('url', @@test_url)
|
190
|
+
assert_equal response['status'], 'OK'
|
191
|
+
puts 'Feed tests complete'
|
192
|
+
puts ''
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_microformats
|
196
|
+
puts 'Checking microformats . . . '
|
197
|
+
response = @@alchemyapi.microformats('text', @@test_text)
|
198
|
+
assert_equal response['status'], 'ERROR' # only valid for HTML and URL content
|
199
|
+
response = @@alchemyapi.microformats('html', @@test_html, { 'url' => 'test' })
|
200
|
+
assert_equal response['status'], 'OK'
|
201
|
+
response = @@alchemyapi.microformats('url', @@test_url)
|
202
|
+
assert_equal response['status'], 'OK'
|
203
|
+
puts 'Microformat tests complete'
|
204
|
+
puts ''
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_taxonomy
|
208
|
+
puts 'Checking taxonomy . . . '
|
209
|
+
response = @@alchemyapi.taxonomy('text', @@test_text)
|
210
|
+
assert_equal response['status'], 'OK'
|
211
|
+
response = @@alchemyapi.taxonomy('url', @@test_url)
|
212
|
+
assert_equal response['status'], 'OK'
|
213
|
+
response = @@alchemyapi.taxonomy('html', @@test_html, { 'url' => 'test' })
|
214
|
+
assert_equal response['status'], 'OK'
|
215
|
+
response = @@alchemyapi.taxonomy('random', @@test_text)
|
216
|
+
assert_equal response['status'], 'ERROR' # invalid flavor
|
217
|
+
puts 'Taxonomy tests complete'
|
218
|
+
puts ''
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_combined
|
222
|
+
puts 'Checking combined . . . '
|
223
|
+
response = @@alchemyapi.combined('html', @@test_html, { 'url' => 'test' })
|
224
|
+
assert_equal response['status'], 'ERROR' # only valid for text and URL content
|
225
|
+
response = @@alchemyapi.combined('text', @@test_text)
|
226
|
+
assert_equal response['status'], 'OK'
|
227
|
+
response = @@alchemyapi.combined('url', @@test_url)
|
228
|
+
assert_equal response['status'], 'OK'
|
229
|
+
puts 'Combined tests complete'
|
230
|
+
puts ''
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_image_extract
|
234
|
+
puts 'Checking image extract . . . '
|
235
|
+
response = @@alchemyapi.image_extract('text', @@test_text)
|
236
|
+
assert_equal response['status'], 'ERROR' # only valid for URL content
|
237
|
+
response = @@alchemyapi.image_extract('html', @@test_html, { 'url' => 'test' })
|
238
|
+
assert_equal response['status'], 'ERROR' # only valid for URL content
|
239
|
+
response = @@alchemyapi.image_extract('url', @@test_url)
|
240
|
+
assert_equal response['status'], 'OK'
|
241
|
+
puts 'Image extract tests complete'
|
242
|
+
puts ''
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_image_tag
|
246
|
+
puts 'Checking image tag . . . '
|
247
|
+
response = @@alchemyapi.image_tag('text', @@test_text)
|
248
|
+
assert_equal response['status'], 'ERROR' # only valid for URL or image content
|
249
|
+
response = @@alchemyapi.image_tag('html', @@test_html, { 'url' => 'test' })
|
250
|
+
assert_equal response['status'], 'ERROR' # only valid for URL or image content
|
251
|
+
response = @@alchemyapi.image_tag('url', @@test_url)
|
252
|
+
assert_equal response['status'], 'OK'
|
253
|
+
response = @@alchemyapi.image_tag('image', '', { 'imagePostMode' => 'raw' }, @@test_image)
|
254
|
+
assert_equal response['status'], 'OK'
|
255
|
+
puts 'Image tag tests complete'
|
256
|
+
puts ''
|
257
|
+
end
|
258
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alchemy-api-rb-gem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.2'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- AlchemyAPI
|
8
|
+
- ProGM
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-06-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.8'
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.8.1
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "~>"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '1.8'
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.1
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: faraday
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.8'
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.8.1
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - "~>"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0.8'
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.8.1
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: excon
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0.28'
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 0.28.0
|
64
|
+
type: :runtime
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0.28'
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 0.28.0
|
74
|
+
- !ruby/object:Gem::Dependency
|
75
|
+
name: minitest
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '5.3'
|
81
|
+
type: :development
|
82
|
+
prerelease: false
|
83
|
+
version_requirements: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '5.3'
|
88
|
+
- !ruby/object:Gem::Dependency
|
89
|
+
name: rake
|
90
|
+
requirement: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '10.3'
|
95
|
+
type: :development
|
96
|
+
prerelease: false
|
97
|
+
version_requirements: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '10.3'
|
102
|
+
description: A gem distribution for AlchemyAPI for ruby, see https://github.com/AlchemyAPI/alchemyapi_ruby
|
103
|
+
email: progiemmeh@gmail.com
|
104
|
+
executables: []
|
105
|
+
extensions: []
|
106
|
+
extra_rdoc_files: []
|
107
|
+
files:
|
108
|
+
- dog.jpg
|
109
|
+
- lib/alchemyapi.rb
|
110
|
+
- tests/test_alchemyapi.rb
|
111
|
+
homepage: https://github.com/ProGM/alchemy-api-rb-gem
|
112
|
+
licenses:
|
113
|
+
- Apache License 2.0
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.4.1
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: AlchemyAPI
|
135
|
+
test_files: []
|