-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
445a5cd
commit 74b10c3
Showing
3 changed files
with
17 additions
and
24 deletions.
There are no files selected for viewing
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
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
35 changes: 11 additions & 24 deletions
35
financius/src/main/java/com/code44/finance/api/currencies/ExchangeRatesResponse.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,31 @@ | ||
package com.code44.finance.api.currencies; | ||
|
||
import com.code44.finance.data.model.ExchangeRate; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.annotations.SerializedName; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class ExchangeRatesResponse { | ||
@SerializedName("query") | ||
private Query query; | ||
private JsonObject query; | ||
|
||
public Set<ExchangeRate> getExchangeRates() { | ||
final Set<ExchangeRate> exchangeRates = new HashSet<>(); | ||
for (Rate rate : query.results.rates) { | ||
final JsonArray jsonArray = query.getAsJsonObject("results").getAsJsonArray("rate"); | ||
for (int i = 0, size = jsonArray.size(); i < size; i++) { | ||
final JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); | ||
final String id = jsonObject.get("id").getAsString(); | ||
final double rate = jsonObject.get("Rate").getAsDouble(); | ||
final ExchangeRate exchangeRate = new ExchangeRate(); | ||
exchangeRate.setFromCode(rate.id.substring(0, 3)); | ||
exchangeRate.setToCode(rate.id.substring(3)); | ||
exchangeRate.setRate(rate.rate); | ||
exchangeRate.setFromCode(id.substring(0, 3)); | ||
exchangeRate.setToCode(id.substring(3)); | ||
exchangeRate.setRate(rate); | ||
exchangeRates.add(exchangeRate); | ||
} | ||
|
||
return exchangeRates; | ||
} | ||
|
||
private static class Query { | ||
@SerializedName("results") | ||
private Results results; | ||
} | ||
|
||
private static class Results { | ||
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection") @SerializedName("rate") | ||
private List<Rate> rates; | ||
} | ||
|
||
private static class Rate { | ||
@SerializedName("id") | ||
private String id; | ||
|
||
@SerializedName("Rate") | ||
private double rate; | ||
} | ||
} |