added geoip lookup feature

This commit is contained in:
Henry Oswald 2014-10-13 00:45:45 +01:00
parent 40c3c3e1ce
commit 259871cbdd
2 changed files with 108 additions and 0 deletions

View file

@ -0,0 +1,29 @@
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)->
opts =
url: "#{settings.apis.geoIpLookup.url}/#{ip}"
request.get opts, (err, ipDetails)->
callback(err, ipDetails)
getCurrencyCode : (ip, callback)->
GeoIpLookup.getDetails ip, (err, ipDetails)->
currencyCode = currencyMappings[ipDetails?.country_code?.toUpperCase()]
callback(err, currencyCode)

View file

@ -0,0 +1,79 @@
should = require('chai').should()
SandboxedModule = require('sandboxed-module')
assert = require('assert')
path = require('path')
sinon = require('sinon')
modulePath = path.join __dirname, "../../../../app/js/infrastructure/GeoIpLookup"
expect = require("chai").expect
describe "GeoIpLookup", ->
beforeEach ->
@settings =
apis:
geoIpLookup:
url:"http://lookup.com"
@request =
get: sinon.stub()
@GeoIpLookup = SandboxedModule.require modulePath, requires:
"request": @request
"settings-sharelatex":@settings
"logger-sharelatex": log:->
@ipAddress = "123.456.789.123"
@stubbedResponse =
"ip":@ipAddress
"country_code":"GB"
"country_name":"United Kingdom"
"region_code":"H9"
"region_name":"London, City of"
"city":"London"
"zipcode":"SE16"
"latitude":51.0
"longitude":-0.0493
"metro_code":""
"area_code":""
describe "getDetails", ->
beforeEach ->
@request.get.callsArgWith(1, null, @stubbedResponse)
it "should request the details using the ip", (done)->
@GeoIpLookup.getDetails @ipAddress, (err)=>
@request.get.calledWith(url:@settings.apis.geoIpLookup.url+"/"+@ipAddress).should.equal true
done()
it "should return the ip details", (done)->
@GeoIpLookup.getDetails @ipAddress, (err, returnedDetails)=>
assert.deepEqual returnedDetails, @stubbedResponse
done()
describe "getCurrencyCode", ->
it "should return GBP for GB country", (done)->
@GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
@GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
currencyCode.should.equal "GBP"
done()
it "should return GBP for gb country", (done)->
@stubbedResponse.country_code = "gb"
@GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
@GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
currencyCode.should.equal "GBP"
done()
it "should return USD for US", (done)->
@stubbedResponse.country_code = "US"
@GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
@GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
currencyCode.should.equal "USD"
done()
it "should return EUR for DE", (done)->
@stubbedResponse.country_code = "DE"
@GeoIpLookup.getDetails = sinon.stub().callsArgWith(1, null, @stubbedResponse)
@GeoIpLookup.getCurrencyCode @ipAddress, (err, currencyCode)->
currencyCode.should.equal "EUR"
done()