├── .npmignore ├── .gitignore ├── grunt.js ├── LICENSE-MIT ├── package.json ├── test └── wordnik-bb_test.js ├── README.md └── lib └── wordnik-bb.js /.npmignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | 3 | lib-cov 4 | *.seed 5 | *.log 6 | *.csv 7 | *.dat 8 | *.out 9 | *.pid 10 | *.gz 11 | 12 | pids 13 | logs 14 | results 15 | 16 | npm-debug.log 17 | -------------------------------------------------------------------------------- /grunt.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | 3 | // Project configuration. 4 | grunt.initConfig({ 5 | pkg: '', 6 | test: { 7 | files: ['test/**/*.js'] 8 | }, 9 | lint: { 10 | files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js'] 11 | }, 12 | watch: { 13 | files: '', 14 | tasks: 'default' 15 | }, 16 | jshint: { 17 | options: { 18 | curly: true, 19 | eqeqeq: true, 20 | immed: true, 21 | latedef: true, 22 | newcap: true, 23 | noarg: true, 24 | sub: true, 25 | undef: true, 26 | boss: true, 27 | eqnull: true, 28 | node: true, 29 | strict: false 30 | }, 31 | globals: { 32 | exports: true 33 | } 34 | } 35 | }); 36 | 37 | // Default task. 38 | grunt.registerTask('default', 'lint test'); 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Darius Kazemi 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wordnik-bb", 3 | "description": "An interface to the Wordnik API, which lets you get dictionary definitions, random words, pronunciation, and more! Built with Backbone.js, and uses promises.", 4 | "version": "0.1.1", 5 | "homepage": "https://github.com/dariusk/wordnik-bb", 6 | "author": { 7 | "name": "Darius Kazemi", 8 | "email": "darius.kazemi@gmail.com", 9 | "url": "http://tinysubversions.com" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/dariusk/wordnik-bb.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/dariusk/wordnik-bb/issues" 17 | }, 18 | "licenses": [ 19 | { 20 | "type": "MIT", 21 | "url": "https://github.com/dariusk/wordnik-bb/blob/master/LICENSE-MIT" 22 | } 23 | ], 24 | "main": "lib/wordnik-bb", 25 | "dependencies": { 26 | "underscore": ">=1.4.4", 27 | "backbone": ">=0.9.10", 28 | "request": "2.12.0", 29 | "jquery": "1.8.3" 30 | }, 31 | "engines": { 32 | "node": ">=0.8.0" 33 | }, 34 | "scripts": { 35 | "test": "grunt test" 36 | }, 37 | "devDependencies": { 38 | "grunt": "~0.3.17" 39 | }, 40 | "keywords": [] 41 | } 42 | -------------------------------------------------------------------------------- /test/wordnik-bb_test.js: -------------------------------------------------------------------------------- 1 | var _ = require('underscore'); 2 | var Backbone = require('backbone'); 3 | 4 | var wordnik_bb = require('../lib/wordnik-bb.js'); 5 | 6 | /* 7 | ======== A Handy Little Nodeunit Reference ======== 8 | https://github.com/caolan/nodeunit 9 | 10 | Test methods: 11 | test.expect(numAssertions) 12 | test.done() 13 | Test assertions: 14 | test.ok(value, [message]) 15 | test.equal(actual, expected, [message]) 16 | test.notEqual(actual, expected, [message]) 17 | test.deepEqual(actual, expected, [message]) 18 | test.notDeepEqual(actual, expected, [message]) 19 | test.strictEqual(actual, expected, [message]) 20 | test.notStrictEqual(actual, expected, [message]) 21 | test.throws(block, [error], [message]) 22 | test.doesNotThrow(block, [error], [message]) 23 | test.ifError(value) 24 | */ 25 | 26 | exports['init'] = { 27 | setUp: function(done) { 28 | // setup here 29 | this.W = wordnik_bb.init(''); 30 | done(); 31 | }, 32 | 'Wordnik object structure': function(test) { 33 | test.expect(2); 34 | // tests here 35 | test.equal(typeof wordnik_bb, "object", "Wordnik lib returns an object."); 36 | test.equal(typeof wordnik_bb.init, "function", 'The object has an init function'); 37 | test.done(); 38 | }, 39 | 'Word model': function(test) { 40 | test.expect(2); 41 | test.equal(typeof this.W.Word, "function", "Word is a function (constructor)"); 42 | var word = new this.W.Word({ word: 'kings' }); 43 | test.ok(word.idAttribute, "Word constructor returns a Backbone Model"); 44 | test.done(); 45 | }, 46 | tearDown: function(done) { 47 | delete this.W; 48 | done(); 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wordnik-bb 2 | 3 | An interface to the Wordnik API, which lets you get dictionary definitions, random words, pronunciation, and more! Built with Backbone.js. 4 | 5 | ## Getting Started 6 | 7 | Make sure you have a [Wordnik API key](http://developer.wordnik.com/) and pass it to the init function on require: 8 | 9 | ```javascript 10 | var APIKEY = 'YOURAPIKEY'; 11 | var Wordnik = require('wordnik-bb').init(APIKEY); 12 | ``` 13 | 14 | ## Examples 15 | Here's an example of declaring a new word and then populating it with the data from every available word method. 16 | ```javascript 17 | var word = new Wordnik.Word({word: 'king', params:{includeSuggestions:true}}); 18 | word.getEverything() 19 | .then( function() { 20 | console.log("A WHOLE lot of data in a Word model: ", word); 21 | }); 22 | ``` 23 | 24 | Here's an example of using the getRandomWordModel function, which behind the scenes generates a random word and then creates a Wordnik.Word model based on it. 25 | ```javascript 26 | var randomWordPromise = Wordnik.getRandomWordModel({ 27 | includePartOfSpeech: "verb-transitive", 28 | minCorpusCount: 10000 29 | } 30 | ); 31 | randomWordPromise.done(function(word) { 32 | console.log("The model for our random word: ", word); 33 | // We could also get more info about the random word: 34 | // word.getEverything() 35 | // .then( function() { 36 | // console.log("And now we've populated the model with all the available data: ", word); 37 | // } 38 | }); 39 | ``` 40 | 41 | ## Contributing 42 | In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/gruntjs/grunt). 43 | 44 | ## Release History 45 | * 0.1.0 (2/14/2013) - initial release 46 | 47 | ## License 48 | Copyright (c) 2013 Darius Kazemi 49 | Licensed under the MIT license. 50 | -------------------------------------------------------------------------------- /lib/wordnik-bb.js: -------------------------------------------------------------------------------- 1 | /* 2 | * wordnik-bb 3 | * https://github.com/dariusk/wordnik-bb 4 | * 5 | * Copyright (c) 2013 Darius Kazemi 6 | * Licensed under the MIT license. 7 | */ 8 | 9 | // TODO: add validation 10 | var _ = require('underscore'); 11 | var Backbone = require('backbone'); 12 | var request = require('request'); 13 | var $ = require('jquery'); 14 | Backbone.$ = $; 15 | 16 | exports.init = function(APIKEY) { 17 | var Word = Backbone.Model.extend({ 18 | idAttribute: 'word', 19 | defaults: { 20 | params: { 21 | useCanonical: false, 22 | includeSuggestions: false 23 | } 24 | }, 25 | url: function() { 26 | var url = "http://api.wordnik.com//v4/word.json/"+this.get('word'); 27 | //console.log(url); 28 | return url; 29 | }, 30 | validate: function(attrs, options) { 31 | }, 32 | parse: function(resp,options) { 33 | // if we're getting the base object, just parse it so the properties end up on the root of the model's attributes 34 | if (options.command === "") { 35 | return JSON.parse(resp); 36 | } 37 | // otherwise, the response from this API call should go into a property with the same name as the API call 38 | else { 39 | var toReturn = {}; 40 | toReturn[options.command.substr(1,options.command.length)] = JSON.parse(resp); 41 | return toReturn; 42 | } 43 | }, 44 | sync: function(method, model, options) { 45 | // Parse query string from the params object property 46 | var params = this.get("params"); 47 | var queryString = ""; 48 | for (var i in params) { 49 | if (params.hasOwnProperty(i)) { 50 | queryString += "&"+i+"="+params[i]; 51 | } 52 | } 53 | queryString = queryString.substr(1,queryString.length); 54 | 55 | // override sync 56 | var _sync = Backbone.sync; 57 | var deferred = $.Deferred(); 58 | if (method === 'read') { 59 | // use the request module instead of jQuery so we can do serverside cross-domain access 60 | request({ 61 | url : _.result(model, 'url')+options.command+"?"+queryString+"&api_key="+APIKEY 62 | }, function(error, response, body) { 63 | if (!error && response.statusCode === 200) { 64 | options.success(model, body, options); 65 | deferred.resolve(model); 66 | } 67 | else { 68 | options.error(model, response, options.BBoptions); 69 | deferred.reject(model); 70 | } 71 | }); 72 | 73 | return deferred.promise(); 74 | } 75 | else { 76 | return _sync.apply(this, arguments); 77 | } 78 | }, 79 | callWordnik: function(resource) { 80 | var self = this; 81 | return this.fetch({ 82 | command: resource, 83 | success : function(model, response) { 84 | //console.log(response); 85 | }, 86 | error : function(model, response) { 87 | console.log(response.responseText); 88 | } 89 | }); 90 | }, 91 | getWord: function() { 92 | return this.callWordnik(""); 93 | }, 94 | getExamples: function() { 95 | return this.callWordnik("/examples"); 96 | }, 97 | getDefinitions: function() { 98 | return this.callWordnik("/definitions"); 99 | }, 100 | getTopExample: function() { 101 | return this.callWordnik("/topExample"); 102 | }, 103 | getRelatedWords: function() { 104 | return this.callWordnik("/relatedWords"); 105 | }, 106 | getPronunciations: function() { 107 | return this.callWordnik("/pronunciations"); 108 | }, 109 | getScrabbleScore: function() { 110 | return this.callWordnik("/scrabbleScore"); 111 | }, 112 | getHyphenation: function() { 113 | return this.callWordnik("/hyphenation"); 114 | }, 115 | getFrequency: function() { 116 | return this.callWordnik("/frequency"); 117 | }, 118 | getPhrases: function() { 119 | return this.callWordnik("/phrases"); 120 | }, 121 | getEtymologies: function() { 122 | return this.callWordnik("/etymologies"); 123 | }, 124 | getAudio: function() { 125 | return this.callWordnik("/audio"); 126 | }, 127 | getEverything: function() { 128 | var self = this; 129 | 130 | return $.when( 131 | self.getWord(), 132 | self.getExamples(), 133 | self.getDefinitions(), 134 | self.getTopExample(), 135 | self.getRelatedWords(), 136 | self.getPronunciations(), 137 | self.getScrabbleScore(), 138 | self.getHyphenation(), 139 | self.getFrequency(), 140 | self.getPhrases(), 141 | self.getEtymologies(), 142 | self.getAudio() 143 | ) 144 | .then(function() { 145 | }) 146 | .fail(function() { 147 | console.log("failed!"); 148 | }); 149 | } 150 | }); 151 | var Words = Backbone.Collection.extend({ 152 | model: Word 153 | }); 154 | var RandomWord = Backbone.Model.extend({ 155 | defaults: { 156 | params: {} 157 | }, 158 | url: function() { 159 | // Parse query string from the params object property 160 | var params = this.get("params"); 161 | var queryString = ""; 162 | for (var i in params) { 163 | if (params.hasOwnProperty(i) && i !== "wordParams") { 164 | queryString += "&"+i+"="+params[i]; 165 | } 166 | } 167 | queryString = queryString.substr(1,queryString.length); 168 | 169 | return "http://api.wordnik.com/v4/words.json/randomWord?" + queryString + "&api_key=" + APIKEY; 170 | }, 171 | getRandomWord: function() { 172 | return this.fetch({ 173 | success: function(model, response) { 174 | }, 175 | error: function(model, response) { 176 | console.log("Error fetching random word: " + response.responseText); 177 | } 178 | }); 179 | } 180 | }); 181 | var getRandomWordModel = function(options) { 182 | this.options = options || {}; 183 | var deferred = $.Deferred(); 184 | var self = this; 185 | var random = new RandomWord({ 186 | params: self.options 187 | }); 188 | 189 | random.getRandomWord() 190 | .then( function(random) { 191 | var word = new Word({word: random.word, params: self.options.wordParams}); 192 | deferred.resolve(word); 193 | }); 194 | 195 | return deferred.promise(); 196 | }; 197 | return { 198 | Word: Word, 199 | Words: Words, 200 | RandomWord: RandomWord, 201 | getRandomWordModel: getRandomWordModel 202 | }; 203 | }; 204 | --------------------------------------------------------------------------------