Current section
Files
Jump to
Current section
Files
lib/elixir_freshbooks/helper/xml.ex
defmodule ElixirFreshbooks.Helper.XML do
@moduledoc """
XML helper macros.
See: http://www.freshbooks.com/developers/docs/clients
Copyright 2015 Marcelo Gornstein <marcelog@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
defmacro __using__(_opts) do
quote do
import ElixirFreshbooks.Helper.XML
require Record
require Logger
Record.defrecord(
:xmlText,
Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
)
Record.defrecord(
:xmlElement,
Record.extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl")
)
Record.defrecord(
:xmlAttribute,
Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
)
end
end
defmacro xml_attribute(doc, attribute) do
quote [location: :keep] do
attribute = String.to_atom(unquote(attribute))
attrs = xml_attributes(unquote(doc))
case Enum.filter(
attrs, fn(a) -> xmlAttribute(a, :name) === attribute end
) do
[] -> nil
[a] -> to_string(xmlAttribute(a, :value))
end
end
end
defmacro xml_attributes(doc) do
quote [location: :keep] do
xmlElement(unquote(doc), :attributes)
end
end
defmacro xml_find(doc, xpath) do
quote [location: :keep] do
Exmerl.XPath.find unquote(doc), unquote(xpath)
end
end
defmacro xml_value(doc, element) do
quote [location: :keep] do
elements = xml_find unquote(doc), "#{unquote(element)}/text()"
for e <- elements, do: to_string xmlText(e, :value)
end
end
defmacro xml_one_value_int(doc, element) do
quote [location: :keep] do
case xml_one_value(unquote(doc), unquote(element)) do
nil -> nil
code ->
{code, ""} = Integer.parse code
code
end
end
end
defmacro xml_one_value(doc, element) do
quote [location: :keep] do
case xml_find unquote(doc), "#{unquote(element)}/text()" do
[] -> nil
[e] -> to_string xmlText(e, :value)
end
end
end
end