Skip to content

Commit

Permalink
Merge pull request #229 from schrockwell/2.0
Browse files Browse the repository at this point in the history
Correctly parsing responses from non-object responses
  • Loading branch information
schrockwell authored Apr 3, 2017
2 parents 1e9b87d + d6d39ed commit 7330ff7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
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

0 comments on commit 7330ff7

Please sign in to comment.