@@ -3,30 +3,25 @@ var TimezoneParams = require('./TimezoneParams.js');
33
44module . 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}
0 commit comments