Skip to content

Commit

Permalink
setup default shipping modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mithereal committed Nov 23, 2022
1 parent 5861e3e commit b791158
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ config :shippex,
currency: :usd, # :usd, :can, :mxn, :eur
carriers: [
ups: [
module: Shippex.Carrier.UPS,
module: Shippex.Carrier.UPS, # optional
username: "MyUsername",
password: "MyPassword",
secret_key: "123123",
Expand Down
18 changes: 14 additions & 4 deletions lib/shippex/carrier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Shippex.Carrier do
function for fetching the Carrier module.
"""

alias Shippex.{Shipment, Service, Rate, Transaction}
alias Shippex.{Shipment, Service, Rate, Transaction, Util}

@callback fetch_rates(Shipment.t()) :: [{atom, Rate.t()}]
@callback fetch_rate(Shipment.t(), Service.t()) :: [{atom, Rate.t()}] | {atom, Rate.t()}
Expand All @@ -15,6 +15,7 @@ defmodule Shippex.Carrier do
@callback validate_address(Address.t()) :: {:ok, [Address.t()]} | {:error, any()}
@callback track_packages(String.t() | [String.t()]) :: {:ok | :error, any()}
@callback services_country?(ISO.country_code()) :: boolean()
@callback carrier() :: atom

@type t() :: atom()

Expand All @@ -31,13 +32,22 @@ defmodule Shippex.Carrier do
@spec module(atom | String.t()) :: module()
def module(carrier) when is_atom(carrier) do
# NOTE, this might be a good place to use a protocol?
default_modules = Util.get_modules()

carriers =
Application.get_env(:shippex, :carriers, [])
|> Enum.with_index(fn(c, i) ->
module = List.first(c).module
{i, module}
|> Enum.with_index(fn c, i ->
module = Keyword.get(c, :module)

case module do
nil ->
Enum.filter(default_modules, fn {_module, module_carrier} -> module_carrier == i end)

module ->
{i, module}
end
end)
|> Enum.reject(fn x -> x == nil end)

module =
case Enum.filter(carriers, fn {x, _} -> x == carrier end) do
Expand Down
7 changes: 5 additions & 2 deletions lib/shippex/carrier/dummy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ defmodule Shippex.Carrier.Dummy do
end

defp rate() do
{carrier, _} = carrier()
%Shippex.Rate{
service: %Shippex.Service{
id: :dummy,
carrier: carrier(),
carrier: carrier,
description: "Dummy Shipping Service"
},
price: 1000,
Expand All @@ -61,7 +62,9 @@ defmodule Shippex.Carrier.Dummy do
defp shipment() do
end

defp carrier() do
@impl true
def carrier() do
:dummy
end

end
5 changes: 5 additions & 0 deletions lib/shippex/carrier/ups.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,9 @@ defmodule Shippex.Carrier.UPS do
raise InvalidConfigError, message: "USPS config is either invalid or not found."
end
end

@impl true
def carrier() do
:ups
end
end
5 changes: 5 additions & 0 deletions lib/shippex/carrier/usps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,9 @@ defmodule Shippex.Carrier.USPS do
raise InvalidConfigError, message: "UPS config is either invalid or not found."
end
end

@impl true
def carrier() do
:usps
end
end
26 changes: 26 additions & 0 deletions lib/shippex/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,30 @@ defmodule Shippex.Util do
{key, val}
end
end

@doc """
Returns the mane and module tuple.
iex> Util.get_modules()
{%{}, :module_name}
"""
def get_modules() do
{:ok, modules} = :application.get_key(:shippex, :modules)

modules
|> Stream.map(&Module.split/1)
|> Stream.filter(fn module ->
case module do
["Shippex", "Carrier", _] -> true
["Shippex", "Carrier", "_", "Client"] -> false
_ -> false
end
end)
# concat
|> Stream.map(&Module.concat/1)
|> Stream.map(&{&1, apply(&1, :carrier, [])})
|> Enum.map(fn output ->
output
end)
end
end

0 comments on commit b791158

Please sign in to comment.