11defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
2+ @ moduledoc """
3+ Handles data transformations between the API `Stripe.Event` struct and maps of
4+ attributes suitable for work with our own `StripeEvent` database objects.
5+ """
26 import CodeCorps.MapUtils , only: [ keys_to_string: 1 ]
37 import CodeCorps.StripeService.Util , only: [ transform_map: 2 ]
48
@@ -14,6 +18,7 @@ defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
1418 Transforms a `%Stripe.Event{}` and a set of local attributes into a
1519 map of parameters used to create or update a `StripeEvent` record.
1620 """
21+ @ spec to_params ( Stripe.Event . t , map ) :: { :ok , map }
1722 def to_params ( % Stripe.Event { } = stripe_event , % { } = attributes ) do
1823 result =
1924 stripe_event
@@ -45,12 +50,11 @@ defmodule CodeCorps.StripeService.Adapters.StripeEventAdapter do
4550 params |> Map . merge ( attributes )
4651 end
4752
48- defp add_object_type ( params , stripe_event ) do
49- object_type = stripe_event . data . object . object
50- params |> Map . put ( :object_type , object_type )
51- end
53+ # NOTE: unlike object_id, object_type should never be nil
54+ # Due to that, we do not have a catch-all clause for the nil case
55+ # If it ever is nil, it should fail and we should know about it
56+ defp add_object_type ( params , % { data: % { object: % { object: object } } } ) , do: params |> Map . put ( :object_type , object )
5257
53- defp add_object_id ( params , stripe_event ) do
54- params |> Map . put ( :object_id , stripe_event . data . object . id )
55- end
58+ defp add_object_id ( params , % { data: % { object: % { id: id } } } ) , do: params |> Map . put ( :object_id , id )
59+ defp add_object_id ( params , _stripe_event ) , do: params |> Map . put ( :object_id , nil )
5660end
0 commit comments