-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from ArkhamCookie/69-top-example-api
topExample API coverage
- Loading branch information
Showing
4 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
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
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
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) | ||
} | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
|
||
export * from './scrabbleScore.js' | ||
export * from './examples.js' | ||
export * from './topExample.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
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() | ||
} |