Skip to content

Commit

Permalink
Auto transform all the responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mfeckie committed May 23, 2018
1 parent 3bf08d3 commit 1d552b8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 78 deletions.
4 changes: 1 addition & 3 deletions lib/balance.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule PINXS.Balance do
alias PINXS.HTTP.API
alias PINXS.Response
alias __MODULE__

@moduledoc """
Expand All @@ -18,7 +17,6 @@ defmodule PINXS.Balance do
"""
@spec get() :: {:ok, Balance.t()} | {:error, PINXS.Error.t()}
def get() do
API.get("/balance")
|> Response.transform(__MODULE__)
API.get("/balance", __MODULE__)
end
end
4 changes: 1 addition & 3 deletions lib/bank_accounts/bank_account.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule PINXS.BankAccounts.BankAccount do
alias PINXS.HTTP.API
alias PINXS.Response
alias __MODULE__

@derive [Poison.Encoder]
Expand Down Expand Up @@ -28,7 +27,6 @@ defmodule PINXS.BankAccounts.BankAccount do
"""
@spec create(BankAccount.t) :: {:ok, BankAccount.t} | {:error, PINXS.Error.t}
def create(%BankAccount{} = bank_account) do
API.post("/bank_accounts", bank_account)
|> Response.transform(__MODULE__)
API.post("/bank_accounts", bank_account, __MODULE__)
end
end
4 changes: 1 addition & 3 deletions lib/cards/card.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule PINXS.Cards.Card do
alias PINXS.HTTP.API
alias PINXS.Response
alias __MODULE__

@moduledoc """
Expand Down Expand Up @@ -56,7 +55,6 @@ defmodule PINXS.Cards.Card do
"""
@spec create(Card.t()) :: {:ok, Card.t()} | {:error, PINXS.Error.t()}
def create(%Card{} = card) do
API.post("/cards", card)
|> Response.transform(__MODULE__)
API.post("/cards", card, __MODULE__)
end
end
16 changes: 5 additions & 11 deletions lib/charges/charge.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule PINXS.Charges.Charge do
alias PINXS.HTTP.API
alias PINXS.Response
alias __MODULE__

@moduledoc """
Expand Down Expand Up @@ -75,8 +74,7 @@ defmodule PINXS.Charges.Charge do
Captures a previously authorized charge
"""
def capture(%Charge{token: token}, amount \\ %{}) do
API.put("/charges/#{token}/capture", amount)
|> Response.transform(__MODULE__)
API.put("/charges/#{token}/capture", amount, __MODULE__)
end

@doc """
Expand All @@ -98,34 +96,30 @@ defmodule PINXS.Charges.Charge do
do: create_charge(charge_map)

defp create_charge(charge_map) do
API.post("/charges", charge_map)
|> Response.transform(__MODULE__)
API.post("/charges", charge_map, __MODULE__)
end

@doc """
Retreives a paginated list of charges
"""
@spec get_all() :: {:ok, [Charge.t]} | {:error, PINXS.Error.t}
def get_all() do
API.get("/charges")
|> Response.transform(__MODULE__)
API.get("/charges", __MODULE__)
end

@doc """
Retreives a specific pages of charges
"""
@spec get_all(integer()) :: {:ok, [Charge.t]} | {:error, PINXS.Error.t}
def get_all(page) do
API.get("/charges?page=#{page}")
|> Response.transform(__MODULE__)
API.get("/charges?page=#{page}", __MODULE__)
end

@doc """
Retreives a single charge
"""
@spec get(String.t()) :: {:ok, Charge.t} | {:error, PINXS.Error.t()}
def get(token) do
API.get("/charges/#{token}")
|> Response.transform(__MODULE__)
API.get("/charges/#{token}", __MODULE__)
end
end
34 changes: 11 additions & 23 deletions lib/customers/customer.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule PINXS.Customers.Customer do
alias PINXS.HTTP.API
alias PINXS.Response
alias PINXS.Cards.Card
alias PINXS.Charges.Charge
alias __MODULE__
Expand Down Expand Up @@ -40,13 +39,11 @@ defmodule PINXS.Customers.Customer do
"""
@spec add_card(Customer.t(), Card.t()) :: {:ok, Card.t()} | {:error, PINXS.Error.t()}
def add_card(%Customer{token: token}, %Card{} = card) do
API.post("/customers/#{token}/cards", card)
|> Response.transform(Card)
API.post("/customers/#{token}/cards", card, Card)
end

def add_card(%Customer{token: token}, card_token) when is_binary(card_token) do
API.post("/customers/#{token}/cards", %{card_token: card_token})
|> Response.transform(Card)
API.post("/customers/#{token}/cards", %{card_token: card_token}, Card)
end

@doc """
Expand All @@ -60,17 +57,15 @@ defmodule PINXS.Customers.Customer do
do: create_customer(customer)

