Skip to content

Commit

Permalink
add address struct for identity addresses (#9)
Browse files Browse the repository at this point in the history
* add address struct for identity addresses

* fix a couple of lints
  • Loading branch information
coburncoburn authored Sep 15, 2022
1 parent 2153085 commit b07969c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/circlex/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule Circlex.Api do
after_request_hook.({request, response})

case response do
{:ok, %HTTPoison.Response{status_code: status_code, body: body} = response} ->
{:ok, %HTTPoison.Response{status_code: status_code, body: body}} ->
with {:ok, json} <- Jason.decode(body) do
case status_code do
code when code in 200..299 ->
Expand Down
32 changes: 32 additions & 0 deletions lib/circlex/struct/physical_address.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Circlex.Struct.PhysicalAddress do
@moduledoc """
Transfer beneficiaries need a physical address for travel rule requirements
References: https://www.circle.com/blog/introducing-the-travel-rule-universal-solution-technology
"""
use Circlex.Struct.JasonHelper
import Circlex.Struct.Util

defstruct [:line1, :line2, :city, :district, :postal_code, :country]

def deserialize(physical_address) do
%__MODULE__{
line1: fetch(physical_address, :line1),
line2: fetch(physical_address, :line2),
city: fetch(physical_address, :city),
district: fetch(physical_address, :district),
postal_code: fetch(physical_address, :postalCode),
country: fetch(physical_address, :country)
}
end

def serialize(physical_address) do
%{
line1: physical_address.line1,
line2: physical_address.line2,
city: physical_address.city,
district: physical_address.district,
postalCode: physical_address.postal_code,
country: physical_address.country
}
end
end
12 changes: 7 additions & 5 deletions lib/circlex/struct/source_dest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ defmodule Circlex.Struct.SourceDest do
%__MODULE__{
name: fetch(identity, :name),
type: fetch(identity, :type),
addresses: fetch(identity, :addresses)
addresses:
(fetch(identity, :addresses) || [])
|> Enum.map(&Circlex.Struct.PhysicalAddress.deserialize/1)
}
end

def serialize(source) do
def serialize(identity) do
%{
name: source.name,
type: source.type,
addresses: source.addresses
name: identity.name,
type: identity.type,
addresses: Enum.map(identity.addresses || [], &Circlex.Struct.PhysicalAddress.serialize/1)
}
end
end
Expand Down
1 change: 0 additions & 1 deletion test/circlex/emulator/logic/wallet_logic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ defmodule Circlex.Emulator.Logic.WalletLogicTest do
assert {:ok,
[
%Wallet{
addresses: nil,
balances: [
%Amount{amount: "1000.00", currency: "GBP"},
%Amount{amount: "150234.93", currency: "USD"}
Expand Down
66 changes: 66 additions & 0 deletions test/circlex/struct/source_dest_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule Circlex.Struct.SourceDestTest do
use ExUnit.Case
alias Circlex.Struct.{SourceDest, SourceDest.Identity, PhysicalAddress}

@source_dest %SourceDest{
id: "12345",
identities: [
%Identity{
addresses: [
%PhysicalAddress{
city: "San Franciso",
country: "US",
district: "CA",
line1: "1460 Mission St. Unit 02-121",
line2: nil,
postal_code: "94103"
}
],
name: "Compound Prime",
type: "business"
}
],
type: :wallet
}

@source_dest_ser %{
id: "12345",
identities: [
%{
addresses: [
%{
city: "San Franciso",
country: "US",
district: "CA",
line1: "1460 Mission St. Unit 02-121",
line2: nil,
postalCode: "94103"
}
],
name: "Compound Prime",
type: "business"
}
],
type: "wallet"
}

describe "deserialize" do
test "normal" do
assert SourceDest.deserialize(@source_dest_ser) == @source_dest
end
end

describe "serialize" do
test "normal" do
assert SourceDest.serialize(@source_dest) ==
@source_dest_ser
end
end

describe "JasonEncoding" do
test "it calls serialize then jason.encode" do
assert Jason.encode(@source_dest) ==
Jason.encode(@source_dest_ser)
end
end
end

0 comments on commit b07969c

Please sign in to comment.