Created
January 29, 2014 01:48
-
-
Save iamjono/8680248 to your computer and use it in GitHub Desktop.
This Lasso 9 data type uses the JSON Webservice of http://www.geoplugin.com/ to geolocate IP addresses. See http://www.geoplugin.com/webservices/ for more specific details of this free service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
// ================================== | |
/* | |
@author Jonathan Guthrie ([email protected]) | |
#version 1.00 | |
This Lasso 9 data type uses the JSON Webservice of http://www.geoplugin.com/ to geolocate IP addresses | |
See http://www.geoplugin.com/webservices/ for more specific details of this free service | |
Usage: | |
local(userLocation = geoPlugin) | |
#userLocation->locate(web_request->remoteAddr) | |
#userLocation->city | |
#userLocation->countryCode | |
... | |
*/ | |
// ================================== | |
define geoPlugin => type { | |
//the geoPlugin server | |
data private host::string = 'http://www.geoplugin.net/json.gp?ip={IP}&base_currency={CURRENCY}' | |
//the default base currency | |
data | |
public currency::string = 'CAD' | |
//initiate the geoPlugin vars | |
data | |
public ip::string = '', | |
public city::string = '', | |
public region::string = '', | |
public areaCode::string = '', | |
public dmaCode::string = '', | |
public countryCode::string = '', | |
public countryName::string = '', | |
public continentCode::string = '', | |
public latitude::decimal = 0.000000, | |
public longitude::decimal = 0.000000, | |
public currencyCode::string = '', | |
public currencySymbol::string = '', | |
public currencyConverter::decimal = 1.000000 | |
public locate(ip::string='') => { | |
#ip->size == 0 ? #ip = string(client_ip) | |
.ip = #ip | |
.host->replace('{IP}',#ip) | |
.host->replace('{CURRENCY}',.currency) | |
local(data = array) | |
local(response = string(include_url(.host))) | |
#response->removeleading('geoPlugin(') | |
#response->removetrailing(')') | |
#response >> 'temporarily blacklisted' ? log_critical(#response) | |
#data = json_deserialize(#response) | |
// //set the geoPlugin vars | |
if(not #data->isA(::void)) => { | |
.city = string(#data->find('geoplugin_city')) | |
.region = string(#data->find('geoplugin_region')) | |
.areaCode = string(#data->find('geoplugin_areaCode')) | |
.dmaCode = string(#data->find('geoplugin_dmaCode')) | |
.countryCode = string(#data->find('geoplugin_countryCode')) | |
.countryName = string(#data->find('geoplugin_countryName')) | |
.continentCode = string(#data->find('geoplugin_continentCode')) | |
.latitude = decimal(#data->find('geoplugin_latitude')) | |
.longitude = decimal(#data->find('geoplugin_longitude')) | |
.currencyCode = string(#data->find('geoplugin_currencyCode')) | |
.currencySymbol = string(#data->find('geoplugin_currencySymbol')) | |
.currencyConverter = decimal(#data->find('geoplugin_currencyConverter')) | |
} | |
} | |
public convert(amount::decimal, float::integer=2, symbol::boolean=true) => { | |
//easily convert amounts to geolocated currency. | |
local(out = decimal(#amount * .currencyConverter)->asString(-precision=#float)) | |
if(#symbol == true) => { | |
return (.currencySymbol + #out) | |
} else { | |
return #out | |
} | |
} | |
public convert(amount::any, float::integer=2, symbol::boolean=true) => { | |
return ('geoPlugin Warning: The amount passed to geoPlugin::convert is not a decimal.') | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment