Skip to content

Instantly share code, notes, and snippets.

@knewter
Created May 23, 2014 01:16
Show Gist options
  • Save knewter/881a1fb5919708ba1623 to your computer and use it in GitHub Desktop.
Save knewter/881a1fb5919708ba1623 to your computer and use it in GitHub Desktop.
:!mix test test/website_pipeline_test.exs
test/website_pipeline_test.exs:1: warning: redefining module WebsitePipeline
test/website_pipeline_test.exs:2: warning: variable 'fun' is unused
** (CompileError) test/website_pipeline_test.exs:4: function 'fun'/1 undefined
(stdlib) lists.erl:1336: :lists.foreach/2
(stdlib) erl_eval.erl:657: :erl_eval.do_apply/6
(stdlib) erl_eval.erl:121: :erl_eval.exprs/5
(elixir) src/elixir.erl:156: :elixir.erl_eval/2
(elixir) src/elixir.erl:149: :elixir.eval_forms/4
(elixir) src/elixir.erl:156: :elixir.erl_eval/2
shell returned 1
defmodule WebsitePipeline do
def map(sites, fun) do
sites
|> Enum.map(fn(url) -> Task.async(fn -> url |> get_body |> fun end) end)
|> Enum.map(&Task.await/1)
end
def get_body(url) do
IO.puts url
HTTPotion.get(url).body
end
defmodule Util do
def extract_title(html) do
title_pattern = ~r"<title>([^<]*)</title>"
Regex.run(title_pattern, html) |> Enum.at(1)
end
end
end
defmodule WebsitePipelineTest do
use ExUnit.Case
alias WebsitePipeline.Util
test "Mapping a pipeline of websites" do
sites = ["http://example.org/", "http://slashdot.org", "http://elixir-lang.org", "http://www.erlang.org"]
expected_titles = ["Example Domain", "Slashdot: News for nerds, stuff that matters", "Elixir", "Erlang Programming Language"]
assert expected_titles == WebsitePipeline.map(sites, &Util.extract_title/1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment