diff --git a/config/dev.exs b/config/dev.exs index 82fae0c..3d7ed8f 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -2,3 +2,4 @@ import Config config(:onigumo, :http_client, HTTPoison) config(:onigumo, :downloader, Onigumo.Downloader) +config(:onigumo, :parser, Onigumo.Parser) diff --git a/lib/cli.ex b/lib/cli.ex index 6a76a11..4ace1e4 100644 --- a/lib/cli.ex +++ b/lib/cli.ex @@ -1,6 +1,7 @@ defmodule Onigumo.CLI do @components %{ - :downloader => Application.compile_env(:onigumo, :downloader) + :downloader => Application.compile_env(:onigumo, :downloader), + :parser => Application.compile_env(:onigumo, :parser) } def main(argv) do diff --git a/lib/onigumo/parser.ex b/lib/onigumo/parser.ex new file mode 100644 index 0000000..9691253 --- /dev/null +++ b/lib/onigumo/parser.ex @@ -0,0 +1,26 @@ +defmodule Onigumo.Parser do + @moduledoc """ + Web scraper + """ + @behaviour Onigumo.Component + + @impl Onigumo.Component + def main(root_path) do + root_path + |> list_downloaded() + |> Enum.map(&IO.puts(&1)) + + :ok + end + + def list_downloaded(path) do + path + |> File.ls!() + |> Enum.filter(&is_downloaded(&1)) + end + + def is_downloaded(path) do + suffix = Application.get_env(:onigumo, :downloaded_suffix) + Path.extname(path) == suffix + end +end diff --git a/test/onigumo_parser_test.exs b/test/onigumo_parser_test.exs new file mode 100644 index 0000000..c225f92 --- /dev/null +++ b/test/onigumo_parser_test.exs @@ -0,0 +1,72 @@ +defmodule OnigumoParserTest do + use ExUnit.Case + import Mox + + @suffixes [ + ".json", + "" + ] + + @files [ + {[], []}, + {["first", "second"], []}, + {["first"], ["second"]}, + {[], ["first", "second"]}, + ] + + setup(:verify_on_exit!) + + describe("Onigumo.Parser.main/1") do + @tag :tmp_dir + test("run Parser", %{tmp_dir: tmp_dir}) do + _result = Onigumo.Parser.main(tmp_dir) + assert(false) # TODO: Do! + end + + @tag :tmp_dir + test("return :ok", %{tmp_dir: tmp_dir}) do + result = Onigumo.Parser.main(tmp_dir) + assert(result == :ok) + end + end + + describe("Onigumo.Parser.list_downloaded/1") do + for {expected, unexpected} <- @files do + @tag :tmp_dir + test("list a directory expecting #{inspect(expected)} and not expecting #{inspect(unexpected)}", %{tmp_dir: tmp_dir}) do + suffix = Application.get_env(:onigumo, :downloaded_suffix) + + expected = Enum.map(unquote(expected), fn file -> + file <> suffix + end) + + Enum.map(expected ++ unquote(unexpected), fn file -> + path = Path.join(tmp_dir, file) + File.write!(path, "") + end) + + result = Onigumo.Parser.list_downloaded(tmp_dir) + assert(MapSet.new(result) == MapSet.new(expected)) + end + end + end + + describe("Onigumo.Parser.is_downloaded/1") do + test("recognize a downloaded file") do + suffix = Application.get_env(:onigumo, :downloaded_suffix) + path = "/test_dir/test_file#{suffix}" + + result = Onigumo.Parser.is_downloaded(path) + assert(result == true) + end + + for suffix <- @suffixes do + test("do not recognize another file #{inspect(suffix)}") do + path = "/test_dir/test_file#{unquote(suffix)}" + + result = Onigumo.Parser.is_downloaded(path) + assert(result == false) + end + end + end +end