Skip to content

Commit

Permalink
using SweetXml
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelog committed Jan 29, 2017
1 parent a66090c commit 50973c5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lib/elixir_authorizenet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule AuthorizeNet do
}]
case Http.req :post, uri(), body do
{:ok, 200, _headers, <<0xEF, 0xBB, 0xBF, body :: binary>>} ->
{doc, _} = Exmerl.from_string body
doc = SweetXml.parse body
[result] = xml_value doc, "//messages/resultCode"
if result === "Error" do
codes = xml_value doc, "//code"
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir_authorizenet/customer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ defmodule AuthorizeNet.Customer do
@spec from_xml(Record) :: AuthorizeNet.Customer.t
def from_xml(doc) do
profile_id = xml_one_value_int doc, "//customerProfileId"
payment_profiles = for p <- xml_find(doc, "//paymentProfiles") do
payment_profiles = for p <- xml_find(doc, ~x"//paymentProfiles"ls) do
PaymentProfile.from_xml p, profile_id
end
shipping_addresses = for a <- xml_find(doc, "//shipToList") do
shipping_addresses = for a <- xml_find(doc, ~x"//shipToList"ls) do
Address.from_xml a, profile_id
end
new(
Expand Down
28 changes: 8 additions & 20 deletions lib/elixir_authorizenet/helper/xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,36 @@ defmodule AuthorizeNet.Helper.XML do
import AuthorizeNet.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")
)
import SweetXml
end
end

defmacro xml_find(doc, xpath) do
quote [location: :keep] do
Exmerl.XPath.find unquote(doc), unquote(xpath)
SweetXml.xpath 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)
SweetXml.xpath unquote(doc), ~x"#{unquote(element)}/text()"ls
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
case SweetXml.xpath unquote(doc), ~x"#{unquote(element)}/text()"I do
0 -> nil
i -> i
end
end
end

