-
Notifications
You must be signed in to change notification settings - Fork 3
/
snippet_park_lookup.js
140 lines (100 loc) · 3.63 KB
/
snippet_park_lookup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* This file contains code snippets for the API calls to
* - NPS (National Park Service)
* - OpenWeather
*/
/* Dependencies */
var request = require("request");
require('dotenv').config();
/* The National Park Service (NPS) API Key */
var parks_api_key = process.env.PARKS_API_KEY;
/**
* Functions
*/
/**
* @function park_lookup_by_code
* @description This function looks up information on the specified national park.
* @param {string} park_code
*/
function park_lookup_by_code(park_code){
/* Ensure that a valid park name was provided */
if(!park_code){
/* To Be Implemented : return error back to client */
return console.log("Please specify a park name");
}
/* Construct the query URL retrieve information on the park */
var query_url = "http://api.nps.gov/api/v1/parks/?parkCode=" + park_code;
/* Append API Key to the query URL */
query_url += ("&apiKey="+parks_api_key);
/* Also ask for images of the park */
query_url += "&fields=images";
console.log("Query URL is " + query_url);
request(query_url, function(error, response, body){
var park_found = false;
var park_info = {};
if(error || response.statusCode != 200){
return console.log("Oops couldn't find the park");
}
var results = JSON.parse(body);
console.log(results.total + " matches found!\n");
console.log(results);
park_info = results.data[0];
/* The specified national park was found. Retrieve its co-ordinates to perform
a weather lookup */
console.log(park_info.latLong);
let coordinates = park_info.latLong.match(/[+-]?\d+(\.\d+)?/g).map(Number);
console.log(coordinates);
let latitude = coordinates[0];
let longitude = coordinates[1];
console.log("*** Park co-ordinates are ***");
console.log("Latitude : " + latitude + " and Longitude : " + longitude);
});
}
/**
* @function park_lookup_by_state
* @description This function looks up all the national parks in the specified state.
* @param {string} state_code
*/
function park_lookup_by_state(state_code){
if(!state_code || typeof(state_code) !== "string" || state_code.length !== 2){
/* return error */
return console.log("Please enter a valid state");
}
/* Construct the query URL retrieve information on parks by state */
var query_url = "http://api.nps.gov/api/v1/parks/?stateCode=" + state_code;
/* Also ask for images of the park */
query_url += "&fields=images";
/* Limit the number of results to 3 */
query_url += "&limit=3";
/* Append API Key to the query URL */
query_url += ("&apiKey="+parks_api_key);
console.log("Query URL is " + query_url);
request(query_url, function(error, response, body){
var park_found = false;
var park_info = {};
if(error || response.statusCode != 200){
return console.log("Oops couldn't find the park");
}
var results = JSON.parse(body);
console.log(results.total + " matches found!\n");
console.log(results);
park_info = results.data[0];
/* The specified national park was found. Retrieve its co-ordinates to perform
a weather lookup */
console.log(park_info.latLong);
let coordinates = park_info.latLong.match(/[+-]?\d+(\.\d+)?/g).map(Number);
console.log(coordinates);
let latitude = coordinates[0];
let longitude = coordinates[1];
console.log("*** Park co-ordinates are ***");
console.log("Latitude : " + latitude + " and Longitude : " + longitude);
});
}
/* Test the 'lookup by park code' function by looking up Denali (DENA) */
park_lookup_by_code("DENA");
/* Test the 'lookup by state' function by looking up parks in Minnesote (MN) */
park_lookup_by_state("MN");
module.exports = {
state_lookup : park_lookup_by_state,
parkid_lookup : park_lookup_by_code
}