-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
110 lines (97 loc) · 3.93 KB
/
index.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
'use strict';
const req = require('request');
const moment = require('moment');
const queryString = require('query-string');
class OpenWeather {
/* This function creates an OpenWeather object after taking an API key
* @class OpenWeather
* @param {String} apiKey This is your apiKey generated by Google.
*/
constructor(apiKey) {
this.apiKey = apiKey;
this.loc = '';
this.query = {}
this.exclude = "&exclude=";
this.units = "&units=";
this.lang = "&lang=";
this.mode = "";
}
/* This function is used to search for a specific location.
* @param {String} city This is a specific city.
* @return {OpenWeather} this Returns the object itself to allow for chaining dot operators.
*/
location(city) {
!city ? null : this.loc = city;
return this;
}
/* This function establishes if the output should be in xml format.
* @param {Boolean} bool Either true or false.
* @return {OpenWeather} this Returns the object itself to allow for chaining dot operators.
*/
xmlmode(bool) {
!bool ? null : this.mode += "&mode=xml";
return this;
}
/* This function is used to set time units. It is currently not in use.
* @param {String} val This is a specific unit of time.
* @return {OpenWeather} this Returns the object itself to allow for chaining dot operators.
*/
time(val) {
!val ? this.time = 'hourly/1hour' : this.time = val;
return this;
}
/* This function establishes which language the user wants the output to be in.
* @param {String} lan This is a specific language written in 2 letter shorthand.
* @return {OpenWeather} this Returns the object itself to allow for chaining dot operators.
*/
language(lan) {
// String indicating the language in which to return the resource.
// Default value set to en-us.
!lan ? null : this.lang += lan;
return this;
}
/* This function establishes if the output should follow metric units.
* @param {Boolean} bool Either true or false.
* @return {OpenWeather} this Returns the object itself to allow for chaining dot operators.
*/
metric(bool) {
!bool ? null : this.units += "metric";
return this;
}
/* This function establishes if the output should follow imperial units.
* @param {Boolean} bool Either true or false.
* @return {OpenWeather} this Returns the object itself to allow for chaining dot operators.
*/
imperial(bool) {
!bool ? null : this.units += "imperial";
return this;
}
/* This function specifies whether or not to include the full JSON object in the API output.
* @param {Boolean} bool Either true or false. Defaults to false.
* @return {OpenWeather} this Returns the object itself to allow for chaining dot operators.
*/
details(bool) {
!bool ? null : this.query.details = bool;
return this;
}
/* This function generates the request URL.
*/
generateReqUrl() {
this.url = `https://api.openweathermap.org/data/2.5/weather?q=${this.loc}&appid=${this.apiKey}${this.units}${this.lang}${this.mode}`;
this.query ? this.url += `&${queryString.stringify(this.query)}` : this.url;
}
/* This function creates and completes the get request, using Promise to deal with the HTTP request.
* @return {JSON or Error} (no identifier) Returns the output of the request.
*/
get() {
return new Promise((resolve, reject) => {
this.generateReqUrl();
req({ url: this.url, json: true }, (err, res, body) => {
err ? reject(`Forecast cannot be retrieved. ERROR: ${err}`) : null;
res.statusCode !== 200 ? reject(`Forecast cannot be retrieved. Response: ${res.statusCode} ${res.statusMessage}`) : null;
resolve(body)
})
})
}
}
module.exports = OpenWeather;