defmacro xml_one_value(doc, element) do
quote [location: :keep] do
case xml_find unquote(doc), "#{unquote(element)}/text()" do
case SweetXml.xpath unquote(doc), ~x"#{unquote(element)}/text()"ls do
[] -> nil
elements ->
strings = for e <- elements, do: to_string xmlText(e, :value)
Enum.join strings, ""
elements -> Enum.join elements, ""
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/elixir_authorizenet/payment_profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule AuthorizeNet.PaymentProfile do
offset: offset
]
]
profiles = xml_find doc, "//paymentProfile"
profiles = xml_find doc, ~x"//paymentProfile"l
for p <- profiles do
customer_id = xml_one_value_int p, "//customerProfileId"
from_xml p, customer_id
Expand Down Expand Up @@ -195,11 +195,11 @@ defmodule AuthorizeNet.PaymentProfile do
end
type
end
payment = case xml_find doc, "//creditCard" do
payment = case xml_find doc, ~x"//creditCard"l do
[] -> BankAccount.from_xml doc
_ -> Card.from_xml doc
end
address = case xml_find doc, "//billTo" do
address = case xml_find doc, ~x"//billTo"l do
[] -> nil
_ -> Address.from_xml doc, customer_id
end
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_authorizenet/transaction_response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule AuthorizeNet.TransactionResponse do
success = (xml_one_value(doc, "//resultCode") !== "Error") and (code === 1)
codes = xml_value doc, "//code"
texts = xml_value doc, "//text"
user_fields = case xml_find doc, "//userField" do
user_fields = case xml_find doc, ~x"//userField"l do
[] -> []
user_fields -> for f <- user_fields do
{xml_one_value(f, "//name"), xml_one_value(f, "//value")}
Expand Down
34 changes: 17 additions & 17 deletions test/elixir_authorizenet_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule AuthorizeNetTest do
request_assert "customer_profiles_get_all", "getCustomerProfileIdsRequest",
fn() -> AuthorizeNet.Customer.get_all end,
fn(body, msgs) ->
msgs = if xml_find(body, "//merchantAuthentication") === [] do
msgs = if xml_find(body, ~x"//merchantAuthentication"l) === [] do
["missing auth section"|msgs]
else
msgs
Expand Down Expand Up @@ -742,7 +742,7 @@ defmodule AuthorizeNetTest do
T.run
end,
fn(body, msgs) ->
[s1, s2, s3, s4, s5] = xml_find body, "//setting"
[s1, s2, s3, s4, s5] = xml_find body, ~x"//setting"l
assert_fields(s1, msgs, [
{"settingName", "allowPartialAuth"},
{"settingValue", "0"}
Expand Down Expand Up @@ -781,7 +781,7 @@ defmodule AuthorizeNetTest do
T.run
end,
fn(body, msgs) ->
[s1, s2, s3, s4, s5] = xml_find body, "//setting"
[s1, s2, s3, s4, s5] = xml_find body, ~x"//setting"l
assert_fields(s1, msgs, [
{"settingName", "allowPartialAuth"},
{"settingValue", "1"}
Expand Down Expand Up @@ -816,7 +816,7 @@ defmodule AuthorizeNetTest do
T.run
end,
fn(body, msgs) ->
[o_data] = xml_find body, "//opaqueData"
[o_data] = xml_find body, ~x"//opaqueData"l
assert_fields(o_data, msgs, [
{"dataDescriptor", "COMMON.APPLE.INAPP.PAYMENT"},
{"dataValue", "443331"}
Expand All @@ -836,7 +836,7 @@ defmodule AuthorizeNetTest do
T.run
end,
fn(body, msgs) ->
[card] = xml_find body, "//creditCard"
[card] = xml_find body, ~x"//creditCard"l
assert_fields(card, msgs, [
{"cardNumber", "5424000000000015"},
{"expirationDate", "2015-08"},
Expand All @@ -860,7 +860,7 @@ defmodule AuthorizeNetTest do
T.run
end,
fn(body, msgs) ->
[acc] = xml_find body, "//bankAccount"
[acc] = xml_find body, ~x"//bankAccount"l
assert_fields(acc, msgs, [
{"bankName", "bank_name"},
{"echeckType", "CCD"},
Expand Down Expand Up @@ -913,16 +913,16 @@ defmodule AuthorizeNetTest do
T.run
end,
fn(body, msgs) ->
[profile] = xml_find body, "//profile"
[order] = xml_find body, "//order"
[f1, f2] = xml_find body, "//userField"
[ship_to] = xml_find body, "//shipTo"
[bill_to] = xml_find body, "//billTo"
[i1, i2] = xml_find body, "//lineItem"
[tax] = xml_find body, "//tax"
[duty] = xml_find body, "//duty"
[shipping] = xml_find body, "//shipping"
[customer] = xml_find body, "//customer"
[profile] = xml_find body, ~x"//profile"l
[order] = xml_find body, ~x"//order"l
[f1, f2] = xml_find body, ~x"//userField"l
[ship_to] = xml_find body, ~x"//shipTo"l
[bill_to] = xml_find body, ~x"//billTo"l
[i1, i2] = xml_find body, ~x"//lineItem"l
[tax] = xml_find body, ~x"//tax"l
[duty] = xml_find body, ~x"//duty"l
[shipping] = xml_find body, ~x"//shipping"l
[customer] = xml_find body, ~x"//customer"l
assert "3.01" === hd(xml_value body, "//amount")
msgs = assert_fields(body, msgs, [
{"marketType", "2"},
Expand Down Expand Up @@ -1041,7 +1041,7 @@ defmodule AuthorizeNetTest do
{:error, error} -> ["invalid schema: #{inspect error}"|msgs]
:ok -> msgs
end
msgs = if xml_find(body, "//#{request_type}") === [] do
msgs = if xml_find(body, ~x"//#{request_type}"l) === [] do
["missing request section"|msgs]
else
msgs
Expand Down

0 comments on commit 50973c5

Please sign in to comment.