Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

switch to Jason instead of Poison, plus extra error handling, etc #26

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions lib/forecastr/pirate_weather.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ defmodule Forecastr.PirateWeather do
@type when_to_forecast :: :today | :next_days
@spec weather(when_to_forecast, String.t(), map()) :: {:ok, map()} | {:error, atom()}
def weather(when_to_forecast, query, opts) do
case api_endpoint(when_to_forecast) do
nil ->
{:error, :no_api_key}

endpoint ->
params = convert_params(opts)

%{
"lat" => lat,
"lon" => lon,
"address" => %{
"city" => city,
"country" => country
}
} =
query
|> Geocoder.geocode()
|> pick_location()

with {:ok, forecast} <- fetch_weather_information(endpoint <> "/#{lat},#{lon}", params) do
{:ok,
forecast
|> add("name", city)
|> add("country", country)
|> add("when_to_forecast", Atom.to_string(when_to_forecast))}
end
with {:ok, endpoint} <- api_endpoint(when_to_forecast),
%{
"lat" => lat,
"lon" => lon,
"address" => %{
"city" => city,
"country" => country
}
} <-
query
|> Geocoder.geocode()
|> pick_location(),
{:ok, forecast} <-
fetch_weather_information(endpoint <> "/#{lat},#{lon}", convert_params(opts)) do
{:ok,
forecast
|> add("name", city)
|> add("country", country)
|> add("when_to_forecast", Atom.to_string(when_to_forecast))}
else
{:error, e} ->
{:error, e}

other ->
IO.warn(inspect(other))
{:error, "Could not find location or forecast"}
end
end

Expand Down Expand Up @@ -131,10 +131,10 @@ defmodule Forecastr.PirateWeather do
def api_endpoint(_) do
case Application.get_env(:forecastr, :appid) do
appid when is_binary(appid) and appid != "" ->
"https://api.pirateweather.net/forecast/#{appid}"
{:ok, "https://api.pirateweather.net/forecast/#{appid}"}

_ ->
nil
{:error, "Missing a PirateWeather API key"}
end
end

Expand All @@ -158,4 +158,5 @@ defmodule Forecastr.PirateWeather do
do: put_in(body, ["address", "city"], suburb)

defp pick_location({"address", address}), do: %{"address" => address} |> pick_location()
defp pick_location(other), do: other
end
34 changes: 8 additions & 26 deletions lib/forecastr/renderer/ascii.ex
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,8 @@ defmodule Forecastr.Renderer.ASCII do
#{white(output_type)} .-.
#{white(output_type)} ( ).
#{white(output_type)}(___(__)
#{blue(output_type)} ʻ#{reset(output_type)}#{light_white(output_type)} *#{reset(output_type)} ʻ #{
light_white(output_type)
}*
#{light_white(output_type)}*#{reset(output_type)}#{blue(output_type)} ʻ#{reset(output_type)} *#{
blue(output_type)
} ʻ
#{blue(output_type)} ʻ#{reset(output_type)}#{light_white(output_type)} *#{reset(output_type)} ʻ #{light_white(output_type)}*
#{light_white(output_type)}*#{reset(output_type)}#{blue(output_type)} ʻ#{reset(output_type)} *#{blue(output_type)} ʻ
"""
end

Expand All @@ -254,12 +250,8 @@ defmodule Forecastr.Renderer.ASCII do
#{yellow(output_type)}_`/\"\"#{white(output_type)}.-.
#{yellow(output_type)} ,\\_#{white(output_type)}\( ).
#{yellow(output_type)} /#{reset(output_type)}#{white(output_type)}(___(__)
#{blue(output_type)} ʻ#{reset(output_type)}#{white(output_type)} *#{blue(output_type)} ʻ#{
reset(output_type)
}#{white(output_type)} *
#{white(output_type)} *#{reset(output_type)}#{blue(output_type)} ʻ#{reset(output_type)}#{
white(output_type)
} *#{reset(output_type)}#{blue(output_type)} ʻ
#{blue(output_type)} ʻ#{reset(output_type)}#{white(output_type)} *#{blue(output_type)} ʻ#{reset(output_type)}#{white(output_type)} *
#{white(output_type)} *#{reset(output_type)}#{blue(output_type)} ʻ#{reset(output_type)}#{white(output_type)} *#{reset(output_type)}#{blue(output_type)} ʻ
"""
end

Expand Down Expand Up @@ -307,14 +299,8 @@ defmodule Forecastr.Renderer.ASCII do
#{white(output_type)} .-.
#{white(output_type)} ( ).
#{white(output_type)} (___(__)
#{blue(output_type)}‚ʻ#{reset(output_type)}#{bright_yellow(output_type)}⚡#{reset(output_type)}#{
blue(output_type)
}ʻ‚#{reset(output_type)}#{bright_yellow(output_type)}⚡#{reset(output_type)}#{
blue(output_type)
}‚ʻ
#{blue(output_type)}‚ʻ‚ʻ#{reset(output_type)}#{bright_yellow(output_type)}⚡#{
reset(output_type)
}#{blue(output_type)}ʻ‚ʻ
#{blue(output_type)}‚ʻ#{reset(output_type)}#{bright_yellow(output_type)}⚡#{reset(output_type)}#{blue(output_type)}ʻ‚#{reset(output_type)}#{bright_yellow(output_type)}⚡#{reset(output_type)}#{blue(output_type)}‚ʻ
#{blue(output_type)}‚ʻ‚ʻ#{reset(output_type)}#{bright_yellow(output_type)}⚡#{reset(output_type)}#{blue(output_type)}ʻ‚ʻ
"""
end

Expand All @@ -323,9 +309,7 @@ defmodule Forecastr.Renderer.ASCII do
#{yellow(output_type)}_`/\"\"#{reset(output_type)}#{white(output_type)}.-.
#{yellow(output_type)} ,\\#{reset(output_type)}#{white(output_type)}_( ).
#{yellow(output_type)} /#{reset(output_type)}#{white(output_type)}(___(__)
#{bright_yellow(output_type)} ⚡#{reset(output_type)}#{blue(output_type)}ʻ ʻ#{
reset(output_type)
}#{bright_yellow(output_type)}⚡ʻ
#{bright_yellow(output_type)} ⚡#{reset(output_type)}#{blue(output_type)}ʻ ʻ#{reset(output_type)}#{bright_yellow(output_type)}⚡ʻ
#{blue(output_type)} ʻ ʻ ʻ ʻ
"""
end
Expand All @@ -335,9 +319,7 @@ defmodule Forecastr.Renderer.ASCII do
#{yellow(output_type)}_`/\"\"#{reset(output_type)}.-.
#{yellow(output_type)} ,\\#{reset(output_type)}_( ).
#{yellow(output_type)} /#{reset(output_type)}(___(__)
#{light_white(output_type)} *#{reset(output_type)}#{bright_yellow(output_type)}⚡#{
reset(output_type)
}#{light_white(output_type)} *#{reset(output_type)}#{bright_yellow(output_type)}⚡
#{light_white(output_type)} *#{reset(output_type)}#{bright_yellow(output_type)}⚡#{reset(output_type)}#{light_white(output_type)} *#{reset(output_type)}#{bright_yellow(output_type)}⚡
#{light_white(output_type)} * * *
"""
end
Expand Down
Loading