Skip to content

Commit

Permalink
Merge pull request #83 from ArkhamCookie/69-top-example-api
Browse files Browse the repository at this point in the history
topExample API coverage
  • Loading branch information
ArkhamCookie authored Nov 17, 2023
2 parents bbb2261 + 3f20ece commit cc509f3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
| [`/{word}/pronunciations`](https://developer.wordnik.com/docs#!/word/getTextPronunciations) | Returns text pronunciations for a given word | [ ] |
| [`/{word}/relatedWords`](https://developer.wordnik.com/docs#!/word/getRelatedWords) | Given a word as a string, returns relationships from the Word Graph | [ ] |
| [`/{word}/scrabbleScore`](https://developer.wordnik.com/docs#!/word/getScrabbleScore) | Returns the Scrabble score for a word | [x] |
| [`/{word}/topExample`](https://developer.wordnik.com/docs#!/word/getTopExample) | Returns a top example for a word | [ ] |
| [`/{word}/topExample`](https://developer.wordnik.com/docs#!/word/getTopExample) | Returns a top example for a word | [x] |

### [`/words.json`](https://developer.wordnik.com/docs#/words)

Expand Down
22 changes: 22 additions & 0 deletions lib/tests/topExample-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env -S deno test --allow-read --allow-env --allow-net

import { assertEquals, assertExists } from 'assert'

import { topExample } from '../mod.js'

Deno.test({
name: 'word/topExample',
permissions: { read: true, env: true, net: true },
async fn() {
const testWord = 'cookie'

const response = await topExample(testWord)
assertExists(response)

assertEquals(response.word, testWord)

// This might have to be updated if the top example changes
const expectedResponse = 'I noticed there\'s a small cookie, nice? ya all for $7.90 but excluding serbice charge la. de tea/coffee comes with a kan small cookie~'
assertEquals(response.text, expectedResponse)
}
})
1 change: 1 addition & 0 deletions lib/word/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

export * from './scrabbleScore.js'
export * from './examples.js'
export * from './topExample.js'
32 changes: 32 additions & 0 deletions lib/word/topExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Wordnik } from '../mod.js'

/**
* Top Example
* @param {string} word - word to fetch examples for
* @param {boolean} useCanonical - If true will try to return the correct word root ('cats' -> 'cat'). If false returns exactly what was requested.
* @returns {JSON} response
*/

export async function topExample(word, useCanonical) {
if (!word) { console.error('A word is required') }
const wordnik = new Wordnik('word.json/' + word + '/topExample')
let endpoint =
wordnik.base + wordnik.version + wordnik.target

if (useCanonical === true) {
endpoint = endpoint + 'useCanonical=true'
}

endpoint = endpoint + '&' + wordnik.key

const response = await fetch(endpoint)

console.debug(endpoint)

if (response.status !== 200) {
console.error('Error! Status: ' + response.statusText)
return
}

return await response.json()
}

0 comments on commit cc509f3

Please sign in to comment.