overleaf/services/web/app/coffee/infrastructure/GeoIpLookup.coffee

53 lines
1.5 KiB
CoffeeScript
Raw Normal View History

2014-10-12 19:45:45 -04:00
request = require("request")
settings = require("settings-sharelatex")
_ = require("underscore")
logger = require("logger-sharelatex")
URL = require("url")
2014-10-12 19:45:45 -04:00
currencyMappings = {
"GB":"GBP"
"US":"USD"
"CH":"CHF"
"NZ":"NZD"
"AU":"AUD"
"DK":"DKK"
"NO":"NOK"
"CA":"CAD"
"SE":"SEK"
2014-10-12 19:45:45 -04:00
}
# Countries which would likely prefer Euro's
EuroCountries = ["AT", "BE", "BG", "HR", "CY", "CZ",
"EE", "FI", "FR", "DE", "EL", "HU", "IE",
2014-10-12 19:45:45 -04:00
"IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT",
"RO", "SK", "SI", "ES"]
2014-10-12 19:45:45 -04:00
_.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]
2014-10-12 19:45:45 -04:00
opts =
url: URL.resolve(settings.apis.geoIpLookup.url,ip)
timeout: 1000
json:true
logger.log ip:ip, opts:opts, "getting geo ip details"
request.get opts, (err, res, ipDetails)->
if err?
logger.err err:err, ip:ip, "error getting ip details"
2014-10-12 19:45:45 -04:00
callback(err, ipDetails)
getCurrencyCode : (ip, callback)->
GeoIpLookup.getDetails ip, (err, ipDetails)->
if err? or !ipDetails?
logger.err err:err, ip:ip, "problem getting currencyCode for ip, defaulting to USD"
return callback(null, "USD")
countryCode = ipDetails?.country_code?.toUpperCase()
2014-10-14 07:14:03 -04:00
currencyCode = currencyMappings[countryCode] || "USD"
logger.log ip:ip, currencyCode:currencyCode, ipDetails:ipDetails, "got currencyCode for ip"
callback(err, currencyCode, countryCode)