Skip to content
This repository has been archived by the owner on Nov 30, 2019. It is now read-only.

Commit

Permalink
Merge pull request #7 from philipmckenna/configuration-and-tests
Browse files Browse the repository at this point in the history
test: client method tests
  • Loading branch information
migo315 authored Oct 8, 2019
2 parents 2378c7c + c5f03cb commit 65b4c34
Show file tree
Hide file tree
Showing 14 changed files with 741 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BABEL_ENV=production
BABEL_ENV=production
7 changes: 7 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('dotenv').config();

module.exports = {
baseUrl: 'https://api.coaster.cloud',
withAcl: false,
language: 'en',
};
44 changes: 0 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"lodash": "^4.17.11"
},
"devDependencies": {
"axios-mock-adapter": "^1.17.0",
"babel-jest": "^24.9.0",
"babel-minify": "^0.5.1",
"babel-preset-es2015": "^6.24.1",
Expand Down
70 changes: 70 additions & 0 deletions src/build-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const _ = require('lodash');
const { language } = require('../config');

const buildQuery = (config, withAcl) => {
let query = [];

if (config.hasOwnProperty('page') && config.page) {
query.push('page=' + encodeURIComponent(config.page));
}

if (config.hasOwnProperty('itemsPerPage') && config.itemsPerPage) {
query.push('itemsPerPage=' + encodeURIComponent(config.itemsPerPage));
}

if (config.hasOwnProperty('searchTerm') && config.searchTerm) {
query.push('search=' + encodeURIComponent(config.searchTerm));
}

if (config.hasOwnProperty('language') && config.language) {
query.push('language=' + encodeURIComponent(config.language));
} else {
query.push('language=' + encodeURIComponent(language));
}

if (config.hasOwnProperty('sort') && config.sort) {
query.push('sort=' + encodeURIComponent(config.sort));
}

if (config.hasOwnProperty('regulation')) {
let regulation = _.merge(
{
size: null,
age: null
},
config.regulation
);

if (regulation.size !== null || regulation.age !== null) {
let size = (/^\d+$/.test(regulation.size)) ? regulation.size : 'null';
let age = (/^\d+$/.test(regulation.age)) ? regulation.age : 'null';

query.push('filter[]=' + encodeURIComponent(`regulation(${size},${age})`));
}
}

if (config.hasOwnProperty('facets')) {
config.facets.forEach(function (facet) {
query.push('facet[]=' + encodeURIComponent(facet));
});
}

if (config.hasOwnProperty('filters')) {
config.filters.forEach(function (filter) {
query.push('filter[]=' + encodeURIComponent(filter));
});
}

if (config.hasOwnProperty('founded') && config.founded !== null && (/^\d{4}$/.test(config.founded))) {
let year = config.founded;
query.push('filter[]=' + encodeURIComponent(`founded.year:${year}`));
}

if (withAcl === true) {
query.push('acl=true');
}

return query.length > 0 ? '?' + query.join('&') : '';
};

module.exports = { buildQuery };
89 changes: 10 additions & 79 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@

// Imports
const Axios = require('axios');
const _ = require('lodash');
const { baseUrl, withAcl } = require('../config');
const { buildQuery } = require('./build-query');

module.exports = {
baseUrl: 'https://api.coaster.cloud',
withAcl: false,
language: 'en',

// Get parks list by optional config or null on failure
async getParks(config = {}) {
let result = null;

try {
let response = await Axios.get(this.baseUrl + '/parks' + this.buildQuery(config));
let response = await Axios.get(baseUrl + '/parks' + buildQuery(config, withAcl));
result = response.data;
} catch (error) {
console.error(error)
Expand All @@ -35,7 +33,7 @@ module.exports = {
let result = null;

try {
let response = await Axios.get(this.baseUrl + '/parks/' + uuid + this.buildQuery(config));
let response = await Axios.get(baseUrl + '/parks/' + uuid + buildQuery(config, withAcl));
result = response.data;
} catch (error) {
console.error(error)
Expand All @@ -50,7 +48,7 @@ module.exports = {

try {
let response = await Axios.get(
this.baseUrl + '/parks/' + uuid + '/waiting-times' + this.buildQuery(config)
baseUrl + '/parks/' + uuid + '/waiting-times' + buildQuery(config, withAcl)
);
result = response.data;
} catch (error) {
Expand All @@ -66,13 +64,13 @@ module.exports = {
let url = null;

if (config.hasOwnProperty('park') && config.park) {
url = this.baseUrl + '/parks/' + config.park + '/attractions';
url = baseUrl + '/parks/' + config.park + '/attractions';
} else {
url = this.baseUrl + '/attractions';
url = baseUrl + '/attractions';
}

try {
let response = await Axios.get(url + this.buildQuery(config));
let response = await Axios.get(url + buildQuery(config, withAcl));
result = response.data;
} catch (error) {
console.error(error)
Expand All @@ -86,7 +84,7 @@ module.exports = {
let result = null;

try {
let response = await Axios.get(this.baseUrl + '/attractions/' + uuid + this.buildQuery(config));
let response = await Axios.get(baseUrl + '/attractions/' + uuid + buildQuery(config, withAcl));
result = response.data;
} catch (error) {
console.error(error)
Expand All @@ -105,79 +103,12 @@ module.exports = {
};

try {
await Axios.post(this.baseUrl + '/parks/' + park + '/waiting-times', queues, config);
await Axios.post(baseUrl + '/parks/' + park + '/waiting-times', queues, config);
result = true;
} catch (error) {
console.error(error)
}

return result;
},

// Build query
buildQuery(config) {
let query = [];

if (config.hasOwnProperty('page') && config.page) {
query.push('page=' + encodeURIComponent(config.page));
}

if (config.hasOwnProperty('itemsPerPage') && config.itemsPerPage) {
query.push('itemsPerPage=' + encodeURIComponent(config.itemsPerPage));
}

if (config.hasOwnProperty('searchTerm') && config.searchTerm) {
query.push('search=' + encodeURIComponent(config.searchTerm));
}

if (config.hasOwnProperty('language') && config.language) {
query.push('language=' + encodeURIComponent(config.language));
} else {
query.push('language=' + encodeURIComponent(this.language));
}

if (config.hasOwnProperty('sort') && config.sort) {
query.push('sort=' + encodeURIComponent(config.sort));
}

if (config.hasOwnProperty('regulation')) {
let regulation = _.merge(
{
size: null,
age: null
},
config.regulation
);

if (regulation.size !== null || regulation.age !== null) {
let size = (/^\d+$/.test(regulation.size)) ? regulation.size : 'null';
let age = (/^\d+$/.test(regulation.age)) ? regulation.age : 'null';

query.push('filter[]=' + encodeURIComponent(`regulation(${size},${age})`));
}
}

if (config.hasOwnProperty('facets')) {
config.facets.forEach(function (facet) {
query.push('facet[]=' + encodeURIComponent(facet));
});
}

if (config.hasOwnProperty('filters')) {
config.filters.forEach(function (filter) {
query.push('filter[]=' + encodeURIComponent(filter));
});
}

if (config.hasOwnProperty('founded') && config.founded !== null && (/^\d{4}$/.test(config.founded))) {
let year = config.founded;
query.push('filter[]=' + encodeURIComponent(`founded.year:${year}`));
}

if (this.withAcl === true) {
query.push('acl=true');
}

return query.length > 0 ? '?' + query.join('&') : '';
}
};
Loading

0 comments on commit 65b4c34

Please sign in to comment.