Permalink
Browse files

Refactored out `Replacer.Pathfinder`

  • Loading branch information...
fredwu committed Aug 4, 2017
1 parent 3c6e37a commit 7ac06b40a6c75b5e01311e70710185011f82471f
Showing with 38 additions and 13 deletions.
  1. +7 −13 lib/crawler/replacer.ex
  2. +24 −0 lib/crawler/replacer/pathfinder.ex
  3. +7 −0 test/crawler/replacer/pathfinder_test.exs
@@ -1,6 +1,6 @@
defmodule Crawler.Replacer do
alias Crawler.Replacer.{Normaliser, Prefixer}
alias Crawler.{Parser, Snapper}
alias Crawler.Replacer.{Prefixer, Pathfinder}
alias Crawler.Parser

@doc """
## Examples
@@ -44,19 +44,13 @@ defmodule Crawler.Replacer do
{:ok, body}
end

defp get_link({_, url}, _opts), do: url
defp get_link({_, link}, _opts), do: link

defp modify_body(body, url, current_url) do
String.replace(body, url, modify_link(url, current_url))
defp modify_body(body, link, current_url) do
String.replace(body, link, modify_link(link, current_url))
end

defp modify_link(url, current_url) do
Prefixer.prefix(current_url) <> link_path(url, current_url)
end

defp link_path(url, current_url) do
current_url
|> Snapper.snap_domain
|> Normaliser.normalise(url)
defp modify_link(link, current_url) do
Prefixer.prefix(current_url) <> Pathfinder.find_path(link, current_url)
end
end
@@ -0,0 +1,24 @@
defmodule Crawler.Replacer.Pathfinder do
alias Crawler.{Snapper, Replacer.Normaliser}

@doc """
## Examples
iex> Pathfinder.find_path(
iex> "https://hello.world:8888/remote/dir",
iex> "https://cool.beans:7777/dir"
iex> )
"hello.world-8888/remote/dir"
iex> Pathfinder.find_path(
iex> "local/dir",
iex> "https://cool.beans:7777/dir"
iex> )
"cool.beans-7777/local/dir"
"""
def find_path(link, current_url) do
current_url
|> Snapper.snap_domain
|> Normaliser.normalise(link)
end
end
@@ -0,0 +1,7 @@
defmodule Crawler.Replacer.PathfinderTest do
use Crawler.TestCase, async: true

alias Crawler.Replacer.Pathfinder

doctest Pathfinder
end

0 comments on commit 7ac06b4

Please sign in to comment.