lib/hyperswitch.ex

defmodule Hyperswitch do
  @moduledoc """
  Documentation for `Hyperswitch`.
  """

  ## Module attributes

  @api_key Application.compile_env!(:hyperswitch, :api_key)

  ## Public functions

  @spec api_key() :: binary()
  def api_key do
    @api_key
  end

  @spec api_url() :: binary()
  def api_url do
    api_url = Application.get_env(:hyperswitch, :api_url, nil)
    environment = Application.get_env(:hyperswitch, :environment, :sandbox)

    cond do
      is_url(api_url) ->
        api_url

      environment == :sandbox ->
        "https://sandbox.hyperswitch.io"

      environment == :production ->
        raise "Not yet implemented!"

      true ->
        raise "Unable to determine the API url"
    end
  end

  ## Private functions

  @spec is_url(nil | binary()) :: boolean()
  defp is_url(value) when is_binary(value) do
    String.starts_with?(value, "http://") or String.starts_with?(value, "https://")
  end

  defp is_url(_value) do
    false
  end
end