Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function to create multiple requests from urls #10

Merged
merged 1 commit into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/crawly/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ defmodule Crawly.Utils do
A helper function which returns a Request structure for the given URL
"""
@spec request_from_url(binary()) :: Crawly.Request.t()
def request_from_url(url) do
%Crawly.Request{url: url, headers: []}
end
def request_from_url(url), do: %Crawly.Request{url: url, headers: []}

@doc """
A helper function which converts a list of URLS into a requests list.
"""
@spec requests_from_urls([binary()]) :: [Crawly.Request.t()]
def requests_from_urls(urls), do: Enum.map(urls, &request_from_url/1)

@doc """
Pipeline/Middleware helper
Expand Down
21 changes: 21 additions & 0 deletions test/utils_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule UtilsTest do
use ExUnit.Case

test "Request from url" do
requests = Crawly.Utils.request_from_url("https://test.com")
assert requests == %Crawly.Request{url: "https://test.com", headers: []}
end

test "Requests from urls" do
requests =
Crawly.Utils.requests_from_urls([
"https://test.com",
"https://example.com"
])

assert requests == [
%Crawly.Request{url: "https://test.com", headers: []},
%Crawly.Request{url: "https://example.com", headers: []}
]
end
end