From 3fa056a1a35815fc6519a8fbbf0d0bc2a3772ae4 Mon Sep 17 00:00:00 2001 From: Danylo Negriienko Date: Mon, 6 Jul 2020 17:21:23 +0300 Subject: [PATCH] detect(nil) return error --- README.md | 2 +- lib/translator/microsoft.ex | 10 +++++----- lib/translator/yandex.ex | 10 +++++----- mix.exs | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index acdddaa..b64ffd0 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ by adding `localizator` to your list of dependencies in `mix.exs`: ```elixir def deps do [ - {:localizator, "~> 0.1.15"} + {:localizator, "~> 0.1.16"} ] end ``` diff --git a/lib/translator/microsoft.ex b/lib/translator/microsoft.ex index 80b78c4..c3d87da 100644 --- a/lib/translator/microsoft.ex +++ b/lib/translator/microsoft.ex @@ -8,6 +8,10 @@ defmodule Localizator.Translator.Microsoft do @behaviour Localizator.Translator.Base + @impl true + @spec detect(nil) :: {:error, message} + def detect(nil), do: {:error, "Couldn't detect language"} + @impl true @spec detect(text) :: {:ok, locale} | {:error, message} def detect(text) when is_bitstring(text) do @@ -21,11 +25,7 @@ defmodule Localizator.Translator.Microsoft do @spec detect(map) :: {:ok, locale} | {:error, message} def detect(map) when is_map(map) do {:ok, sample} = Map.fetch(map, List.first(Map.keys(map))) - - case MicrosoftTranslator.detect(sample) do - %{languageCode: locale} -> {:ok, locale} - %{} -> {:error, "Couldn't detect language"} - end + detect(sample) end @impl true diff --git a/lib/translator/yandex.ex b/lib/translator/yandex.ex index b2ac203..089aa81 100644 --- a/lib/translator/yandex.ex +++ b/lib/translator/yandex.ex @@ -8,6 +8,10 @@ defmodule Localizator.Translator.Yandex do @behaviour Localizator.Translator.Base + @impl true + @spec detect(nil) :: {:error, message} + def detect(nil), do: {:error, "Couldn't detect language"} + @impl true @spec detect(text) :: {:ok, locale} | {:error, message} def detect(text) when is_bitstring(text) do @@ -21,11 +25,7 @@ defmodule Localizator.Translator.Yandex do @spec detect(map) :: {:ok, locale} | {:error, message} def detect(map) when is_map(map) do {:ok, sample} = Map.fetch(map, List.first(Map.keys(map))) - - case YandexTranslate.detect(sample) do - %{languageCode: locale} -> {:ok, locale} - %{} -> {:error, "Couldn't detect language"} - end + detect(sample) end @impl true diff --git a/mix.exs b/mix.exs index a8bab2b..6810418 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Localizator.MixProject do def project do [ app: :localizator, - version: "0.1.15", + version: "0.1.16", elixir: "~> 1.10", start_permanent: Mix.env() == :prod, deps: deps()