Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly parsing responses from non-object responses #229

Merged
merged 1 commit into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/stripe/connect/oauth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/fixtures/card_deleted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deleted": true,
"id":"card_1A49JREym4h6pgdFkbcuN03L"
}
18 changes: 15 additions & 3 deletions test/stripe/converter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down