defp create_customer(customer) do
API.post("/customers", customer)
|> Response.transform(__MODULE__)
API.post("/customers", customer, __MODULE__)
end

@doc """
Deletes a customer
"""
@spec delete(Customer.t()) :: {:ok, true} | {:error, PINXS.Error.t()}
def delete(%Customer{token: token}) do
API.delete("/customers/#{token}")
|> Response.transform(__MODULE__)
API.delete("/customers/#{token}", __MODULE__)
end

@doc """
Expand All @@ -79,53 +74,47 @@ defmodule PINXS.Customers.Customer do
@spec delete_card(Customer.t(), String.t()) ::
{:ok, Customer.t()} | {:error, PINXS.Error.t()}
def delete_card(%Customer{token: token}, card_token) do
API.delete("/customers/#{token}/cards/#{card_token}")
|> Response.transform(__MODULE__)
API.delete("/customers/#{token}/cards/#{card_token}", __MODULE__)
end

@doc """
Retreives a customer
"""
@spec get(String.t()) :: {:ok, Customer.t()} | {:error, PINXS.Error.t()}
def get(token) do
API.get("/customers/#{token}")
|> Response.transform(__MODULE__)
API.get("/customers/#{token}", __MODULE__)
end

@doc """
Retrieves a paginated list of customers
"""
@spec get_all() :: {:ok, [Customer.t()]} | {:error, PINXS.Error.t()}
def get_all() do
API.get("/customers")
|> Response.transform(__MODULE__)
API.get("/customers", __MODULE__)
end

@doc """
Retreives a specific page of customers
"""
@spec get_all(integer()) :: {:ok, [Customer.t()]} | {:error, PINXS.Error.t()}
def get_all(page) when is_integer(page) do
API.get("/customers?page=#{page}")
|> Response.transform(__MODULE__)
API.get("/customers?page=#{page}", __MODULE__)
end

@doc """
Retrieves all cards for the given customer
"""
@spec get_cards(Customer.t()) :: {:ok, [Card.t()]} | {:error, PINXS.Error.t()}
def get_cards(%Customer{token: token}) do
API.get("/customers/#{token}/cards")
|> Response.transform(Card)
API.get("/customers/#{token}/cards", Card)
end

@doc """
Retrieves all charges for customer
"""
@spec get_charges(Customer.t()) :: {:ok, [Charge.t]} | {:error, PINXS.Error.t()}
def get_charges(%Customer{token: token}) do
API.get("/customers/#{token}/charges")
|> Response.transform(PINXS.Charges.Charge)
API.get("/customers/#{token}/charges", PINXS.Charges.Charge)
end

# TODO Add 'Subscriptions'
Expand All @@ -135,7 +124,6 @@ defmodule PINXS.Customers.Customer do
"""
@spec update(Customer.t(), map()) :: {:ok, Customer.t()} | {:error, PINXS.Error.t()}
def update(%Customer{token: token}, params) when not is_nil(token) do
API.put("/customers/#{token}", params)
|> Response.transform(__MODULE__)
API.put("/customers/#{token}", params, __MODULE__)
end
end
5 changes: 5 additions & 0 deletions lib/http/api.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule PINXS.HTTP.API do
alias PINXS.HTTP.ClientBase, as: Client
alias PINXS.Response

@moduledoc """
The API module provides low level functions for communicating with
Expand All @@ -10,7 +11,11 @@ defmodule PINXS.HTTP.API do
"""

