Skip to content

Commit

Permalink
Refactor out helpers for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Nov 12, 2016
1 parent 221907f commit 04ab6d4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 129 deletions.
29 changes: 1 addition & 28 deletions lib/stripe/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,6 @@ defmodule Stripe.Account do

@spec do_retrieve(String.t, list) :: {:ok, t} | {:error, Exception.t}
defp do_retrieve(endpoint, opts \\ []) do
IO.inspect opts
case Stripe.request(:get, endpoint, %{}, %{}, opts) do
{:ok, result} -> {:ok, to_struct(result)}
{:error, error} -> {:error, error}
end
end

defp to_struct(response) do
%__MODULE__{
id: Map.get(response, "id"),
business_name: Map.get(response, "business_name"),
business_primary_color: Map.get(response, "business_primary_color"),
business_url: Map.get(response, "business_url"),
charges_enabled: Map.get(response, "charges_enabled"),
country: Map.get(response, "country"),
default_currency: Map.get(response, "default_currency"),
details_submitted: Map.get(response, "details_submitted"),
display_name: Map.get(response, "display_name"),
email: Map.get(response, "email"),
managed: Map.get(response, "managed"),
metadata: Map.get(response, "metadata"),
statement_descriptor: Map.get(response, "statement_descriptor"),
support_email: Map.get(response, "support_email"),
support_phone: Map.get(response, "support_phone"),
support_url: Map.get(response, "support_url"),
timezone: Map.get(response, "timezone"),
transfers_enabled: Map.get(response, "transfers_enabled")
}
Stripe.Request.retrieve(endpoint, %__MODULE__{}, opts)
end
end
24 changes: 3 additions & 21 deletions lib/stripe/card.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ defmodule Stripe.Card do
@spec retrieve(source, String.t, String.t, Keyword.t) :: {:ok, t} | {:error, Exception.t}
def retrieve(owner_type, owner_id, card_id, opts \\ []) do
endpoint = endpoint_for_owner(owner_type, owner_id) <> "/" <> card_id

case Stripe.request(:get, endpoint, %{}, %{}, opts) do
{:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)}
{:error, error} -> {:error, error}
end
Stripe.Request.retrieve(endpoint, %__MODULE__{}, opts)
end

@doc """
Expand All @@ -106,17 +102,7 @@ defmodule Stripe.Card do
@spec update(source, String.t, String.t, map, Keyword.t) :: {:ok, t} | {:error, Exception.t}
def update(owner_type, owner_id, card_id, changes, opts \\ []) do
endpoint = endpoint_for_owner(owner_type, owner_id) <> "/" <> card_id

card =
changes
|> Util.map_keys_to_atoms()
|> Map.take(@valid_update_keys)
|> Util.drop_nil_keys()

case Stripe.request(:post, endpoint, card, %{}, opts) do
{:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)}
{:error, error} -> {:error, error}
end
Stripe.Request.update(endpoint, changes, @valid_update_keys, %__MODULE__{}, opts)
end

@doc """
Expand All @@ -125,10 +111,6 @@ defmodule Stripe.Card do
@spec delete(source, String.t, String.t, Keyword.t) :: :ok | {:error, Exception.t}
def delete(owner_type, owner_id, card_id, opts \\ []) do
endpoint = endpoint_for_owner(owner_type, owner_id) <> "/" <> card_id

case Stripe.request(:delete, endpoint, %{}, %{}, opts) do
{:ok, _} -> :ok
{:error, error} -> {:error, error}
end
Stripe.Request.delete(endpoint, opts)
end
end
54 changes: 4 additions & 50 deletions lib/stripe/customer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,7 @@ defmodule Stripe.Customer do
@spec create(t, Keyword.t) :: {:ok, t} | {:error, Exception.t}
def create(customer, opts \\ []) do
endpoint = @plural_endpoint

customer =
Map.from_struct(customer)
|> Map.take(@valid_create_keys)
|> Util.drop_nil_keys()

case Stripe.request(:post, endpoint, customer, %{}, opts) do
{:ok, result} -> {:ok, to_struct(result)}
{:error, error} -> {:error, error}
end
Stripe.Request.create(endpoint, customer, @valid_create_keys, %__MODULE__{}, opts)
end

@doc """
Expand All @@ -58,10 +49,7 @@ defmodule Stripe.Customer do
@spec retrieve(binary, Keyword.t) :: {:ok, t} | {:error, Exception.t}
def retrieve(id, opts \\ []) do
endpoint = @plural_endpoint <> "/" <> id
case Stripe.request(:get, endpoint, %{}, %{}, opts) do
{:ok, result} -> {:ok, to_struct(result)}
{:error, error} -> {:error, error}
end
Stripe.Request.retrieve(endpoint, %__MODULE__{}, opts)
end

@doc """
Expand All @@ -72,17 +60,7 @@ defmodule Stripe.Customer do
@spec update(t, map, list) :: {:ok, t} | {:error, Exception.t}
def update(id, changes, opts \\ []) do
endpoint = @plural_endpoint <> "/" <> id

customer =
changes
|> Util.map_keys_to_atoms()
|> Map.take(@valid_update_keys)
|> Util.drop_nil_keys()

