Skip to content

Commit

Permalink
feat: add alerts support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasanchez committed Jun 8, 2021
1 parent 78ac0d8 commit 7eb2438
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public List<Map<String, Object>> getWeather(String ciudad) {
}
});
}

public Map<String, Object> getAlerts(String ciudad) {
return new HashMap<String, Object>() {
{
put("CurrentAlerts", new String[] {"STORM", "HAIL"});
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package quemepongo.api.clima.accuweather;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import quemepongo.model.clima.AlertaClimatica;
import quemepongo.model.clima.CondicionClimatica;
import quemepongo.model.clima.GradoTemperatura;
import quemepongo.model.clima.Temperatura;
Expand All @@ -16,7 +19,8 @@
public class AccuWeatherAdapter implements ServicioMetereologico {

private AccuWeatherAPI api;
private List<Map<String, Object>> data;
private List<Map<String, Object>> climaticData;
private Map<String, Object> alertsData;

public AccuWeatherAdapter() {
this(new AccuWeatherAPI());
Expand All @@ -28,34 +32,71 @@ public AccuWeatherAdapter(AccuWeatherAPI api) {

@Override
public CondicionClimatica getCondicionClimatica(String localidad) {
setData(localidad);
setClimaticData(localidad);
return new CondicionClimatica(getTemperatura(), getPrecipitaciones());
}

private void setData(String localidad) {
this.data = api.getWeather(localidad);
@Override
public List<AlertaClimatica> getAlertas(String localidad) {
setAlertsData(localidad);
return parseAlerts();
}

private List<AlertaClimatica> parseAlerts() {
String[] alerts = (String[]) alertsData.get("Current Alerts");

List<AlertaClimatica> allertsParsed = Arrays.stream(alerts)
.map(s -> getCorrespondingAlert(s)).collect(Collectors.toList());

allertsParsed.remove(null);

return allertsParsed;
}

private AlertaClimatica getCorrespondingAlert(String str) {
switch (str) {
case "STORM":
return AlertaClimatica.TORMENTA;
case "HAIL":
return AlertaClimatica.GRANIZO;
case "SNOW":
return AlertaClimatica.NEVADA;
default:
return null;
}
}

private void setClimaticData(String localidad) {
this.climaticData = api.getWeather(localidad);
}

private void setAlertsData(String localidad) {
this.alertsData = api.getAlerts(localidad);
}

private Temperatura getTemperatura() {
return new Temperatura(getUnidad(), getGrados());
}

private double getPrecipitaciones() {
return Double.valueOf((Integer) data.get(0).get("PrecipitationProbability"));
return Double.valueOf((Integer) climaticData.get(0).get("PrecipitationProbability"));
}

private double getGrados() {
@SuppressWarnings("unchecked")
Map<String, Object> temperature = (Map<String, Object>) data.get(0).get("Temperature");
Map<String, Object> temperature =
(Map<String, Object>) climaticData.get(0).get("Temperature");
Object value = temperature.get("Value");
return Double.valueOf((Integer) value);
}

private GradoTemperatura getUnidad() {
@SuppressWarnings("unchecked")
Map<String, Object> temperature = (Map<String, Object>) data.get(0).get("Temperature");
Map<String, Object> temperature =
(Map<String, Object>) climaticData.get(0).get("Temperature");

return temperature.get("Unit").equals("F") ? GradoTemperatura.FAHRENHEIT
: GradoTemperatura.CELSIUS;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package quemepongo.service.clima;

import java.util.List;
import quemepongo.api.clima.accuweather.AccuWeatherAdapter;
import quemepongo.model.clima.AlertaClimatica;
import quemepongo.model.clima.CondicionClimatica;

/**
Expand All @@ -15,10 +17,20 @@ public interface ServicioMetereologico {
* Obtiene datos climáticos de una ciudad.
*
* @param localidad la ciudad de la cual obtener datos climáticos
* @return entrega una lista con el clima de las próximas 12 horas en un diccionario.
* @return las condiciones climaticas actuales.
* @since Iteration IV
*/
CondicionClimatica getCondicionClimatica(String localidad);

/**
* Obtiene las alertas climaticas de una ciudad.
*
* @param localidad la ciudad de la cual obtener datos climáticos
* @return un listado de alertas actuales
* @since Iteration VI
*/
List<AlertaClimatica> getAlertas(String localidad);

/**
* Obtiene el servicio meteorologico actual.
*
Expand Down

0 comments on commit 7eb2438

Please sign in to comment.