def delete(url), do: Client.delete(url)
def delete(url, module), do: delete(url) |> Response.transform(module)
def get(url), do: Client.get(url)
def get(url, module), do: get(url) |> Response.transform(module)
def post(url, params), do: Client.post(url, params)
def post(url, params, module), do: post(url, params) |> Response.transform(module)
def put(url, params), do: Client.put(url, params)
def put(url, params, module), do: put(url, params) |> Response.transform(module)
end
19 changes: 6 additions & 13 deletions lib/recipients/recipient.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule PINXS.Recipients.Recipient do
alias PINXS.HTTP.API
alias PINXS.Response
alias __MODULE__

@moduledoc """
Expand Down Expand Up @@ -40,13 +39,11 @@ defmodule PINXS.Recipients.Recipient do
"""
@spec create(Recipient.t()) :: {:ok, Recipient.t()} | {:error, PINXS.Error.t()}
def create(%Recipient{bank_account: bank_account} = recipient) when not is_nil(bank_account) do
API.post("/recipients", recipient)
|> Response.transform(__MODULE__)
API.post("/recipients", recipient, __MODULE__)
end

def create(%Recipient{bank_account_token: bank_account_token} = recipient) when not is_nil(bank_account_token) do
API.post("/recipients", recipient)
|> Response.transform(__MODULE__)
API.post("/recipients", recipient, __MODULE__)
end


Expand All @@ -55,32 +52,28 @@ defmodule PINXS.Recipients.Recipient do
"""
@spec get(String.t) :: {:ok, Recipient.t} | {:error, PINXS.Error.t}
def get(recipient_token) do
API.get("/recipients/#{recipient_token}")
|> Response.transform(__MODULE__)
API.get("/recipients/#{recipient_token}", __MODULE__)
end

@doc """
Gets a paginated list of recipients
"""
@spec get_all() :: {:ok, [Recipient.t]} | {:error, PINXS.Error.t}
def get_all() do
API.get("/recipients")
|> Response.transform(__MODULE__)
API.get("/recipients", __MODULE__)
end

@doc """
Get a specific page of recipients
"""
def get_all(page) do
API.get("/recipients?page=#{page}")
|> Response.transform(__MODULE__)
API.get("/recipients?page=#{page}", __MODULE__)
end

@doc """
Update recipient details
"""
def update_recipient(%Recipient{ token: token}, params) do
API.put("/recipients/#{token}", params)
|> Response.transform(__MODULE__)
API.put("/recipients/#{token}", params, __MODULE__)
end
end
16 changes: 5 additions & 11 deletions lib/refunds/refund.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule PINXS.Refunds.Refund do
alias PINXS.HTTP.API
alias PINXS.Response
alias PINXS.Charges.Charge
alias __MODULE__

Expand Down Expand Up @@ -36,43 +35,38 @@ defmodule PINXS.Refunds.Refund do
"""
@spec create(Charge.t, map()) :: {:ok, Refund.t} | {:error, PINXS.Error.t}
def create(%Charge{token: token}, amount \\ %{}) do
API.post("/charges/#{token}/refunds", amount)
|> Response.transform(__MODULE__)
API.post("/charges/#{token}/refunds", amount, __MODULE__)
end

@doc """
Retrieves a specific refunds
"""
@spec get(Refund.t) :: {:ok, Refund.t} | {:error, PINXS.Error.t}
def get(%Refund{token: token}) do
API.get("/refunds/#{token}")
|> Response.transform(__MODULE__)
API.get("/refunds/#{token}", __MODULE__)
end

@doc """
Get a paginated list of refunds
"""
@spec get_all() :: {:ok, [Refund.t]} | {:error, PINXS.Error.t}
def get_all() do
API.get("/refunds")
|> Response.transform(__MODULE__)
API.get("/refunds", __MODULE__)
end
@doc """
Gets a specific page of refunds
"""
@spec get_all(integer()) :: {:ok, [Refund.t]} | {:error, PINXS.Error.t}
def get_all(page) when is_integer(page) do
API.get("/refunds?page=#{page}")
|> Response.transform(__MODULE__)
API.get("/refunds?page=#{page}", __MODULE__)
end

@doc """
Gets refunds for a specific charge
"""
@spec get_all_for_charge(Charge.t) :: {:ok, [Refund.t]} | {:error, PINXS.Error.t}
def get_all_for_charge(%Charge{token: token}) do
API.get("/charges/#{token}/refunds")
|> Response.transform(__MODULE__)
API.get("/charges/#{token}/refunds", __MODULE__)
end


Expand Down
Loading

0 comments on commit 1d552b8

Please sign in to comment.