2014-10-12 19:45:45 -04:00
|
|
|
request = require("request")
|
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
_ = require("underscore")
|
|
|
|
|
|
|
|
currencyMappings = {
|
|
|
|
"GB":"GBP"
|
|
|
|
"US":"USD"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Countries which would likely prefer Euro's
|
|
|
|
EuroCountries = ["AT", "BE", "BG", "HR", "CY", "CZ",
|
|
|
|
"DK", "EE", "FI", "FR", "DE", "EL", "HU", "IE",
|
|
|
|
"IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT",
|
|
|
|
"RO", "SK", "SI", "ES", "SE"]
|
|
|
|
|
|
|
|
_.each EuroCountries, (country)-> currencyMappings[country] = "EUR"
|
|
|
|
|
|
|
|
module.exports = GeoIpLookup =
|
|
|
|
|
|
|
|
getDetails : (ip, callback)->
|
2014-10-13 08:04:20 -04:00
|
|
|
if !ip?
|
|
|
|
e = new Error("no ip passed")
|
|
|
|
return callback(e)
|
|
|
|
ip = ip.trim().split(" ")[0]
|
|
|
|
|
2014-10-12 19:45:45 -04:00
|
|
|
opts =
|
|
|
|
url: "#{settings.apis.geoIpLookup.url}/#{ip}"
|
2014-10-13 08:15:48 -04:00
|
|
|
timeout: 1000
|
2014-10-12 19:45:45 -04:00
|
|
|
request.get opts, (err, ipDetails)->
|
|
|
|
callback(err, ipDetails)
|
|
|
|
|
|
|
|
getCurrencyCode : (ip, callback)->
|
|
|
|
GeoIpLookup.getDetails ip, (err, ipDetails)->
|
2014-10-13 08:08:11 -04:00
|
|
|
if err? or !ipDetails?
|
|
|
|
return callback(null, "USD")
|
2014-10-12 19:45:45 -04:00
|
|
|
currencyCode = currencyMappings[ipDetails?.country_code?.toUpperCase()]
|
|
|
|
callback(err, currencyCode)
|