Skip to content

Commit 76ec887

Browse files
Jeffrey BrownJeffrey Brown
Jeffrey Brown
authored and
Jeffrey Brown
committed
Asteroids - Feed Function
1 parent a7f9f1a commit 76ec887

File tree

5 files changed

+71
-44
lines changed

5 files changed

+71
-44
lines changed

gulpfile.js

100755100644
File mode changed.

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
// export modules
44
module.exports = require('./lib/apod.js')
5+
module.exports = require ('./lib/asteroids.js')

lib/asteroids.js

+43-39
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,67 @@
11
// import libraries //
2-
const axios = require('axios');
3-
const Joi = require('joi');
4-
5-
// define validation schema for payload processing //
6-
const validationSchema = Joi.object({
7-
count: Joi.number().integer(),
8-
date: Joi.date(),
9-
end_date: Joi.date(),
10-
key: Joi.string().required(),
11-
start_date: Joi.date().less(Joi.ref('end_date')),
12-
thumbs: Joi.boolean()
13-
});
14-
15-
// NeoWs (Near Earth Object Web Service) //
16-
function asteroids(payload, callback) {
2+
const axios = require('axios')
3+
const Joi = require('joi')
4+
5+
// import utilities //
6+
const dateUtils = require('../utils/dates.js')
177

18-
// payload logic validation //
19-
if (payload.end_date && !payload.start_date) throw new Error('Start Date must be included if using End Date');
8+
// neows (near earth object web service) //
9+
function asteroids(payload, callback) {
10+
let request, start, end, key, type;
2011

21-
let request;
2212
// neo request type is feed //
2313
if (payload.type == 'feed') {
2414

15+
// define validation schema //
16+
const validationSchema = Joi.object({
17+
end_date: Joi.string().regex(/\d{4}-\d{2}-\d{2}/, { name: 'endDate' }).optional(),
18+
key: Joi.string().required(),
19+
start_date: Joi.string().regex(/\d{4}-\d{2}-\d{2}/, { name: 'startDate' }).optional(),
20+
type: Joi.string().regex(/\w{4,6}/, { name: 'requestType' }).required(),
21+
})
22+
23+
type = payload.type;
24+
key = payload.key;
25+
26+
// validate the payload input //
27+
const { error } = validationSchema.validate(payload)
28+
if (error) throw error
29+
30+
// payload logic validation //
31+
if (payload.end_date && (payload.start_date > payload.end_date)) throw new Error('Start date cannot come after the end date provided')
32+
if (payload.end_date && !payload.start_date) throw new Error('Start Date must be included if using End Date')
33+
2534
// function to add days to a given date //
2635
function addDays(date, days) {
27-
const res = new Date(Number(date));
36+
let res = new Date(date);
2837
res.setDate(date.getDate() + days);
2938
return res;
3039
}
3140

32-
3341
// calculate seven days from the start date //
34-
let endLimit = addDays(new Date(payload.start_date), 7);
42+
let endLimit = addDays(dateUtils.formatStringToDate(payload.start_date), 7);
43+
44+
// if there is no current end date, set the end date to the end limit (seven days from the start date) //
45+
if (!payload.end_date) {
46+
end = endLimit
47+
} else {
48+
end = new Date(payload.end_date);
49+
}
3550

3651
// throw an error if the end date is later than the cutoff date //
37-
if (!(new Date(payload.end_date) < endLimit)) throw new Error ('Start and End dates cannot be more than seven days apart')
38-
39-
// define validation schema //
40-
const validationSchema = Joi.object({
41-
end_date: Joi.date(),
42-
key: Joi.string().required(),
43-
start_date: Joi.date().less(Joi.ref('end_date')),
44-
type: Joi.string()
45-
});
46-
47-
// validate the payload input //
48-
const { error, value } = validationSchema.validate(payload);
49-
if (error) throw error;
50-
51-
// calculate seven days from start_date //
52+
if (end > endLimit) {
53+
throw new Error ('Start and End dates cannot be more than seven days apart')
54+
}
5255

56+
// define url request parameters //
5357
let requestURLParameters = `api_key=${payload.key}`;
5458
if (payload.start_date) requestURLParameters = requestURLParameters.concat(`&start_date=${payload.start_date}`);
5559
if (payload.end_date) requestURLParameters = requestURLParameters.concat(`&end_date=${payload.end_date}`);
5660

61+
// define request url //
5762
request = `https://api.nasa.gov/neo/rest/v1/feed?${requestURLParameters}`;
58-
}
59-
63+
console.log(request)
64+
}
6065

6166
// neo request type is lookup //
6267
else if (payload.type == 'lookup') {
@@ -80,7 +85,6 @@ function asteroids(payload, callback) {
8085
else if (payload.type == 'browse') {
8186
let requestURLParameters = `api_key=${payload.key}`;
8287
request = `https://api.nasa.gov/neo/rest/v1/neo/browse/?${requestURLParameters}`;
83-
8488
}
8589

8690
// api request //

testing.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const celestial = require('./index');
22

3-
// get 10 random results //
3+
// get results for 04/20/2021 //
44
const payload = {
5+
end_date: '2021-04-27',
56
key: 'DEMO_KEY',
6-
count: 10,
7+
start_date: '2021-04-20',
8+
type: 'feed'
79
};
810

9-
celestial.apod(payload, asteroids => {
10-
console.log(asteroids);
11-
});
11+
celestial.asteroids(payload, neows => {
12+
console.log(neows)
13+
return neows;
14+
});

utils/dates.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// global operational functions for controlling the format of dates //
2+
3+
// function to take date as iso string and return in yyyy-mm-dd format //
4+
function formatDateToString(date) {
5+
let year = date.getUTCFullYear()
6+
let month = (date.getUTCMonth() + 1)
7+
let day = date.getUTCDate()
8+
return `${year}-${month}-${day}`
9+
}
10+
11+
// function to take date as yyyy-mm-dd and return in iso format //
12+
function formatStringToDate(date) {
13+
let res = new Date(date);
14+
return res;
15+
}
16+
17+
// export module //
18+
module.exports.formatDateToString = formatDateToString
19+
module.exports.formatStringToDate = formatStringToDate

0 commit comments

Comments
 (0)