Skip to content

Commit

Permalink
Add prettier formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshopkins committed Jan 29, 2017
1 parent cdd32b8 commit db2378d
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 122 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"coverage": "nyc yarn test && nyc report",
"coverage:coveralls": "yarn coverage -- --reporter=text-lcov | coveralls",
"prepublish": "yarn compile",
"format": "prettier --write --single-quote src/**/*.js",
"test": "API_KEY=123 mocha $(find . -name '*spec.js' -not -path './node_modules/*') --compilers js:babel-register"
},
"files": [
Expand Down Expand Up @@ -39,6 +40,7 @@
"mocha": "^3.2.0",
"nyc": "^10.0.0",
"power-assert": "^1.4.2",
"prettier": "^0.13.1",
"sinon": "^1.17.7"
}
}
2 changes: 1 addition & 1 deletion src/error/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class extends Error {
constructor (message, config) {
constructor(message, config) {
super(message);
this.message = message;
this.config = config;
Expand Down
7 changes: 2 additions & 5 deletions src/error/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ describe('API: Error', () => {
const error = new ApiError('This is an error message!', {
foo: 'bar',
moo: 'cow'
})
assert.equal(error.message, 'This is an error message!');
assert.deepEqual(error.config, {
foo: 'bar',
moo: 'cow'
});
assert.equal(error.message, 'This is an error message!');
assert.deepEqual(error.config, { foo: 'bar', moo: 'cow' });
assert(error instanceof Error);
});
});
33 changes: 24 additions & 9 deletions src/polling/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import Future, { after, of } from 'fluture';
import { chain, compose, lensIndex, lensProp, map, partial, path, prop, propEq, view } from 'ramda';
import {
chain,
compose,
lensIndex,
lensProp,
map,
partial,
path,
prop,
propEq,
view
} from 'ramda';
import { Nothing } from 'sanctuary';

import { getJson, makeRequest} from '../utils';
import { getJson, makeRequest } from '../utils';

const handleResponse = chain(res => {
if (res.status === 304) {
Expand All @@ -13,22 +24,26 @@ const handleResponse = chain(res => {

const poll = compose(handleResponse, makeRequest());

export const resolvePollRes = (rej, res, fetchData, url, data, timeout = setTimeout) => {
export const resolvePollRes = (
rej,
res,
fetchData,
url,
data,
timeout = setTimeout
) => {
if (propEq('Status', 'UpdatesComplete', data)) {
console.log('Query result complete');
res(data);
} else {
console.log('Query result incomplete. Retrying...')
console.log('Query result incomplete. Retrying...');
timeout(() => fetchData(url)(rej, res), 1000);
}
}
};

export const fetchData = (url, pollEndpoint = poll, pollRes = resolvePollRes) =>
(rej, res) => {
pollEndpoint(url).fork(
rej,
partial(pollRes, [rej, res, fetchData, url])
)
pollEndpoint(url).fork(rej, partial(pollRes, [rej, res, fetchData, url]));
};

export default chain(compose(Future, fetchData));
24 changes: 7 additions & 17 deletions src/polling/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ describe('API: Polling', () => {
const poll = () => of({ some: 'data' });
const pollRes = spy();
fetchData('http://moo', poll, pollRes)(rej, res);
assert(pollRes.calledWith(
rej,
res,
fetchData,
'http://moo',
{ some: 'data' }
))
assert(
pollRes.calledWith(rej, res, fetchData, 'http://moo', { some: 'data' })
);
});
});

Expand All @@ -35,9 +31,7 @@ describe('API: Polling', () => {
const rej = spy();
const res = spy();
const fetchData = spy();
const data = {
Status: 'UpdatesComplete'
};
const data = { Status: 'UpdatesComplete' };

const result = resolvePollRes(rej, res, fetchData, 'url', data);
assert(res.calledWith(data));
Expand All @@ -49,9 +43,7 @@ describe('API: Polling', () => {
const res = () => {};
const fetchData = () => {};
const timeout = spy();
const data = {
Status: 'UpdatesPending'
};
const data = { Status: 'UpdatesPending' };

const result = resolvePollRes(
rej,
Expand All @@ -71,9 +63,7 @@ describe('API: Polling', () => {
const fetchData2 = spy();
const fetchData = spy(() => fetchData2);
const timeout = spy();
const data = {
Status: 'UpdatesPending'
};
const data = { Status: 'UpdatesPending' };

const result = resolvePollRes(
rej,
Expand All @@ -86,7 +76,7 @@ describe('API: Polling', () => {

timeout.args[0][0]();
assert(fetchData.calledWith('url'));
assert(fetchData2.calledWith(rej, res))
assert(fetchData2.calledWith(rej, res));
});
});
});
Expand Down
64 changes: 40 additions & 24 deletions src/session/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import { after } from 'fluture';
import { always, chain, compose, lensProp, map, merge, over, partial, prop, tap } from 'ramda';
import {
always,
chain,
compose,
lensProp,
map,
merge,
over,
partial,
prop,
tap
} from 'ramda';
import { stringify as formatQuerystring } from 'querystring';
import { format as formatUrl, parse as parseUrl } from 'url';

import { authenticateUrl, log, makeRequest, makeUrl } from '../utils';

const logSession = log(always('Session now accessible \n'));

const logDelay = log(always(`Temporarily witholding session access to due to https://support.business.skyscanner.net/hc/en-us/articles/211308489-Flights-Live-Pricing?_ga=1.109063173.1468313731.1483528061#poll \n`));
const logDelay = log(
always(
`Temporarily witholding session access to due to https://support.business.skyscanner.net/hc/en-us/articles/211308489-Flights-Live-Pricing?_ga=1.109063173.1468313731.1483528061#poll \n`
)
);

const delayResponse = compose(logSession, chain(partial(after, [1000])), logDelay);
const delayResponse = compose(
logSession,
chain(partial(after, [1000])),
logDelay
);

const addUrlAuthentication = compose(formatUrl, authenticateUrl, parseUrl);

const getPollLocation = map(compose(
addUrlAuthentication,
headers => headers.get('location'),
prop(['headers'])
));
const getPollLocation = map(
compose(
addUrlAuthentication,
headers => headers.get('location'),
prop(['headers'])
)
);

export const createRequestConfig = query => {
const config = {
Expand All @@ -27,22 +48,17 @@ export const createRequestConfig = query => {
locale: 'en-GB',
locationschema: 'sky'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST'
};
return over(lensProp('body'), compose(formatQuerystring, merge(query)), config);
}
return over(
lensProp('body'),
compose(formatQuerystring, merge(query)),
config
);
};

const requestSession = compose(
makeRequest,
createRequestConfig
);
const requestSession = compose(makeRequest, createRequestConfig);

export default query => compose(
delayResponse,
getPollLocation,
requestSession(query),
makeUrl
)();
export default query =>
compose(delayResponse, getPollLocation, requestSession(query), makeUrl)();
6 changes: 2 additions & 4 deletions src/session/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ describe('API: Session', () => {
country: 'UK',
currency: 'GBP',
locale: 'en-GB',
locationschema: 'sky',
locationschema: 'sky'
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST'
};

Expand Down
51 changes: 26 additions & 25 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,37 @@ import { format as formatUrl } from 'url';
import ApiError from '../error';

const urlAuthentication = apiKey =>
over(lensProp('query'), merge({ apikey: apiKey }))
over(lensProp('query'), merge({ apikey: apiKey }));

const addPath = path => set(lensProp('pathname'), `apiservices/pricing/v1.0/${path || ''}`);
const addPath = path =>
set(lensProp('pathname'), `apiservices/pricing/v1.0/${path || ''}`);

export const authenticateUrl = urlAuthentication(process.env.API_KEY);

export const log = cb => map(tap(compose(console.log, cb)))
export const log = cb => map(tap(compose(console.log, cb)));

export const makeUrl = path => compose(
formatUrl, authenticateUrl, addPath(path))
({
protocol: 'http',
host: 'api.skyscanner.net'
});

export const makeRequest = config => (url, fetch = isomorphicFetch) =>
Future((rej, res) => {
(async () => {
const result = await fetch(url, config);
if (!result.ok && result.status !== 304) {
return rej(new ApiError(
'The status returned isn\'t within the 200-299 range or a 304',
{
status: result.status,
url
}
));
}
res(result);
})()
export const makeUrl = path =>
compose(formatUrl, authenticateUrl, addPath(path))({
protocol: 'http',
host: 'api.skyscanner.net'
});

export const makeRequest = config => (url, fetch = isomorphicFetch) => Future((
rej,
res
) => {
(async () => {
const result = await fetch(url, config);
if (!result.ok && result.status !== 304) {
return rej(
new ApiError(
"The status returned isn't within the 200-299 range or a 304",
{ status: result.status, url }
)
);
}
res(result);
})();
});

export const getJson = res => fromPromise(() => res.json(), 0);
Loading

0 comments on commit db2378d

Please sign in to comment.