diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java index 115916e..c51566d 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java @@ -54,15 +54,14 @@ import java.util.ArrayList; * @since 1.0 */ public final class StockQuote extends AbstractModule { - + /** + * The Alpha Advantage property key. + */ static final String ALPHAVANTAGE_API_KEY_PROP = "alphavantage-api-key"; // The Alpha Advantage URL. private static final String ALAPHADVANTAGE_URL = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE"; - - /** - * The quote command. - */ + // The quote command. private static final String STOCK_CMD = "stock"; /** @@ -73,7 +72,6 @@ public final class StockQuote extends AbstractModule { properties.put(ALPHAVANTAGE_API_KEY_PROP, ""); } - /** * Get a stock quote. * @@ -90,46 +88,56 @@ public final class StockQuote extends AbstractModule { try { final Response response = client.newCall(request).execute(); - final JSONObject json = new JSONObject(response.body().string()); + if (response.body() != null) { + final JSONObject json = new JSONObject(response.body().string()); - try { - final String info = json.getString("Information"); - if (!info.isEmpty()) { - throw new ModuleException(debugMessage, Utils.unescapeXml(info)); + try { + final String info = json.getString("Information"); + if (!info.isEmpty()) { + throw new ModuleException(debugMessage, Utils.unescapeXml(info)); + } + } catch (JSONException ignore) { + // Do nothing. } - } catch (JSONException ignore) { - // Do nothing. - } - try { - final String error = json.getString("Error Message"); - if (!error.isEmpty()) { - throw new ModuleException(debugMessage, Utils.unescapeXml(error)); + try { + final String error = json.getString("Error Message"); + if (!error.isEmpty()) { + throw new ModuleException(debugMessage, Utils.unescapeXml(error)); + } + } catch (JSONException ignore) { + // Do nothing. } - } catch (JSONException ignore) { - // Do nothing. + + final JSONObject quote = json.getJSONObject("Global Quote"); + + if (quote.isEmpty()) { + messages.add(new ErrorMessage("Invalid symbol.")); + return messages; + } + + messages.add( + new PublicMessage("Symbol: " + Utils.unescapeXml(quote.getString("01. symbol")))); + messages.add( + new PublicMessage(" Price: " + Utils.unescapeXml(quote.getString("05. price")))); + messages.add( + new PublicMessage(" Previous: " + + Utils.unescapeXml(quote.getString("08. previous close")))); + messages.add( + new NoticeMessage(" Open: " + Utils.unescapeXml(quote.getString("02. open")))); + messages.add( + new NoticeMessage(" High: " + Utils.unescapeXml(quote.getString("03. high")))); + messages.add( + new NoticeMessage(" Low: " + Utils.unescapeXml(quote.getString("04. low")))); + messages.add( + new NoticeMessage(" Volume: " + Utils.unescapeXml(quote.getString("06. volume")))); + messages.add( + new NoticeMessage(" Latest: " + + Utils.unescapeXml(quote.getString("07. latest trading day")))); + messages.add( + new NoticeMessage(" Change: " + Utils.unescapeXml(quote.getString("09. change")) + + " [" + Utils.unescapeXml(quote.getString("10. change percent")) + ']')); } - - final JSONObject quote = json.getJSONObject("Global Quote"); - - if (quote.isEmpty()) { - messages.add(new ErrorMessage("Invalid symbol.")); - return messages; - } - - messages.add( - new PublicMessage("Symbol: " + Utils.unescapeXml(quote.getString("01. symbol")))); - messages.add(new PublicMessage(" Price: " + Utils.unescapeXml(quote.getString("05. price")))); - messages.add(new PublicMessage(" Previous: " - + Utils.unescapeXml(quote.getString("08. previous close")))); - messages.add(new NoticeMessage(" Open: " + Utils.unescapeXml(quote.getString("02. open")))); - messages.add(new NoticeMessage(" High: " + Utils.unescapeXml(quote.getString("03. high")))); - messages.add(new NoticeMessage(" Low: " + Utils.unescapeXml(quote.getString("04. low")))); - messages.add(new NoticeMessage(" Volume: " + Utils.unescapeXml(quote.getString("06. volume")))); - messages.add(new NoticeMessage(" Latest: " - + Utils.unescapeXml(quote.getString("07. latest trading day")))); - messages.add(new NoticeMessage(" Change: " + Utils.unescapeXml(quote.getString("09. change")) - + " [" + Utils.unescapeXml(quote.getString("10. change percent")) + ']')); } catch (IOException e) { throw new ModuleException(debugMessage, "An error has occurred retrieving a stock quote.", e); } @@ -165,7 +173,7 @@ public final class StockQuote extends AbstractModule { final ArrayList messages = getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP)); for (Message msg : messages) { - bot.send(msg.isNoticeOrError() ? sender : bot.getChannel(), msg.getMessage()); + bot.send(msg.isNotice() ? sender : bot.getChannel(), msg.getMessage()); } } catch (ModuleException e) { diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/ThreadedModule.java b/src/main/java/net/thauvin/erik/mobibot/modules/ThreadedModule.java index 85b3e3d..59e99ce 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/ThreadedModule.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/ThreadedModule.java @@ -54,15 +54,7 @@ public abstract class ThreadedModule extends AbstractModule { } /** - * {@inheritDoc} - */ - @Override - public boolean isEnabled() { - return isValidProperties(); - } - - /** - * Run the thread. + * Runs the thread. */ abstract void run(Mobibot bot, String sender, String args); } diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/War.java b/src/main/java/net/thauvin/erik/mobibot/modules/War.java index 71e606e..74a8f33 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/War.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/War.java @@ -44,15 +44,11 @@ import java.security.SecureRandom; * @since 1.0 */ public final class War extends AbstractModule { - /** - * The war command - */ - public static final String WAR_CMD = "war"; - + // The war command + private static final String WAR_CMD = "war"; // The deck of card. private static final String[] WAR_DECK = new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"}; - // The suits for the deck of card. private static final String[] WAR_SUITS = new String[]{"Hearts", "Spades", "Diamonds", "Clubs"}; diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java index a230c26..1b87363 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java @@ -50,14 +50,10 @@ import java.util.List; * @since 1.0 */ public class Weather2 extends AbstractModule { - - /** - * The weather command. - */ - public static final String WEATHER_CMD = "weather"; - // The OpenWeatherMap API Key property. private static final String OWM_API_KEY_PROP = "owm-api-key"; + // The weather command. + private static final String WEATHER_CMD = "weather"; /** * Creates a new {@link Weather2} instance. @@ -76,7 +72,7 @@ public class Weather2 extends AbstractModule { } private String fAndC(final Double d) { - final Double c = (d - 32) * 5 / 9; + final double c = (d - 32) * 5 / 9; return Math.round(d) + " \u00B0F, " + Math.round(c) + " \u00B0C"; } @@ -104,14 +100,6 @@ public class Weather2 extends AbstractModule { + ". Zip codes are supported in most countries."); } - /** - * {@inheritDoc} - */ - @Override - public boolean isEnabled() { - return isValidProperties(); - } - /** * Fetches the weather data from a specific city. */ @@ -146,7 +134,7 @@ public class Weather2 extends AbstractModule { bot.send(sender, "Temperature: " + fAndC(main.getTemp()), isPrivate); } - if (main.hasHumidity()) { + if (main.hasHumidity() && (main.getHumidity() != null)) { bot.send(sender, "Humidity: " + Math.round(main.getHumidity()) + "%", isPrivate); } }