Doing some basic refactoring to make the code nicer.

Removed all methods we weren't user anymore.
This commit is contained in:
Sergio Díaz 2017-02-13 18:05:47 -06:00
parent 40f17dccaf
commit b033031a43
5 changed files with 33 additions and 38 deletions

View File

@ -33,7 +33,6 @@ describe('Request Cache', () => {
});
});
describe('parse', () => {
it('should transform variables in strings using it\'s cache', () => {
expect(cache.parse('Hello $session.hello')).toBe('Hello World');
@ -56,18 +55,14 @@ describe('Request Cache', () => {
it('should parse arrays as well', () => {
let parsed = cache.parse({ hello: '$array.0.name' })
expect(parsed.hello).toBe('Sergio');
console.log([parsed]);
});
});
describe('safely', () => {
it('should return an object when given an undefined value', () => {
expect(Object.keys(cache.safely(undefined)).length).toBe(0)
expect(Object.keys(cache.parse(undefined)).length).toBe(0)
});
it('should parse any value other than undefined', () => {
expect(cache.safely('Hello $session.hello')).toBe('Hello World');
expect(cache.parse('Hello $session.hello')).toBe('Hello World');
});
});
});

View File

@ -88,6 +88,7 @@ if (program.request) {
process.exit(0);
}).catch(function(err) {
new Line().output();
console.error(err);
process.exit(1);
});

View File

@ -47,21 +47,15 @@ class Request {
}
exec(list = new RequestList(), cache = new RequestCache()) {
let dependencies = [];
if (this.$dependencies.size > 0) {
dependencies = Array.from(this.$dependencies).map(dep => {
return list.execByAlias(dep);
});
}
let dependencies = Array.from(this.$dependencies).map(dep => list.execByAlias(dep));
return Promise.all(dependencies).then(() => {
let endpoint = cache.parse(this.$endpoint);
let request = unirest(this.$verb, endpoint);
request.headers(cache.safely(this.$headers));
request.query(cache.safely(this.$params));
request.send(cache.safely(this.$payload));
request.headers(cache.parse(this.$headers));
request.query(cache.parse(this.$params));
request.send(cache.parse(this.$payload));
return new Promise((resolve, reject) => {
request.end(res => {
@ -78,10 +72,7 @@ class Request {
}
};
if (typeof this.$alias !== 'undefined') {
cache.add(this.$alias, results);
}
resolve(results);
});

View File

@ -18,28 +18,33 @@ class RequestCache {
});
if (typeof result === 'undefined') {
throw new Error('Key not found in cache: ', path);
throw new Error(`${path} not found in cache: `, path);
}
return result;
}
safely(val) {
if (typeof val === 'undefined')
return {};
parse(item) {
let type = typeof item;
return this.parse(val);
if (type === 'undefined') {
return {};
}
parse(item) {
if (typeof item === 'string') {
if (item === null) {
return null;
}
if (type === 'string') {
return item.replace(replacementRegex, key => this.get(key));
} else if(typeof item === 'object' && item !== null) {
}
if (type === 'object') {
Object.keys(item).forEach(k => item[k] = this.parse(item[k]));
return item;
} else {
return item;
}
return item;
}
}

View File

@ -8,10 +8,6 @@ class RequestList {
this.cache = new RequestCache();
}
exec(request) {
return request.exec(this, this.cache);
}
execByAlias(alias) {
let request = this.list.find(r => r.$alias === alias);
@ -19,7 +15,14 @@ class RequestList {
return Promise.reject(`${alias} not found among the requests.`);
}
return this.exec(request);
return request
.exec(this, this.cache)
.catch(reason => {
return Promise.reject(`${request.$verb} ${request.$endpoint} FAILED.
Dependencies not met:
${reason}
`);
});
}
loadRequests(doc) {