Compare commits

...

3 Commits

Author SHA1 Message Date
Nathan Mattes af1c3ff390 orchestrate download and printing 2022-02-25 13:48:26 +01:00
Nathan Mattes 27d91f821a Add XML-parsing and nicely format output
thank you @ pspdfkit to show me how to use XM(ER)L-stuff from erlang :-) Feels like using Swift in combination with ObjC
2022-02-25 13:48:04 +01:00
Nathan Mattes 279688eb1e Show special error when airport wasn't found
and run mix format
2022-02-25 13:46:53 +01:00
5 changed files with 53 additions and 15 deletions

View File

@ -1,10 +1,12 @@
defmodule Weather.CLI do
def run(argv) do
# get shortname of airport from argv
# use Weather.Gov to fetch XML data
# parse XML using :xmler_scan, see: https://medium.com/@blogscot/parsing-xml-using-elixir-mostly-f1368593b0ca
# pring
argv
|> process()
end
end
def process(airport) when is_binary(airport) do
airport
|> Weather.Gov.current_observations()
|> Weather.NiceXMLFormatter.format_observation_nicely()
end
end

View File

@ -1,21 +1,25 @@
defmodule Weather.Gov do
def url(airport) do
"https://w1.weather.gov/xml/current_obs/#{airport}.xml"
end
def current_observerations(airport) do
def current_observations(airport) do
url(airport)
|> HTTPoison.get(["Accept": "application/xml"])
|> HTTPoison.get(Accept: "application/xml")
|> process_result()
end
def process_result({:ok, %HTTPoison.Response{status_code: 200, body: body}}) do
body
end
def process_result({:ok, %HTTPoison.Response{status_code: 404}}) do
IO.puts("Location wasn't found")
System.halt(404)
end
def process_result({:ok, %HTTPoison.Response{status_code: status_code}}) do
IO.puts("Wrong status code: #{status_code}")
IO.puts("Another error occured, code: #{status_code}")
System.halt(status_code)
end
@ -23,4 +27,4 @@ defmodule Weather.Gov do
IO.inspect(reason)
System.halt(2)
end
end
end

View File

@ -0,0 +1,23 @@
defmodule Weather.NiceXMLFormatter do
require Weather.XML
def format_observation_nicely(xml_string) when is_binary(xml_string) do
{doc, []} =
xml_string
|> to_charlist()
|> :xmerl_scan.string()
location = get_child("location", doc)
weather = get_child("weather", doc)
observation_time = get_child("observation_time", doc)
IO.puts "Location: #{location}"
IO.puts "Weather: #{weather}"
IO.puts observation_time
end
defp get_child(node_name, doc) do
:xmerl_xpath.string('/current_observation/#{node_name}/text()', doc)
|> Enum.map(&Weather.XML.xmlText(&1, :value))
end
end

View File

@ -0,0 +1,9 @@
# thank you PSPDFKit-people: https://pspdfkit.com/blog/2018/how-to-parse-xml-documents-in-elixir/
defmodule Weather.XML do
import Record
defrecord(:xmlElement, extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl"))
defrecord(:xmlAttribute, extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl"))
defrecord(:xmlText, extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl"))
end

View File

@ -14,7 +14,7 @@ defmodule Weather.MixProject do
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger, :httpoison]
extra_applications: [:logger, :httpoison, :xmerl]
]
end