Last active
December 19, 2015 05:20
-
-
Save TylerMills/864fca3547b859b651b9 to your computer and use it in GitHub Desktop.
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
/* | |
Requires Unirest. Available via Maven/Gradle: http://mvnrepository.com/artifact/com.mashape.unirest/unirest-java/1.3.0 | |
*/ | |
import com.mashape.unirest.http.*; | |
import com.mashape.unirest.http.async.Callback; | |
import com.mashape.unirest.http.exceptions.UnirestException; | |
import org.json.JSONObject; | |
import java.util.concurrent.Future; | |
public class LLCurrencyConversion { | |
//in main method for proof of concept, you can wrap this in your own method to pass params into | |
public static void main (String args []) { | |
final double value = 19.99; // value of event in original currency | |
String base = "GBP"; // currency to convert into USD | |
String url = "https://api.fixer.io/latest" + | |
"?base=" + base + | |
"&symbols=USD"; | |
System.out.println(url); | |
// using future to be non-blocking when making request | |
Future<HttpResponse<JsonNode>> future = Unirest.get(url) | |
.asJsonAsync(new Callback<JsonNode>() { | |
public void failed(UnirestException e) { | |
System.out.println("The request has failed"); | |
} | |
public void completed(HttpResponse<JsonNode> response) { | |
System.out.println("The request has been successful"); | |
//parse out the conversion multiplier | |
JSONObject myObj = response.getBody().getObject(); | |
JSONObject rateObject = myObj.getJSONObject("rates"); | |
String rate = rateObject.get("USD").toString(); | |
//convert to numerical value | |
double multiplier = Double.parseDouble(rate); | |
//multiply by base currency rate and convert to US cents | |
Double convertedValue = (multiplier * value * 100); | |
Map<String, String> values = new HashMap<String, String>(); | |
values.put("Item Name", "T-Shirt"); | |
Localytics.tagEvent("Item Purchased", values, convertedValue); | |
} | |
public void cancelled() { | |
System.out.println("The request has been cancelled"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment