Skip to content

Commit

Permalink
refact: filename processing
Browse files Browse the repository at this point in the history
This commit updates the ExCnab dependency version to "~> 1.3.4" to
incorporate the latest changes and improvements. Additionally, the
`run/2` function in the `ExCnab.Cnab240.Services.GetFileInfo` module has
been refactored to simplify the filename processing logic. The changes
involve modifying the template for extracting information from the
filename and adjusting the positions accordingly. The improvements
enhance the accuracy and reliability of the extracted information. No
known issues or consequences are expected with this change.

Fixes: #123
  • Loading branch information
eulixir committed Nov 30, 2023
1 parent 492f6b4 commit 112ad7f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In `mix.exs`, add the ExMachina dependency:
```elixir
def deps do
[
{:ex_cnab, "~> 1.3.3"},
{:ex_cnab, "~> 1.3.4"},
]
end
```
Expand Down
32 changes: 10 additions & 22 deletions app/excnab/cnab240/services/get_file_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,22 @@ defmodule ExCnab.Cnab240.Services.GetFileInfo do
alias ExCnab.Cnab240.Validator.Filename, as: FilenameValidator

@spec run(String.t(), Map.t()) :: {:ok, Map.t()} | {:error, String.t()}
def run(file, attrs \\ %{}) do
def run(file, _attrs \\ %{}) do
filename = Path.basename(file)
params = Map.get(attrs, :filename, filename)

params
|> String.slice(0..3)
|> String.match?(~r/\d+/)
|> filename_template(params)
filename
|> filename_template()
|> FilenameValidator.call()
end

defp filename_template(true, filename) do
defp filename_template(filename) do
%{
codigo_convenio: convert_position(filename, 1, 3),
dia_geracao_arquivo: convert_position(filename, 4, 5),
codigo_mes_geracao_arquivo: convert_position(filename, 6),
sequencia_arquivo: convert_position(filename, 7, 8),
formato_arquivo: convert_position(filename, 9, 12),
nome_arquivo: filename
}
end

defp filename_template(false, filename) do
%{
codigo_convenio: convert_position(filename, 1, 4),
dia_geracao_arquivo: convert_position(filename, 5, 6),
sequencia_arquivo: convert_position(filename, 7, 8),
formato_arquivo: convert_position(filename, 9, 12),
codigo_convenio: convert_position(filename, 1, 5),
dia_geracao_arquivo: convert_position(filename, 6, 7),
mes_geracao_arquivo: convert_position(filename, 8, 9),
ano_geracao_arquivo: convert_position(filename, 10, 13),
sequencia_arquivo: convert_position(filename, 14, 15),
formato_arquivo: convert_position(filename, 16, 19),
nome_arquivo: filename
}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ExCnab.MixProject do
def project do
[
app: :ex_cnab,
version: "1.3.3",
version: "1.3.4",
elixir: "~> 1.10",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit 112ad7f

Please sign in to comment.