Skip to content

Commit

Permalink
feat(generators index.js): Add random generator to the api
Browse files Browse the repository at this point in the history
Add random generator endpoint call to the api wrapper
  • Loading branch information
maximodleon committed Jan 20, 2018
1 parent c9cb9a1 commit 778fd78
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 36 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ cache:
notifications:
email: false
node_js:
- '9'
- '8'
- '6'
- '4'
script:
- npm run test
after_success:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"travis-deploy-once": "^4.3.2"
},
"czConfig": {
"path": "node_module/cz-conventional-changelog"
"path": "node_modules/cz-conventional-changelog"
}
}
41 changes: 41 additions & 0 deletions src/generators/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const {
queryApi
} = require('../utils/urlUtils')

const DEFAULT_PARAMS = {
prop: 'info',
inprop: 'url',
format: 'json',
action: 'query'
}

function doSearch(searchTerm, lang = "en", options = {}) {
const params = _getParams(options, {
gsrsearch: searchTerm,
generator: 'search'
})

return queryApi(lang, params).then((response) => {
return response
})
}

function doRandom(lang = "en", options = {}) {
const params = _getParams(options, {
generator: 'random'
})

return queryApi(lang, params).then((response) => {
return response
})
}

function _getParams(queryOptions = {}, generatorOptions = {}) {
return Object.assign({}, DEFAULT_PARAMS, queryOptions, generatorOptions)
}


module.exports = {
doSearch,
doRandom
}
27 changes: 0 additions & 27 deletions src/generators/search.js

This file was deleted.

10 changes: 8 additions & 2 deletions src/utils/urlUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const axios = require('axios')

const queryApi = (url, params) => {
const API_BASE =
".wikipedia.org/w/api.php?";


const queryApi = (lang = "en", params) => {
const url = `https://${lang}${API_BASE}`
return axios.get(url, {
params: {
...params
Expand All @@ -15,5 +20,6 @@ const queryApi = (url, params) => {
};

module.exports = {
queryApi
queryApi,
API_BASE
};
12 changes: 9 additions & 3 deletions src/wiki.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const {
doSearch
} = require('./generators/search')
doSearch,
doRandom
} = require('./generators/index')

function search(searchTerm, lang = "en", options) {
return doSearch(searchTerm, lang, options)
}

function random(lang = "en", options) {
return doRandom(lang, options)
}

module.exports = {
search
search,
random
};

0 comments on commit 778fd78

Please sign in to comment.