Skip to content

Commit b167b40

Browse files
committed
Built _ipgeolocation namespace and put all the public methods under it make the SDK work under _ipgeolocation namespace avoiding contaminating the global JavaScript workspaces
1 parent 49bac5d commit b167b40

File tree

1 file changed

+116
-121
lines changed

1 file changed

+116
-121
lines changed

ipgeolocation.js

Lines changed: 116 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,127 @@
1-
var ess = false;
2-
var asyncParameter = true;
3-
var ipAddressParameter = "";
4-
var excludesParameter = "";
5-
var fieldsParameter = "";
6-
var langParameter = "";
7-
var tzParameter = "";
8-
var latitudeParameter = "";
9-
var longitudeParameter = "";
10-
11-
function enableSessionStorage(enable = false) {
12-
ess = enable;
13-
}
14-
15-
function setAsync(async = true) {
16-
asyncParameter = async;
17-
}
18-
19-
function setIPAddressParameter(ip = "") {
20-
ipAddressParameter = ip;
21-
}
22-
23-
function setExcludesParameter(excludes = "") {
24-
excludesParameter = excludes;
25-
}
26-
27-
function setFieldsParameter(fields = "") {
28-
fieldsParameter = fields;
29-
}
30-
31-
function setLanguageParameter(lang = "") {
32-
langParameter = lang;
33-
}
34-
35-
function setTimezoneParameter(tz = "") {
36-
tzParameter = tz;
37-
}
38-
39-
function setCoordinatesParameter(latitude = "", longitude = "") {
40-
latitudeParameter = latitude;
41-
longitudeParameter = longitude;
42-
}
43-
44-
function getGeolocation(callback, apiKey = "") {
45-
request("ipgeo", callback, apiKey);
46-
}
47-
48-
function getTimezone(callback, apikey = "") {
49-
request("timezone", callback, apikey);
50-
}
51-
52-
function addUrlParameter(parameters, parameterName, parameterValue) {
53-
if (parameters) {
54-
parameters = parameters.concat("&", parameterName, "=", parameterValue);
55-
} else {
56-
parameters = "?".concat(parameterName, "=", parameterValue);
57-
}
1+
var _ipgeolocation = function() {
2+
var useSessionStorage = false;
3+
var asyncCall = true;
4+
var ipAddress = "";
5+
var excludes = "";
6+
var fields = "";
7+
var lang = "en";
8+
var tz = "";
9+
var latitude = "";
10+
var longitude = "";
11+
12+
function request(subUrl, callback, apiKey = "") {
13+
if (useSessionStorage) {
14+
if (subUrl == "ipgeo" && sessionStorage.getItem("_ipGeolocation") && callback) {
15+
callback(JSON.parse(sessionStorage.getItem("_ipGeolocation")));
16+
} else if (subUrl == "timezone" && sessionStorage.getItem("_ipTimeZone") && callback) {
17+
callback(JSON.parse(sessionStorage.getItem("_ipTimeZone")));
18+
}
19+
}
5820

59-
return parameters;
60-
}
61-
62-
function request(subUrl, callback, apiKey = "") {
63-
if (ess) {
64-
if (subUrl == "ipgeo" && sessionStorage.getItem("_ipGeolocation") && callback) {
65-
callback(JSON.parse(sessionStorage.getItem("_ipGeolocation")));
66-
} else if (subUrl == "timezone" && sessionStorage.getItem("_ipTimeZone") && callback) {
67-
callback(JSON.parse(sessionStorage.getItem("_ipTimeZone")));
21+
var urlParameters = "";
22+
23+
if (!subUrl) {
24+
callback(JSON.parse("{'status': 401, message: 'Given path to IP Geolocation API is not valid'}"));
25+
return;
26+
}
27+
28+
if (apiKey) {
29+
urlParameters = addUrlParameter(urlParameters, "apiKey", apiKey);
6830
}
69-
}
70-
71-
var urlParameters = "";
72-
73-
if (!subUrl) {
74-
callback(JSON.parse("{'status': 401, message: 'Given path to IP Geolocation API is not valid'}"));
75-
return;
76-
}
7731

78-
if (apiKey) {
79-
urlParameters = addUrlParameter(urlParameters, "apiKey", apiKey);
80-
}
81-
82-
if (excludesParameter) {
83-
urlParameters = addUrlParameter(urlParameters, "excludes", excludesParameter);
84-
}
32+
if (ipAddress) {
33+
urlParameters = addUrlParameter(urlParameters, "ip", ipAddress);
34+
}
35+
36+
if (fields) {
37+
urlParameters = addUrlParameter(urlParameters, "fields", fields);
38+
}
39+
40+
if (excludes) {
41+
urlParameters = addUrlParameter(urlParameters, "excludes", excludes);
42+
}
43+
44+
if (lang) {
45+
urlParameters = addUrlParameter(urlParameters, "lang", lang);
46+
}
8547

86-
if (fieldsParameter) {
87-
urlParameters = addUrlParameter(urlParameters, "fields", fieldsParameter);
88-
}
89-
90-
if (ipAddressParameter) {
91-
urlParameters = addUrlParameter(urlParameters, "ip", ipAddressParameter);
92-
}
93-
94-
if (langParameter) {
95-
urlParameters = addUrlParameter(urlParameters, "lang", langParameter);
96-
}
48+
if (tz) {
49+
urlParameters = addUrlParameter(urlParameters, "tz", tz);
50+
}
51+
52+
if (latitude && longitude) {
53+
urlParameters = addUrlParameter(urlParameters, "lat", latitude);
54+
urlParameters = addUrlParameter(urlParameters, "long", longitude);
55+
}
9756

98-
if (tzParameter) {
99-
urlParameters = addUrlParameter(urlParameters, "tz", tzParameter);
100-
}
57+
$.ajax ({
58+
async: asyncCall,
59+
method: "GET",
60+
url: "https://api.ipgeolocation.io/".concat(subUrl, urlParameters, ""),
61+
contentType: "application/json",
62+
dataType: "json",
63+
success: function (result, status, xhr) {
64+
if (useSessionStorage) {
65+
if (subUrl == "ipgeo") {
66+
sessionStorage.setItem("_ipGeolocation", JSON.stringify(result));
67+
} else if (subUrl == "timezone") {
68+
sessionStorage.setItem("_ipTimeZone", JSON.stringify(result));
69+
}
70+
}
10171

102-
if (latitudeParameter && longitudeParameter) {
103-
urlParameters = addUrlParameter(urlParameters, "lat", latitudeParameter);
104-
urlParameters = addUrlParameter(urlParameters, "long", longitudeParameter);
105-
}
106-
107-
$.ajax ({
108-
async: asyncParameter,
109-
method: "GET",
110-
url: "https://api.ipgeolocation.io/".concat(subUrl, urlParameters, ""),
111-
contentType: "application/json",
112-
dataType: "json",
113-
success: function (result, status, xhr) {
114-
if (ess) {
115-
if (subUrl == "ipgeo") {
116-
sessionStorage.setItem("_ipGeolocation", JSON.stringify(result));
117-
} else if (subUrl == "timezone") {
118-
sessionStorage.setItem("_ipTimeZone", JSON.stringify(result));
72+
if (callback) {
73+
callback(result);
74+
}
75+
},
76+
error: function (xhr, status, error) {
77+
if (callback) {
78+
callback(JSON.parse(xhr.responseText));
11979
}
12080
}
81+
});
82+
}
12183

122-
if (callback) {
123-
callback(result);
124-
}
84+
function addUrlParameter(parameters, parameterName, parameterValue) {
85+
if (parameters) {
86+
parameters = parameters.concat("&", parameterName, "=", parameterValue);
87+
} else {
88+
parameters = "?".concat(parameterName, "=", parameterValue);
89+
}
90+
91+
return parameters;
92+
}
93+
94+
return {
95+
enableSessionStorage: function(e = false) {
96+
useSessionStorage = e;
12597
},
126-
error: function (xhr, status, error) {
127-
if (callback) {
128-
callback(JSON.parse(xhr.responseText));
129-
}
98+
makeAsyncCallsToAPI: function(a = true) {
99+
asyncCall = a;
100+
},
101+
setIPAddress: function(ip = "") {
102+
ipAddress = ip;
103+
},
104+
setFields: function(f = "") {
105+
fields = f;
106+
},
107+
setExcludes: function(e = "") {
108+
excludes = e;
109+
},
110+
setLanguage: function(l = "en") {
111+
lang = l;
112+
},
113+
setTimeZone: function(t = "") {
114+
tz = t;
115+
},
116+
setCoordinates: function(latitude = "", longitude = "") {
117+
latitudeParameter = latitude;
118+
longitudeParameter = longitude;
119+
},
120+
getGeolocation: function(callback, apiKey = "") {
121+
request("ipgeo", callback, apiKey);
122+
},
123+
getTimezone: function(callback, apikey = "") {
124+
request("timezone", callback, apikey);
130125
}
131-
});
132-
}
126+
}
127+
}();

0 commit comments

Comments
 (0)