initial commit

This commit is contained in:
FloatingGhost 2022-08-28 20:32:46 +01:00
commit d5177b4495
8 changed files with 128 additions and 0 deletions

4
.formatter.exs Normal file
View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

26
.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/
*.iml
# If you run "mix test --cover", coverage assets end up here.
/cover/
# The directory Mix downloads your dependencies sources to.
/deps/
# Where third-party dependencies like ExDoc output generated docs.
/doc/
# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
deep_lex-*.tar
# Temporary files, for example, from tests.
/tmp/

21
README.md Normal file
View File

@ -0,0 +1,21 @@
# DeepLex
**TODO: Add description**
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `deep_lex` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:deep_lex, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/deep_lex>.

39
lib/deep_lex.ex Normal file
View File

@ -0,0 +1,39 @@
defmodule DeepLex do
@moduledoc """
Documentation for `DeepLex`.
"""
use Tesla
@type translation :: %{detected_source_language: String.t(), text: String.t()}
@type response :: %{translations: [translation]}
plug Tesla.Middleware.EncodeFormUrlencoded
plug Tesla.Middleware.DecodeJson, engine_opts: [keys: :atoms!]
defp base_url(:free) do
"https://api-free.deepl.com/v2/"
end
defp base_url(:pro) do
"https://api-free.deepl.com/v2/"
end
@spec translate(String.t(), String.t(), String.t(), String.t()) ::
{:ok, response} | {:error, String.t()}
def translate(api_key, tier, string, to_language) do
with {:ok, response} <- request(api_key, tier, string, to_language) do
{:ok, response.body}
else
{:error, reason} -> {:error, reason}
end
end
defp request(api_key, tier, string, to_language) do
post(base_url(tier) <> "translate", %{
auth_key: api_key,
text: string,
target_lang: to_language
})
end
end

28
mix.exs Normal file
View File

@ -0,0 +1,28 @@
defmodule DeepLex.MixProject do
use Mix.Project
def project do
[
app: :deep_lex,
version: "0.1.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:tesla, "~> 1.4.4"},
{:jason, "~> 1.3.0"}
]
end
end

5
mix.lock Normal file
View File

@ -0,0 +1,5 @@
%{
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"tesla": {:hex, :tesla, "1.4.4", "bb89aa0c9745190930366f6a2ac612cdf2d0e4d7fff449861baa7875afd797b2", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.3", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "d5503a49f9dec1b287567ea8712d085947e247cb11b06bc54adb05bfde466457"},
}

4
test/deep_lex_test.exs Normal file
View File

@ -0,0 +1,4 @@
defmodule DeepLexTest do
use ExUnit.Case
doctest DeepLex
end

1
test/test_helper.exs Normal file
View File

@ -0,0 +1 @@
ExUnit.start()