Skip to content

Commit

Permalink
finish up relay api
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed May 5, 2018
1 parent 4b47c6b commit 63dbb34
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
107 changes: 107 additions & 0 deletions lib/stripe/relay/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Stripe.Relay.Product do
"""

use Stripe.Entity
import Stripe.Request

@type t :: %__MODULE__{
id: Stripe.id(),
Expand All @@ -32,6 +33,8 @@ defmodule Stripe.Relay.Product do
},
shippable: boolean,
skus: Stripe.List.t(Stripe.SKU.t()),
statement_descriptor: String.t(),
unit_label: String.t(),
updated: Stripe.timestamp(),
url: String.t()
}
Expand All @@ -52,7 +55,111 @@ defmodule Stripe.Relay.Product do
:package_dimensions,
:shippable,
:skus,
:statement_descriptor,
:unit_label,
:updated,
:url
]

@plural_endpoint "products"

@doc """
Create a product.
"""
@spec create(params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
optional(:caption) => String.t(),
optional(:deactive_on) => [Stripe.id()],
optional(:description) => String.t(),
optional(:id) => String.t(),
optional(:images) => [Stripe.id()],
optional(:description) => String.t(),
optional(:attributes) => list,
:name => String.t(),
:type => String.t(),
optional(:metadata) => Stripe.Types.metadata(),
optional(:package_dimensions) => map,
optional(:shippable) => boolean,
optional(:url) => String.t()
} | %{}
def create(params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_params(params)
|> put_method(:post)
|> make_request()
end

@doc """
Retrieve a product.
"""
@spec retrieve(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def retrieve(id, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:get)
|> make_request()
end

@doc """
Update a product.
Takes the `id` and a map of changes.
"""
@spec update(Stripe.id() | t, params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
optional(:active) => boolean,
optional(:attributes) => list,
optional(:caption) => String.t(),
optional(:deactive_on) => [Stripe.id()],
optional(:description) => String.t(),
optional(:images) => [Stripe.id()],
optional(:metadata) => Stripe.Types.metadata(),
optional(:name) => String.t(),
optional(:package_dimensions) => map,
optional(:shippable) => boolean,
optional(:url) => String.t()
} | %{}
def update(id, params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:post)
|> put_params(params)
|> make_request()
end

@doc """
Delete a product.
"""
@spec delete(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def delete(id, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:delete)
|> make_request()
end

@doc """
List all product.
"""
@spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()}
when params: %{
optional(:active) => boolean,
optional(:created) => Stripe.date_query(),
optional(:ending_before) => t | Stripe.id(),
optional(:ids) => Stripe.List.t(Stripe.id()),
optional(:limit) => 1..100,
optional(:shippable) => boolean,
optional(:starting_after) => t | Stripe.id(),
optional(:type) => String.t(),
optional(:url) => String.t()
} | %{}
def list(params \\ %{}, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_method(:get)
|> put_params(params)
|> cast_to_id([:ending_before, :starting_after])
|> make_request()
end
end
42 changes: 42 additions & 0 deletions test/stripe/relay/product_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule Stripe.Relay.ProductTest do
use Stripe.StripeCase, async: true

describe "create/2" do
test "creates an invoice" do
assert {:ok, %Stripe.Product{}} = Stripe.Product.create(%{name: "Plus", type: "service"})
assert_stripe_requested(:post, "/v1/products")
end
end

describe "retrieve/2" do
test "retrieves an invoice" do
assert {:ok, %Stripe.Product{}} = Stripe.Product.retrieve("Plus")
assert_stripe_requested(:get, "/v1/products/Plus")
end
end

describe "update/2" do
test "updates an invoice" do
params = %{metadata: %{key: "value"}}
assert {:ok, %Stripe.Product{}} = Stripe.Product.update("Plus", params)
assert_stripe_requested(:post, "/v1/products/Plus")
end
end

describe "list/2" do
test "lists all products" do
assert {:ok, %Stripe.List{data: products}} = Stripe.Product.list()
assert_stripe_requested(:get, "/v1/products")
assert is_list(products)
assert %Stripe.Product{} = hd(products)
end
end

describe "delete/1" do
test "deletes a product" do
{:ok, product} = Stripe.Product.retrieve("Plus")
assert {:ok, _} = Stripe.Product.delete("Plus")
assert_stripe_requested(:delete, "/v1/products/#{product.id}")
end
end
end

0 comments on commit 63dbb34

Please sign in to comment.