Skip to content

Instantly share code, notes, and snippets.

View garyposter's full-sized avatar

Gary Poster garyposter

  • Kard
  • Raleigh, NC, USA
View GitHub Profile

Keybase proof

I hereby claim:

  • I am garyposter on github.
  • I am garyposter (https://keybase.io/garyposter) on keybase.
  • I have a public key whose fingerprint is 4805 0F36 C958 03DD EFE1 CF19 A6D2 68B1 A37D CFFF

To claim this, I am signing this object:

%{pressure_mb: "1023.4", water_temp_f: nil,
image: %{anyAttribs: [], link: "http://weather.gov",
title: "NOAA's National Weather Service",
url: "http://weather.gov/images/xml_logo.gif"}, windchill_string: nil,
icon_url_name: "bkn.png", copyright_url: "http://weather.gov/disclaimer.html",
icon_url_base: "http://forecast.weather.gov/images/wtf/small/",
windchill_c: nil, heat_index_string: nil, icon_name: nil, tide_ft: nil,
observation_time: "Last Updated on Dec 7 2015, 9:51 am EST",
two_day_history_url: "http://www.weather.gov/data/obhistory/KRDU.html",
mean_wave_dir: nil, privacy_policy_url: "http://weather.gov/notice.html",
body
|> xpath(~x"//current_observation/*"l, # Get a list of the child nodes.
tag: ~x"name()", # Get the tag name for each.
value: ~x"./text()"s) # Get the text for each.
|> Enum.map(fn(%{tag: tag, value: value}) -> {List.to_atom(tag), value} end)
|> Enum.into(Map.new)
def erlsom_transform(data = current_observation()), do:
Enum.into(current_observation(data), Map.new, &_transform_value/1)
def erlsom_transform(data = imageType()), do:
Enum.into(imageType(data), Map.new, &_transform_value/1)
def erlsom_transform(data = [first | _rest]) when is_integer(first), do:
List.to_string(data)
def erlsom_transform(:undefined), do: nil
def erlsom_transform(data), do: data
defp _transform_value({k, v}), do: {k, erlsom_transform(v)}
{:ok, xsdModel} = :erlsom.compile_xsd_file(
Path.join([__DIR__, "data", "current_observation.xsd"]))
@xsdModel xsdModel
require Record
xml_path = Path.join([__DIR__, "data", "current_observation.hrl"])
Record.defrecord :current_observation,
Record.extract(:current_observation, from: xml_path)
Record.defrecord :imageType,
Record.extract(:imageType, from: xml_path)
def handle_body(body) do
{:ok, data, _rest} = :erlsom.scan(body, @xsdModel)
erlsom_transform(data)
end
@garyposter
garyposter / elixir_erlsom_example.exs
Last active December 9, 2015 14:57
Some code snippets that show how you can use the erlang erlsom XML library, and Records, in Elixir.
# I wrote a blog version of this (http://codesinger.blogspot.com/2015/12/elixir-erlang-records-and-erlsom-xml.html), too.
# This example gets the XML for current observed weather at an airport from the
# NOAA. It then gets the associated XSD, and uses erlsom to pre-process it.
# This part can be run as an exs script.
import SweetXml
client = [{"User-agent", "Example Elixir Project"}]