alchemy.js | |
---|---|
(function(){
| |
Reference to global object |
var root = this;
|
The top-level namespace. All public Backbone classes and modules will be attached to this. Exported for both NodeJS and the browser. |
var Alchemy;
if (typeof exports !== 'undefined') {
Alchemy = exports;
} else {
Alchemy = root.Alchemy = {};
}
|
Main API object for the library. Any pieces of AlchemyAPI should be called
through this object.
'options.apiKey' -- API access key |
Alchemy.api = function(options){
var apiKey,
outputMode,
hostPrefix;
initialize(options);
|
initialize to either 'options' or to default values |
function initialize(options){
options = options || {};
apiKey = options.apiKey || "";
outputMode = options.outputMode || "json";
hostPrefix = options.hostPrefix || "access";
}
|
RESTful POST command, with params created from 'parameterObject'. |
function doPost(endpoint,prefix,parameterObject){
$.ajax({
url : "http://" + hostPrefix + ".alchemyapi.com/calls/" + prefix + "/" + endpoint,
type : "POST",
data : "apikey=" + apiKey + parameterObject.getParameterString(),
success : function(response){
handleResponse(response,parameterObject);
}
});
}
|
Handles all response from AlchemyAPI. |
function handleResponse(response,parameterObject){
if(parameterObject.outputMode() === "json"){
if(response.status.toUpperCase() !== "OK"){
throw {
name : "AjaxReplyError",
message : response.statusInfo
}
}
}
return response;
}
|
Public api object |
return {
|
returns outputMode ('json','xml','rdf') |
outputMode : function(){
return outputMode;
},
|
returns hostPrefix |
hostPrefix : function(){
return hostPrefix;
},
|
basic validity check |
isValidKey : function(){
return apiKey.length >= 5;
},
|
Sentiment Analysis for any 'text' passed in. Returns API response in supplied 'outputMode' format. |
textGetTextSentiment : function(text,outputMode,baseParams){
text = text || "";
outputMode = outputMode || "";
baseParams = baseParams || Alchemy.params({text:text,outputMode:outputMode});
return doPost("TextGetTextSentiment", "text", baseParams);
}
};
};
|
Basic params object from creating url param string to be passed
into RESTful interface
'options.outputMode' -- json or xml. 'json' by default.
'options.url' -- url of html document |
Alchemy.params = function(options){
var outputMode,
url,
html,
text;
initialize(options);
|
initialize to either 'options' or to default values |
function initialize(options){
options = options || {};
outputMode = options.outputMode || "json";
url = options.url || "";
html = options.html || "";
text = options.text || "";
}
return {
|
getters for param values |
outputMode : function(){
return outputMode;
},
url : function(){
return url;
},
html : function(){
return html;
},
text : function(){
return text;
},
|
returns built param string |
getParameterString : function(){
var paramString = "";
paramString += url.length ? "&url=" + url : "";
paramString += html.length ? "&html=" + html : "";
paramString += text.length ? "&text=" + encodeURI(text) : "";
paramString += outputMode.length ? "&outputMode=" + outputMode : "";
return paramString;
}
};
}
}).call(this);
|