-
-
Save videlais/84bdb60fc16722a0f4d7 to your computer and use it in GitHub Desktop.
An example of how to call the Wordnik API in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Client = require('node-rest-client').Client; | |
var APIKEY = ''; | |
// Create a full configuration object | |
// See http://developer.wordnik.com/docs.html | |
var config = { | |
hasDictionaryDef: false, | |
includePartOfSpeech: "verb-transitive", | |
minCorpusCount: 0, | |
maxCorpusCount: -1, | |
minDictionaryCount: 1, | |
maxDictionaryCount: -1, | |
minLength: 5, | |
maxLength: -1, | |
limit: 2, | |
api_key: APIKEY | |
}; | |
function getRandomWords(config) { | |
// Create a new Client object | |
var client = new Client(); | |
// Start to build the URL | |
var wordnikURL = 'http://api.wordnik.com/v4/words.json/randomWords?'; | |
// Parse the configuraiton object and create the request URL | |
for(var option in config) { | |
wordnikURL = wordnikURL + "&" + option + "=" + config[option]; | |
} | |
// Make sure we are asking for JSON | |
var args = {headers: {'Accept':'application/json'}}; | |
// Create the GET request | |
client.get(wordnikURL, args, function (data, response) { | |
// Once the statusCode is 200, the response has been received with no problems | |
if (response.statusCode === 200) { | |
// Parse the JSON received as the response | |
var result = JSON.parse(data); | |
// "result" will have the object created from the parsed JSON | |
console.log(result); | |
} | |
}); | |
}; | |
// Finally, actually call the function with the configuration object | |
getRandomWords(config); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment