Handled invalid city ID.

This commit is contained in:
Erik C. Thauvin 2019-07-28 15:58:00 -07:00
parent 106accdcf5
commit b8539a7295
2 changed files with 19 additions and 6 deletions

View file

@ -44,6 +44,7 @@ import net.thauvin.erik.mobibot.msg.ErrorMessage;
import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.Message;
import net.thauvin.erik.mobibot.msg.NoticeMessage; import net.thauvin.erik.mobibot.msg.NoticeMessage;
import net.thauvin.erik.mobibot.msg.PublicMessage; import net.thauvin.erik.mobibot.msg.PublicMessage;
import okhttp3.HttpUrl;
import org.jibble.pircbot.Colors; import org.jibble.pircbot.Colors;
import java.util.ArrayList; import java.util.ArrayList;
@ -167,10 +168,20 @@ public class Weather2 extends ThreadedModule {
messages.add(new NoticeMessage(condition.toString())); messages.add(new NoticeMessage(condition.toString()));
} }
} }
messages.add(new NoticeMessage("https://openweathermap.org/city/"
+ cwd.getCityId(), Colors.GREEN));
}
if (cwd.getCityId() != null) {
if (cwd.getCityId() > 0) {
messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(),
Colors.GREEN));
} else {
final HttpUrl url =
HttpUrl.parse("https://openweathermap.org/find").newBuilder().addQueryParameter(
"q", city).build();
messages.add(
new NoticeMessage(url.toString(), Colors.GREEN));
}
}
}
} catch (APIException | NullPointerException e) { } catch (APIException | NullPointerException e) {
throw new ModuleException("getWeather(" + query + ')', "Unable to perform weather lookup.", e); throw new ModuleException("getWeather(" + query + ')', "Unable to perform weather lookup.", e);
} }
@ -194,7 +205,7 @@ public class Weather2 extends ThreadedModule {
bot.send(sender, "For example:"); bot.send(sender, "For example:");
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " paris, fr")); bot.send(sender, bot.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " paris, fr"));
bot.send(sender, "The default ISO 3166 country code is " + Utils.bold("US") bot.send(sender, "The default ISO 3166 country code is " + Utils.bold("US")
+ ". Zip codes are supported in most countries."); + ". Zip codes are supported in most countries.");
} }
/** /**

View file

@ -51,11 +51,13 @@ public class Weather2Test extends LocalProperties {
@SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
@Test @Test
public void testWeather() throws ModuleException { public void testWeather() throws ModuleException {
List<Message> messages = Weather2.getWeather("98204", List<Message> messages = Weather2.getWeather("98204", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
assertThat(messages.get(0).getMessage()).as("is Everett").contains("Everett"); assertThat(messages.get(0).getMessage()).as("is Everett").contains("Everett");
assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Search").endsWith("98204");
messages = Weather2.getWeather("London, UK", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP)); messages = Weather2.getWeather("London, UK", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
assertThat(messages.get(0).getMessage()).as("is UK").contains("UK"); assertThat(messages.get(0).getMessage()).as("is UK").contains("UK");
assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Code").endsWith("4298960");
try { try {
Weather2.getWeather("test", ""); Weather2.getWeather("test", "");