Conversation
| @@ -0,0 +1,63 @@ | |||
| defmodule GithubWebscraping.ExtractFileInfos do | |||
| def fetch_file_name(html) do | |||
| file_name = | |||
There was a problem hiding this comment.
não precisa atribuir a var se vc ja esta retornando o resultado da ultima linha da função
| |> Floki.text() | ||
|
|
||
| cond do | ||
| String.contains?(string_bytes, "KB") -> |
There was a problem hiding this comment.
EU prefiro usar pattern matching ao inves de conds. entao criaria uma funcao auxiliar pra lidar com essas diferencas de codigo ai
There was a problem hiding this comment.
como eu iria fazer isso com pattern ? tem algum exemplo ?
There was a problem hiding this comment.
def get_number_bytes(string_bytes, "MB")
def get_number_bytes(string_bytes, "KB" )
def get_number_bytes(string_bytes, "GB" )
def get_number_bytes(string_bytes, "Bytes" )
E nessa linha ao inves de fazer o cond, vc faz:
get_number_bytes( string_bytes, String.contains?(string_bytes, "MB"))
Aproveitando, as linhas 1 e 2 de cada umas das conds sao iguais, ou seja, duplicidade de código que dá pra ser extraida pra funcoes novas :)
| """ | ||
| alias GithubWebscraping.MappingRepository | ||
|
|
||
| defdelegate get_repository_infos(url), to: MappingRepository, as: :process |
| end | ||
|
|
||
| defp get_all_lines(files) do | ||
| all_lines = Enum.reduce(files, 0, fn file, acc -> file.file_lines + acc end) |
There was a problem hiding this comment.
n precisa de duas linhas, nome do metodo já é suficiente pra entender
| end | ||
|
|
||
| defp get_all_bytes(files) do | ||
| all_bytes = Enum.reduce(files, 0, fn file, acc -> file.file_bytes + acc end) |
| Enum.filter(urls, fn url -> url =~ "tree" end) | ||
| end | ||
|
|
||
| defp is_first_url(url) do |
There was a problem hiding this comment.
first_url?(url) seria uma forma mais elixirzada, certo?
| Enum.filter(urls, fn url -> url =~ "blob" end) | ||
| end | ||
|
|
||
| defp get_pastes_url(urls) do |
There was a problem hiding this comment.
em minha defesa existe essa palavra em inglês
| @@ -0,0 +1,63 @@ | |||
| defmodule GithubWebscraping.ExtractFileInfos do | |||
There was a problem hiding this comment.
Esse modulo recebe um arquivo html ne ? olhando as funções, talvez daria pra chamar só de File. HTMLFile.get_name
| @@ -0,0 +1,8 @@ | |||
| defmodule Lib.GithubWebscraping do | |||
There was a problem hiding this comment.
ficaram 2 arquivos com o mesmo nome. Talvez esse poderia ser só NomeDoProjeto.Api mesmo
| bytes * 1_000_000.0 | ||
|
|
||
| String.contains?(string_bytes, "Bytes") -> | ||
| bytes = String.trim(List.last(List.last(Regex.scan(~r/(\d+)/, string_bytes)))) |
| @derive {Jason.Encoder, only: [:file_url, :file_name, :extension, :file_bytes, :file_lines]} | ||
| defstruct [:file_url, :file_name, :extension, :file_bytes, :file_lines] | ||
|
|
||
| def build(file_url, file_name, extension, file_bytes, file_lines) do |
There was a problem hiding this comment.
O elixir tem uma função struct(%GithubFile{}, [key: value]) que retorna um struct. Não precisaria do build
|
|
||
| alias Lib.GithubWebscraping | ||
|
|
||
| def index(conn, %{"github_url" => github_url}) do |
There was a problem hiding this comment.
Essa rota seria index mesmo? ou show recebendo url? user_path GET /users/:id HelloWeb.UserController :show
There was a problem hiding this comment.
|
|
||
| @spec process(String.t()) :: map() | ||
| def process(url) do | ||
| files = mapping_repository_by_url(url) |
| end | ||
|
|
||
| defp mapping_repository_by_url(url) do | ||
| urls = get_urls(is_first_url(url)) |
|
|
||
| other_files = | ||
| if Enum.count(pastes_url) > 0 do | ||
| Enum.map(pastes_url, fn url -> |
There was a problem hiding this comment.
💅 Enum.map(dirs, &mapping_repository_by_url/1)
| @@ -0,0 +1,84 @@ | |||
| defmodule GithubWebscraping.MappingRepository do | |||
There was a problem hiding this comment.
não sei se entendi o Mapping no nome. O que acha de algo como ExtractRepositoryInfo.run ...
| html | ||
| |> Floki.find("div.repository-content") | ||
| |> Floki.find("div.d-flex.flex-items-start.flex-shrink-0") | ||
| |> Floki.find("strong.final-path") | ||
| |> Floki.text() | ||
|
|
||
| Path.extname(file_name) |
There was a problem hiding this comment.
Isso seria o mesmo que:
html
|> Floki.find(...)
|> Floki.find(...)
|> Floki.find(...)
|> Floki.text()
|> Path.extname()?
| IO.puts("\n----------Returned files----------\n") | ||
| IO.inspect(files) | ||
| IO.puts("\n----------Returned urls-----------\n") | ||
| IO.inspect(pastes_url) |
There was a problem hiding this comment.
Eu n sei o quanto essa opinião vale num projeto Elixir, mas se eu estivesse fazendo isso em OO, criaria uma classe separada para lidar com "apresentação". Basicamente um Presenter, ou View.
Provavelmente faria um módulo separado para agrupar essa responsabilidade de output
There was a problem hiding this comment.
eu só coloquei isso para testes mesmo
There was a problem hiding this comment.
mas bom ponto de qualquer forma

Valei-me