New to elixir and I only have found 2 ways to run code. Wondering if there's a better way:
open interactive shell and run one or multiple functions inside a module by calling
Module_name.function_name()
which just runs that functionIf I want to run the entire module then I open interactive shell and have to paste all the code from the module into the shell then hit
enter
I'm sure it's obvious but I can't find any satisfactory answers anywhere. Just tried option #2 with this module and it returned this when it should return a list of reviews. Thanks.
defmodule KgbScrape do
use HTTPoison.Base
@endpoint "https://www.dealerrater.com/dealer/McKaig-Chevrolet-Buick-A-Dealer-For-The-People-dealer-reviews-23685/page"
def build_urls() do
page_num = ["1","2","3","4","5"]
tail_url = ["/?filter=#link"]
for page <- page_num, tail <- tail_url do
urls_list = @endpoint <> page <> tail
end
end
def fetch_pages(url) do
url
|> HTTPoison.get()
|> response()
end
def process_pages(urls_list) when is_list(urls_list) do
resp =
urls_list
|> Task.async_stream(fn url -> fetch_pages(url) end)
|> Enum.map(fn {_, resp} -> resp end)
Enum.map(resp, fn r ->
r
|> Floki.parse_document!()
|> Floki.find(".review-entry")
|> Map.new(fn entry ->
[{"div", _, [date]}] = Floki.find(entry, "div.italic")
[{"p", _, [content]}] = Floki.find(entry, "p.review-content")
{date, content}
end)
end)
end
def response({:ok, %{body: {:ok, %{"error" => error}}}}) do
{:error, error}
end
def response({:ok, %{body: body}}), do: body
def response({:error, error}), do: {:error, error}
end
and result is:
warning: redefining module KgbScrape (current version loaded from _build/dev/lib/kgbScrapeApp/ebin/Elixir.KgbScrape.beam)
iex:7
warning: variable "urls_list" is unused (if the variable is not meant to be used, prefix it with an underscore)
iex:15: KgbScrape.build_urls/0
{:module, KgbScrape,
<<70, 79, 82, 49, 0, 0, 68, 252, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0,
5, 107, 0, 0, 0, 103, 16, 69, 108, 105, 120, 105, 114, 46, 75, 103,
98, 83, 99, 114, 97, 112, 101, 8, 95, 95, 105, 110, 102, ...>>,
{:response, 1}}
Create a
mix
project and useiex -S mix
as described in the documentation to load all the modules in the project.The outcome you see is the result of module compilation. To use the module one must explicitly call a function, like
KgbScrape.process_pages(["url1", "url2, ...])
.Also, one might create an
escript
withmix escript.build
and run the resulting executable.