Learning from The little Elixir and OTP Guidebook

Hi folks. As mentioned in the title, I am using the book to learn. I have an issue with the Weather Weather project.

I have double and tripple checked and my syntax is correct.

Below is the defmodule that I created:

defmodule Wx.Worker do

def temperature_of(location) do
result = url_for(location) |> HTTPoison.get |> parse_response
case result do
{:ok, temp} →
“#{location}: #{temp}C”
:error →
“#{location} not found”
end
end

  defp url_for(location) do
    location = URI.encode(location)
    "http://api.openweather.org/data/2.5/weather?q=#{location}&appid=#{apikey}"
  end

  defp parse_response({:ok, %HTTPoison.Response{body: body, status_code: 200}}) do
    body |> JSON.decode! |> compute_temperature
  end

  defp parse_response(_) do
    :error
  end

  defp compute_temperature(json) do
    try do
      temp = (json["main"]["temp"] - 273.15) |> Float.round(1)
      {:ok, temp}
    rescue
      _ -> :error
    end
  end

defp apikey do
“MY API KEY - NOT SHOWN”
end

end

My mix.exs file is updated and I have updated the deps.

When I use iex(30)> Wx.Worker.temperature_of “Wellington”. I get the error: “Wellington not found”

When I pass iex(31)> HTTPoison.get “https://api.openweathermap.org/data/2.5/weather?q=Wellington,ZA&appid=MY API KEY NOT SHOWN” I get the (what I can see is the correct response) :slight_smile:

{:ok,
%HTTPoison.Response{
body: “{“coord”:{“lon”:19.0112,“lat”:-33.6398},“weather”:[{“id”:800,“main”:“Clear”,“description”:“clear sky”,“icon”:“01d”}],“base”:“stations”,“main”:{“temp”:293.13,“feels_like”:292.25,“temp_min”:292.42,“temp_max”:293.55,“pressure”:1014,“humidity”:41,“sea_level”:1014,“grnd_level”:999},“visibility”:10000,“wind”:{“speed”:2.09,“deg”:272,“gust”:1.5},“clouds”:{“all”:1},“dt”:1633086290,“sys”:{“type”:2,“id”:2003650,“country”:“ZA”,“sunrise”:1633062053,“sunset”:1633106770},“timezone”:7200,“id”:3359510,“name”:“Wellington”,“cod”:200}”,
headers: [
{“Server”, “openresty”},
{“Date”, “Fri, 01 Oct 2021 11:13:50 GMT”},
{“Content-Type”, “application/json; charset=utf-8”},
{“Content-Length”, “520”},
{“Connection”, “keep-alive”},
{“X-Cache-Key”, “/data/2.5/weather?q=wellington,za”},
{“Access-Control-Allow-Origin”, “*”},
{“Access-Control-Allow-Credentials”, “true”},
{“Access-Control-Allow-Methods”, “GET, POST”}
],
request: %HTTPoison.Request{
body: “”,
headers: [],
method: :get,
options: [],
params: %{},
url: “https://api.openweathermap.org/data/2.5/weather?q=Wellington,ZA&appid=API KEY NOT SHOWN”
},
request_url: “https://api.openweathermap.org/data/2.5/weather?q=Wellington,ZA&appid=API KEY NOT SHOWNf”,
status_code: 200
}}

Openweather says it takes a few hours to activate your API key and I have waited for a few hours. Is there something that you folks can pick up, that I missed.

Here is my mix.exs file:

defmodule Wx.MixProject do
use Mix.Project

def project do
[
app: :wx,
version: “0.1.0”,
elixir: “~> 1.11.2”,
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

Run “mix help compile.app” to learn about applications.

def application do
[
extra_applications: [:logger, :httpoison]
]
end

Run “mix help deps” to learn about dependencies.

defp deps do
[
{:httpoison, “~> 1.8.0”},
{:json, “~> 1.4.1”}
# {:dep_from_hexpm, “~> 0.3.0”},
# {:dep_from_git, git: “https://github.com/elixir-lang/my_dep.git”, tag: “0.1.0”}
]
end
end

Kind regards,
Dmitri

You should wrap your code with 3 backticks… like this ```

It would be much more readable.

You do not use the same parameters.

Wellington versus Wellington,AZ

Maybe it’s the reason.

I liked the book alot, but some parts might be outdated.

1 Like

Thanks for the 3 backticks tip - appreciate it.

I have tried both Wellington, ZA and Wellington and I receive the same error

There is also http versus https that might be problematic.

1 Like

Thanks, I changed it to http and it worked. Thank you so much for taking the time out to check and give advice. I am a 55 year old newbie but have fallen in love with Elixir. No prior programming experience at all.

Much much appreciated.
Regards,
Dmitri

6 Likes