Skip to content

Commit

Permalink
Remove datetime conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Jan 2, 2017
1 parent dbdf507 commit 9b2f3ee
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 71 deletions.
5 changes: 0 additions & 5 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ defmodule Stripe.Converter do

defp maybe_build_struct(nil, value), do: value
defp maybe_build_struct(_module, nil), do: nil
defp maybe_build_struct(DateTime, value) do
{:ok, value} = DateTime.from_unix(value)
value
end
defp maybe_build_struct(module, value) when is_map(value) do
stripe_map_to_struct(module, value)
end
Expand All @@ -45,7 +41,6 @@ defmodule Stripe.Converter do
defp convert_key(key) when is_atom(key), do: to_string(key)
defp convert_key(key) when is_binary(key), do: String.to_atom(key)

defp convert_value(%DateTime{} = value), do: value
defp convert_value(value) when is_map(value) do
Enum.reduce(value, %{}, fn({key, value}, acc) ->
Map.put(acc, String.to_atom(key), convert_value(value))
Expand Down
4 changes: 1 addition & 3 deletions lib/stripe/customer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ defmodule Stripe.Customer do
:metadata
]

@relationships %{
created: DateTime
}
@relationships %{}

@plural_endpoint "customers"

Expand Down
4 changes: 1 addition & 3 deletions lib/stripe/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ defmodule Stripe.Event do
:request, :type, :user_id
]

@relationships %{
created: DateTime
}
@relationships %{}

@plural_endpoint "events"

Expand Down
4 changes: 1 addition & 3 deletions lib/stripe/file_upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ defmodule Stripe.FileUpload do
:id, :object, :created, :purpose, :size, :type, :metadata
]

@relationships %{
created: DateTime
}
@relationships %{}

@plural_endpoint "files"

Expand Down
4 changes: 1 addition & 3 deletions lib/stripe/plan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ defmodule Stripe.Plan do
:livemode, :metadata, :name, :statement_descriptor, :trial_period_days
]

@relationships %{
created: DateTime
}
@relationships %{}

@plural_endpoint "plans"

Expand Down
17 changes: 1 addition & 16 deletions lib/stripe/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ defmodule Stripe.Subscription do
]

@relationships %{
created: DateTime,
current_period_end: DateTime,
current_period_start: DateTime,
ended_at: DateTime,
plan: Stripe.Plan,
start: DateTime,
trial_end: DateTime,
trial_start: DateTime
plan: Stripe.Plan
}

@plural_endpoint "subscriptions"
Expand Down Expand Up @@ -67,14 +60,6 @@ defmodule Stripe.Subscription do
:metadata
]

@doc """
Returns a map of relationship keys and their Struct name.
Relationships must be specified for the relationship to
be returned as a struct.
"""
@spec relationships :: Keyword.t
def relationships, do: @relationships

@doc """
Create a subscription.
"""
Expand Down
3 changes: 1 addition & 2 deletions lib/stripe/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ defmodule Stripe.Token do
]

@relationships %{
card: Stripe.Card,
created: DateTime
card: Stripe.Card
}

@plural_endpoint "tokens"
Expand Down
20 changes: 0 additions & 20 deletions lib/stripe/util.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
defmodule Stripe.Util do
@moduledoc false

@spec get_date(map, atom | String.t) :: DateTime.t | nil
def get_date(m, k) do
case Map.get(m, k) do
nil -> nil
ts -> datetime_from_timestamp(ts)
end
end

defp datetime_from_timestamp(ts) when is_binary ts do
ts = case Integer.parse ts do
:error -> 0
{i, _r} -> i
end
datetime_from_timestamp ts
end

defp datetime_from_timestamp(ts) when is_number ts do
DateTime.from_unix!(ts)
end

@doc """
Performs a root-level conversion of map keys from strings to atoms.
Expand Down
18 changes: 2 additions & 16 deletions test/stripe/converter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ defmodule Stripe.ConverterTest do
defmodule AuthToken do
defstruct [:id, :card, :client_ip, :created, :livemode, :type, :used]
def relationships, do: %{
card: ConverterTest.CreditCard,
created: DateTime
card: ConverterTest.CreditCard
}
end

Expand Down Expand Up @@ -83,20 +82,7 @@ defmodule Stripe.ConverterTest do
country: "US",
exp_month: 8
},
created: %DateTime{
calendar: Calendar.ISO,
day: 10,
hour: 18,
microsecond: {0, 0},
minute: 37,
month: 5,
second: 25,
std_offset: 0,
time_zone: "Etc/UTC",
utc_offset: 0,
year: 2016,
zone_abbr: "UTC"
}
created: 1462905445
}

result = Converter.stripe_map_to_struct(
Expand Down

0 comments on commit 9b2f3ee

Please sign in to comment.