Reputation: 13
Good afternoon. I have a module to get the domain name from the link.
defmodule URIparser do
defstruct domains: []
def make_domain(uri) do
case URI.parse(uri) do
%URI{authority: nil} -> URI.parse("http://#{uri}")
%URI{authority: _} -> URI.parse(uri)
end
end
end
After that I use the pipeline and get the domain I need.
links = ["https://www.google.com/search?newwindow=1&sxsrf", "https://stackoverflow.com/questions/ask", "yahoo.com"]
Enum.each(links, fn(x) -> URIparser.make_domain(x) |> Map.take([:authority]) |> Map.values |> IO.inspect end)
That's what happens in the end:
["google.com"]
["stackoverflow.com"]
["yahoo.com"]
:ok
Please tell us how to complement the pipeline and to put all domains into one list. Other solutions are also available.
Example:
%{domains: ["google.com", "stackoverflow.com", "yahoo.com"]}
Upvotes: 1
Views: 54
Reputation: 10663
Instead of Enum.each
and Map.take
, use Enum.map
and Map.get
Enum.map(links, fn x ->
x
|> URIparser.make_domain()
|> Map.get(:authority)
end)
Upvotes: 1