Skip to content

Commit f48ee83

Browse files
committed
New version is tested
1 parent bb83a1e commit f48ee83

File tree

6 files changed

+110
-72
lines changed

6 files changed

+110
-72
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# directories
2+
node_modules/
3+
4+
# files
5+
Test.*

GeolocationParams.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
module.exports = class GeolocationParams {
22

33
constructor() {
4-
var ipAdress = "";
5-
var fields = "";
6-
var excludes = "";
7-
var lang = "";
8-
var ipAdresses = "";
4+
var ipAdress = '';
5+
var fields = '*';
6+
var excludes = '';
7+
var lang = 'en';
8+
var ipAdresses = [];
99
}
1010

11-
setIPAddress(ipAddress = "") {
11+
setIPAddress(ipAddress = '') {
1212
this.ipAdress = ipAddress;
1313
}
1414

1515
getIPAddress() {
1616
return this.ipAdress;
1717
}
1818

19-
setFields(fields = "") {
19+
setFields(fields = '*') {
2020
this.fields = fields;
2121
}
2222

2323
getFields() {
2424
return this.fields;
2525
}
2626

27-
setExcludes(excludes = "") {
27+
setExcludes(excludes = '') {
2828
this.excludes = excludes;
2929
}
3030

3131
getExcludes() {
3232
return this.excludes;
3333
}
3434

35-
setLang(lang = "en") {
35+
setLang(lang = 'en') {
3636
this.lang = lang;
3737
}
3838

IPGeolocationAPI.js

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,25 @@ var TimezoneParams = require('./TimezoneParams.js');
33

44
module.exports = class IPGeolocationAPI {
55

6-
constructor(apiKey = "", async = true) {
6+
constructor(apiKey = null, async = true) {
77
this.apiKey = apiKey;
88
this.async = async;
99
}
1010

11-
constructor(async = true) {
12-
this.apiKey = "";
13-
this.async = async;
14-
}
15-
1611
getGeolocation(callback, geolocationParams = null) {
1712
if(geolocationParams && geolocationParams.getIPAddresses()) {
1813
var jsonData = JSON.stringify({
19-
"ips": params.getIPAddresses()
14+
'ips': geolocationParams.getIPAddresses()
2015
});
2116

22-
return postRequest("ipgeo-bulk", buildGeolocationUrlParams(this.apiKey, geolocationParams), jsonData, callback);
17+
postRequest('ipgeo-bulk', buildGeolocationUrlParams(this.apiKey, geolocationParams), jsonData, callback);
2318
} else {
24-
return getRequest("ipgeo", buildGeolocationUrlParams(this.apiKey, geolocationParams), callback);
19+
getRequest('ipgeo', buildGeolocationUrlParams(this.apiKey, geolocationParams), callback);
2520
}
2621
}
2722

2823
getTimezone(callback, timezoneParams = null) {
29-
return getRequest("timezone", buildTimezoneUrlParams(this.apiKey, timezoneParams), callback);
24+
return getRequest('timezone', buildTimezoneUrlParams(this.apiKey, timezoneParams), callback);
3025
}
3126

3227
getApiKey() {
@@ -38,120 +33,145 @@ module.exports = class IPGeolocationAPI {
3833
}
3934
}
4035

41-
function buildGeolocationUrlParams(apiKey = "", geolocationParams = null) {
42-
var urlParams = "";
36+
function buildGeolocationUrlParams(apiKey = '', geolocationParams = null) {
37+
var urlParams = '';
4338

4439
if(apiKey) {
45-
urlParams = urlParams.concat("apiKey=", apiKey);
40+
urlParams = urlParams.concat('apiKey=', apiKey);
4641
}
4742

4843
if(geolocationParams) {
4944
if(geolocationParams.getIPAddress()) {
5045
if(urlParams) {
51-
urlParams = urlParams.concat("&");
46+
urlParams = urlParams.concat('&');
5247
}
53-
urlParams = urlParams.concat("ip=", geolocationParams.getIPAddress());
48+
49+
urlParams = urlParams.concat('ip=', geolocationParams.getIPAddress());
5450
}
5551

5652
if(geolocationParams.getFields()) {
5753
if(urlParams) {
58-
urlParams = urlParams.concat("&");
54+
urlParams = urlParams.concat('&');
5955
}
60-
urlParams = urlParams.concat("fields=", geolocationParams.getFields());
56+
57+
urlParams = urlParams.concat('fields=', geolocationParams.getFields());
6158
}
6259

6360
if(geolocationParams.getExcludes()) {
6461
if(urlParams) {
65-
urlParams = urlParams.concat("&");
62+
urlParams = urlParams.concat('&');
6663
}
67-
urlParams = urlParams.concat("excludes=", geolocationParams.getExcludes());
64+
65+
urlParams = urlParams.concat('excludes=', geolocationParams.getExcludes());
6866
}
6967

7068
if(geolocationParams.getLang()) {
7169
if(urlParams) {
72-
urlParams = urlParams.concat("&");
70+
urlParams = urlParams.concat('&');
7371
}
74-
urlParams = urlParams.concat("lang=", geolocationParams.getLang());
72+
73+
urlParams = urlParams.concat('lang=', geolocationParams.getLang());
7574
}
7675
}
7776
return urlParams;
7877
}
7978

80-
function buildTimezoneUrlParams(apiKey = "", timezoneParams = null) {
81-
var urlParams = "";
79+
function buildTimezoneUrlParams(apiKey = '', timezoneParams = null) {
80+
var urlParams = '';
8281

8382
if(apiKey) {
84-
urlParams = urlParams.concat("apiKey=", apiKey);
83+
urlParams = urlParams.concat('apiKey=', apiKey);
8584
}
8685

8786
if(timezoneParams) {
8887
if(timezoneParams.getIPAddress()) {
8988
if(urlParams) {
90-
urlParams = urlParams.concat("&");
89+
urlParams = urlParams.concat('&');
9190
}
92-
urlParams = urlParams.concat("ip=", timezoneParams.getIPAddress());
91+
92+
urlParams = urlParams.concat('ip=', timezoneParams.getIPAddress());
9393
}
9494

9595
if(timezoneParams.getTimezone()) {
9696
if(urlParams) {
97-
urlParams = urlParams.concat("&");
97+
urlParams = urlParams.concat('&');
9898
}
99-
urlParams = urlParams.concat("tz=", timezoneParams.getTimezone());
99+
100+
urlParams = urlParams.concat('tz=', timezoneParams.getTimezone());
100101
}
101102

102-
if(timezoneParams.getLatitude() !== 1000.0 && timezoneParams.getLongitude() !== 1000.0) {
103+
if(timezoneParams.getLatitude() !== '1000.0' && timezoneParams.getLongitude() !== '1000.0') {
103104
if(urlParams) {
104-
urlParams = urlParams.concat("&");
105+
urlParams = urlParams.concat('&');
105106
}
106-
urlParams = urlParams.concat("lat=", timezoneParams.getLatitude(), "&long=", timezoneParams.getLongitude());
107+
108+
urlParams = urlParams.concat('lat=', timezoneParams.getLatitude(), '&long=', timezoneParams.getLongitude());
107109
}
108110

109111
if(timezoneParams.getLang()) {
110112
if(urlParams) {
111-
urlParams = urlParams.concat("&");
113+
urlParams = urlParams.concat('&');
112114
}
113-
urlParams = urlParams.concat("lang=", timezoneParams.getLang());
115+
116+
urlParams = urlParams.concat('lang=', timezoneParams.getLang());
114117
}
115118
}
116119
return urlParams;
117120
}
118121

119-
function getRequest(subUrl = "", urlParams = "", callback) {
120-
var jsonData = null;
121-
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
122-
var xhttp = new XMLHttpRequest();
122+
function getRequest(subUrl, urlParams = '', callback) {
123+
var jsonData = {};
124+
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
125+
var xhr = new XMLHttpRequest();
123126

124-
xhttp.withCredentials = true;
125-
xhttp.onreadystatechange = function() {
127+
xhr.onreadystatechange = function() {
126128
if (this.readyState === 4) {
127-
jsonData = JSON.parse(this.responseText);
129+
if (this.status == 0){
130+
jsonData = {
131+
'message': 'Internet is not connected!'
132+
};
133+
} else {
134+
jsonData = JSON.parse(this.responseText);
135+
}
128136

129137
if (callback && typeof(callback) === typeof(Function)) {
130138
callback(jsonData);
139+
} else {
140+
console.error(`Passed callback '${callback}' is not a valid Function.`)
131141
}
132142
}
133143
};
134-
xhttp.open("GET", "https://api.ipgeolocation.io/".concat(subUrl, "?", urlParams, ""), this.async);
135-
xhttp.send();
144+
xhr.withCredentials = true;
145+
xhr.open('GET', 'https://api.ipgeolocation.io/'.concat(subUrl, '?', urlParams, ''), this.async);
146+
xhr.setRequestHeader('Accept', 'application/json');
147+
xhr.send();
136148
}
137149

138-
function postRequest(subUrl = "", urlParams = "", requestData = "", callback) {
139-
var jsonData = null;
140-
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
141-
var xhttp = new XMLHttpRequest();
150+
function postRequest(subUrl, urlParams = '', requestData = {}, callback) {
151+
var jsonData = {};
152+
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
153+
var xhr = new XMLHttpRequest();
142154

143-
xhttp.withCredentials = true;
144-
xhttp.onreadystatechange = function() {
155+
xhr.onreadystatechange = function() {
145156
if (this.readyState === 4) {
146-
jsonData = JSON.parse(this.responseText);
157+
if (this.status == 0) {
158+
jsonData = {
159+
'message': 'Internet is not connected!'
160+
};
161+
} else {
162+
jsonData = JSON.parse(this.responseText);
163+
}
147164

148165
if (callback && typeof(callback) === typeof(Function)) {
149166
callback(jsonData);
167+
} else {
168+
console.error(`Passed callback '${callback}' is not a valid Function.`)
150169
}
151170
}
152171
};
153-
xhttp.open("POST", "https://api.ipgeolocation.io/".concat(subUrl, "?", urlParams, ""), this.async);
154-
xhttp.setRequestHeader("Content-Type", "application/json");
155-
xhttp.setRequestHeader("Accept", "application/json");
156-
xhttp.send(requestData);
172+
xhr.withCredentials = true;
173+
xhr.open('POST', 'https://api.ipgeolocation.io/'.concat(subUrl, '?', urlParams, ''), this.async);
174+
xhr.setRequestHeader('Content-Type', 'application/json');
175+
xhr.setRequestHeader('Accept', 'application/json');
176+
xhr.send(requestData);
157177
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ $ npm install ip-geolocation-api-javascript-sdk
3636
### Setup API
3737

3838
```javascript
39-
var IPGeolocationAPI = require('ip-geolocation-api-javascript-sdk');
39+
var IPGeolocationAPI = require('ip-geolocation-api-javascript-sdk/IPGeolocationAPI');
4040

4141
// Create IPGeolocationAPI object, passing your valid API key (optional) and async requests mode (optional, default: true)
4242
var ipgeolocationApi = new IPGeolocationAPI("YOUR_API_KEY");

TimezoneParams.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
module.exports = class TimezoneParams {
22

33
constructor() {
4-
var tz = "";
5-
var ipAddress = "";
6-
var lang = "";
7-
var latitude = 1000.0;
8-
var longitude = 1000.0;
4+
var tz = '';
5+
var ipAddress = '';
6+
var lang = 'en';
7+
var latitude = '1000.0';
8+
var longitude = '1000.0';
99
}
1010

11-
setTimezone(tz = "") {
11+
setTimezone(tz = '') {
1212
this.tz = tz;
1313
}
1414

1515
getTimezone() {
1616
return this.tz;
1717
}
1818

19-
setIPAddress(ipAddress = "") {
19+
setIPAddress(ipAddress = '') {
2020
this.ipAdress = ipAddress;
2121
}
2222

2323
getIPAddress() {
2424
return this.ipAdress;
2525
}
2626

27-
setLang(lang = "en") {
27+
setLang(lang = 'en') {
2828
this.lang = lang;
2929
}
3030

3131
getLang() {
3232
return this.lang;
3333
}
3434

35-
setCoordinates(latitude = 1000.0, longitude = 1000.0) {
35+
setCoordinates(latitude = '1000.0', longitude = '1000.0') {
3636
this.latitude = latitude;
3737
this.longitude = longitude;
3838
}

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)