diff --git a/elixir/sample_mix.exs b/elixir/sample_mix.exs new file mode 100644 index 0000000..4cffc1c --- /dev/null +++ b/elixir/sample_mix.exs @@ -0,0 +1,26 @@ +defmodule Elixir.MixProject do + use Mix.Project + + def project do + [ + app: :elixir, + version: "0.1.0", + elixir: "~> 1.12", + compilers: Mix.compilers(), + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # ... + + def deps do + [ + # TODO: Add this into your project and check the latest version from: https://hexdocs.pm/aws/readme.html#installation + {:aws, "~> 0.13.0"}, + {:hackney, "~> 1.18"} + ] + end + + # ... +end diff --git a/elixir/services/textract.ex b/elixir/services/textract.ex new file mode 100644 index 0000000..01cbea0 --- /dev/null +++ b/elixir/services/textract.ex @@ -0,0 +1,47 @@ +defmodule Elixir.Textract do + @moduledoc """ + A sample code example in Elixir. + """ + + @doc """ + Analyze document. + > Copy this function into your Elixir project and copy the `deps/0` in `mix_sample.exs` into your `mix.exs`. + + ## Examples + # Run this to try out `analyze_document/3`/`analyze_document/4` + YourProject.Textract.analyze_document("access_key", "secret_key", "region", "path") + + > More explanations here: https://docs.aws.amazon.com/textract/latest/dg/how-it-works-analyzing.html + + Example provided by @enkr1 | enkr99@gmail.com + + """ + def analyze_document(access_key, secret_key, region, png_path \\ "/path/to/png") do + case AWS.Client.create(access_key, secret_key, region) + |> AWS.Textract.analyze_document(%{ + "Document" => %{"Bytes" => png_path |> File.read!() |> Base.encode64()}, + "FeatureTypes" => ["TABLES", "FORMS"] + }) do + { + :ok, + %{ + "AnalyzeDocumentModelVersion" => _, + "Blocks" => blocks, + "DocumentMetadata" => _ + } = _result_map, + response = _map_with_encoded_body_str + } -> + # ... + response |> IO.inspect(label: "response") + + {:error, {_key, %{body: reason}} = reason_tupple} -> + # ... + reason_tupple |> IO.inspect(label: "reason_tupple") + {:error, reason} + + edge_case -> + edge_case |> IO.inspect(label: "edge_case") + {:error, "Something went wrong when extracting the file ..."} + end + end +end