case Stripe.request(:post, endpoint, customer, %{}, opts) do
{:ok, result} -> {:ok, to_struct(result)}
{:error, error} -> {:error, error}
end
Stripe.Request.update(endpoint, changes, @valid_update_keys, %__MODULE__{}, opts)
end

@doc """
Expand All @@ -91,30 +69,6 @@ defmodule Stripe.Customer do
@spec delete(binary, list) :: :ok | {:error, Exception.t}
def delete(id, opts \\ []) do
endpoint = @plural_endpoint <> "/" <> id

case Stripe.request(:delete, endpoint, %{}, %{}, opts) do
{:ok, _} -> :ok
{:error, error} -> {:error, error}
end
end

defp to_struct(response) do
%__MODULE__{
id: Map.get(response, "id"),
account_balance: Map.get(response, "account_balance"),
business_vat_id: Map.get(response, "business_vat_id"),
created: Util.get_date(response, "created"),
currency: Map.get(response, "currency"),
default_source: Map.get(response, "default_source"),
delinquent: Map.get(response, "delinquent"),
description: Map.get(response, "description"),
discount: Map.get(response, "discount"),
email: Map.get(response, "email"),
livemode: Map.get(response, "livemode"),
metadata: Map.get(response, "metadata"),
shipping: Map.get(response, "shipping"),
sources: Map.get(response, "sources"),
subscriptions: Map.get(response, "subscriptions")
}
Stripe.Request.delete(endpoint, opts)
end
end
34 changes: 4 additions & 30 deletions lib/stripe/plan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,7 @@ defmodule Stripe.Plan do
@spec create(t, Keyword.t) :: stripe_response
def create(plan, opts \\ []) do
endpoint = @plural_endpoint

plan =
plan
|> Map.take(@valid_create_keys)
|> Util.drop_nil_keys()

case Stripe.request(:post, endpoint, plan, %{}, opts) do
{:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)}
{:error, error} -> {:error, error}
end
Stripe.Request.create(endpoint, plan, @valid_create_keys, %__MODULE__{}, opts)
end

@doc """
Expand All @@ -58,10 +49,7 @@ defmodule Stripe.Plan do
@spec retrieve(binary, Keyword.t) :: stripe_response
def retrieve(id, opts \\ []) do
endpoint = @plural_endpoint <> "/" <> id
case Stripe.request(:get, endpoint, %{}, %{}, opts) do
{:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)}
{:error, error} -> {:error, error}
end
Stripe.Request.retrieve(endpoint, %__MODULE__{}, opts)
end

@doc """
Expand All @@ -72,17 +60,7 @@ defmodule Stripe.Plan do
@spec update(t, map, list) :: stripe_response
def update(id, changes, opts \\ []) do
endpoint = @plural_endpoint <> "/" <> id

plan =
changes
|> Util.map_keys_to_atoms()
|> Map.take(@valid_update_keys)
|> Util.drop_nil_keys()

case Stripe.request(:post, endpoint, plan, %{}, opts) do
{:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)}
{:error, error} -> {:error, error}
end
Stripe.Request.update(endpoint, changes, @valid_update_keys, %__MODULE__{}, opts)
end

@doc """
Expand All @@ -91,10 +69,6 @@ defmodule Stripe.Plan do
@spec delete(binary, list) :: stripe_delete_response
def delete(id, opts \\ []) do
endpoint = @plural_endpoint <> "/" <> id

case Stripe.request(:delete, endpoint, %{}, %{}, opts) do
{:ok, _} -> :ok
{:error, error} -> {:error, error}
end
Stripe.Request.delete(endpoint, opts)
end
end
49 changes: 49 additions & 0 deletions lib/stripe/request.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule Stripe.Request do
alias Stripe.Util

@type stripe_response :: {:ok, struct} | {:error, Exception.t}
@type stripe_delete_response :: :ok | {:error, Exception.t}

@spec create(String.t, struct, map, struct, Keyword.t) :: stripe_response
def create(endpoint, struct, valid_keys, return_struct, opts) do
body =
struct
|> Map.take(valid_keys)
|> Util.drop_nil_keys()

case Stripe.request(:post, endpoint, body, %{}, opts) do
{:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)}
{:error, error} -> {:error, error}
end
end

@spec retrieve(String.t, struct, Keyword.t) :: stripe_response
def retrieve(endpoint, return_struct, opts) do
case Stripe.request(:get, endpoint, %{}, %{}, opts) do
{:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)}
{:error, error} -> {:error, error}
end
end

@spec update(String.t, map, map, struct, Keyword.t) :: stripe_response
def update(endpoint, changes, valid_keys, return_struct, opts) do
body =
changes
|> Util.map_keys_to_atoms()
|> Map.take(valid_keys)
|> Util.drop_nil_keys()

case Stripe.request(:post, endpoint, body, %{}, opts) do
{:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)}
{:error, error} -> {:error, error}
end
end

@spec delete(String.t, Keyword.t) :: stripe_delete_response
def delete(endpoint, opts) do
case Stripe.request(:delete, endpoint, %{}, %{}, opts) do
{:ok, _} -> :ok
{:error, error} -> {:error, error}
end
end
end

0 comments on commit 04ab6d4

Please sign in to comment.