From d6d39ede82850525010b568f4b3d3f8e62238216 Mon Sep 17 00:00:00 2001 From: Rockwell Schrock Date: Mon, 3 Apr 2017 11:41:08 -0400 Subject: [PATCH] Correctly parsing responses from DELETE requests --- lib/stripe/connect/oauth.ex | 4 ++-- lib/stripe/converter.ex | 14 +++++++++----- lib/stripe/request.ex | 2 +- test/fixtures/card_deleted.json | 4 ++++ test/stripe/converter_test.exs | 18 +++++++++++++++--- 5 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/card_deleted.json diff --git a/lib/stripe/connect/oauth.ex b/lib/stripe/connect/oauth.ex index b6ca51dd..36697732 100644 --- a/lib/stripe/connect/oauth.ex +++ b/lib/stripe/connect/oauth.ex @@ -73,7 +73,7 @@ defmodule Stripe.Connect.OAuth do } case Stripe.oauth_request(:post, endpoint, body) do - {:ok, result} -> {:ok, Converter.stripe_map_to_struct(result)} + {:ok, result} -> {:ok, Converter.convert_result(result)} {:error, error} -> {:error, error} end end @@ -98,7 +98,7 @@ defmodule Stripe.Connect.OAuth do } case Stripe.oauth_request(:post, endpoint, body) do - {:ok, result} -> {:ok, Converter.stripe_map_to_struct(result)} + {:ok, result} -> {:ok, Converter.convert_result(result)} {:error, error} -> {:error, error} end end diff --git a/lib/stripe/converter.ex b/lib/stripe/converter.ex index b4b9ae3c..5c596960 100644 --- a/lib/stripe/converter.ex +++ b/lib/stripe/converter.ex @@ -1,13 +1,17 @@ defmodule Stripe.Converter do @doc """ - Takes the module (e.g. `Stripe.Card`) and the response from Stripe and - returns a struct (e.g. `%Stripe.Card{}`) containing the Stripe response. + Takes a result map or list of maps from a Stripe response and returns a + struct (e.g. `%Stripe.Card{}`) or list of structs. + + If the result is not a supported Stripe object, it just returns a plain map + with atomized keys. """ - @spec stripe_map_to_struct(%{String.t => any}) :: struct - def stripe_map_to_struct(response), do: convert_stripe_object(response) + @spec convert_result(%{String.t => any}) :: struct + def convert_result(result), do: convert_value(result) - @supported_objects ~w(account bank_account card customer event external_account file_upload invoice list plan subscription token) + @supported_objects ~w(account bank_account card customer event external_account + file_upload invoice list plan subscription token) @spec convert_value(any) :: any defp convert_value(%{"object" => object_name} = value) when is_binary(object_name) do diff --git a/lib/stripe/request.ex b/lib/stripe/request.ex index ca9f50be..7095af9a 100644 --- a/lib/stripe/request.ex +++ b/lib/stripe/request.ex @@ -48,6 +48,6 @@ defmodule Stripe.Request do |> handle_result end - defp handle_result({:ok, result = %{}}), do: {:ok, Converter.stripe_map_to_struct(result)} + defp handle_result({:ok, result = %{}}), do: {:ok, Converter.convert_result(result)} defp handle_result({:error, error}), do: {:error, error} end diff --git a/test/fixtures/card_deleted.json b/test/fixtures/card_deleted.json new file mode 100644 index 00000000..7162ffa8 --- /dev/null +++ b/test/fixtures/card_deleted.json @@ -0,0 +1,4 @@ +{ + "deleted": true, + "id":"card_1A49JREym4h6pgdFkbcuN03L" +} \ No newline at end of file diff --git a/test/stripe/converter_test.exs b/test/stripe/converter_test.exs index 5885fb69..1377ce78 100644 --- a/test/stripe/converter_test.exs +++ b/test/stripe/converter_test.exs @@ -52,7 +52,7 @@ defmodule Stripe.ConverterTest do } fixture = Helper.load_fixture("event_with_customer.json") - result = Converter.stripe_map_to_struct(fixture) + result = Converter.convert_result(fixture) assert result == expected_result end @@ -116,7 +116,7 @@ defmodule Stripe.ConverterTest do } fixture = Helper.load_fixture("card_list.json") - result = Converter.stripe_map_to_struct(fixture) + result = Converter.convert_result(fixture) assert result == expected_result end @@ -153,7 +153,19 @@ defmodule Stripe.ConverterTest do } fixture = Helper.load_fixture("customer.json") - result = Converter.stripe_map_to_struct(fixture) + result = Converter.convert_result(fixture) + + assert result == expected_result + end + + test "converts a deleted response properly" do + expected_result = %{ + deleted: true, + id: "card_1A49JREym4h6pgdFkbcuN03L" + } + + fixture = Helper.load_fixture("card_deleted.json") + result = Converter.convert_result(fixture) assert result == expected_result end