overleaf/services/web/app/coffee/infrastructure/GeoIpLookup.coffee
Henry Oswald 2e6c2c1926 default to USD in geo ip lookup.
Decided to put default logic in the GeoIpLookup.getCurrencyCode as
we are going to want this default everywhere we use it.
2014-10-13 13:08:11 +01:00

36 lines
No EOL
977 B
CoffeeScript

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)->
if !ip?
e = new Error("no ip passed")
return callback(e)
ip = ip.trim().split(" ")[0]
opts =
url: "#{settings.apis.geoIpLookup.url}/#{ip}"
request.get opts, (err, ipDetails)->
callback(err, ipDetails)
getCurrencyCode : (ip, callback)->
GeoIpLookup.getDetails ip, (err, ipDetails)->
if err? or !ipDetails?
return callback(null, "USD")
currencyCode = currencyMappings[ipDetails?.country_code?.toUpperCase()]
callback(err, currencyCode)