Skip to content

Commit

Permalink
Merge branch 'master' into smoove-gbfs-version
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti authored Oct 20, 2021
2 parents d7d083c + 90fb678 commit 8a2d13e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
13 changes: 3 additions & 10 deletions apps/transport/lib/opendatasoft/url_extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,6 @@ defmodule Opendatasoft.UrlExtractor do

@doc """
Get a download from a CSVs if it exists
## Examples
iex> [{"name,file\\ntoulouse,http", %{"id" => "bob"}}, {"stop,lon,lat\\n1,48.8,2.3", %{"id" => "bobette"}}]
...> |> UrlExtractor.get_resources_with_url_from_csv()
{:ok, [%{"url" => "http", "title" => "http", "id" => "bob"}]}
iex> UrlExtractor.get_resources_with_url_from_csv([{"stop,lon,lat\\n1,48.8,2.3", %{"id" => "bob"}}])
{:error, "No url found"}
"""
@spec get_resources_with_url_from_csv([{binary, map()}]) ::
{:ok, [map()]} | {:error, binary()}
Expand Down Expand Up @@ -277,7 +268,9 @@ defmodule Opendatasoft.UrlExtractor do

@spec get_filename(binary()) :: binary()
defp get_filename(url) do
with {:ok, %HTTPoison.Response{headers: headers}} <- HTTPoison.head(url),
httpoison_impl = Transport.Shared.Wrapper.HTTPoison.impl()

with {:ok, %HTTPoison.Response{headers: headers}} <- httpoison_impl.head(url),
{_, content} <- Enum.find(headers, fn {h, _} -> h == "Content-Disposition" end),
%{"filename" => filename} <- Regex.named_captures(~r/filename="(?<filename>.*)"/, content) do
filename
Expand Down
1 change: 1 addition & 0 deletions apps/transport/test/transport/import_data_service_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Transport.ImportDataServiceTest do

setup do
Mox.stub_with(Datagouvfr.Client.CommunityResources.Mock, Datagouvfr.Client.StubCommunityResources)
Mox.stub_with(Transport.HTTPoison.Mock, HTTPoison)
:ok
end

Expand Down
18 changes: 18 additions & 0 deletions apps/transport/test/transport/url_extractor_doc_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
defmodule Transport.UrlExtractorDocTest do
use ExUnit.Case, async: true
alias Opendatasoft.UrlExtractor
import Mox
doctest UrlExtractor

test "get urls in CSV file" do
Transport.HTTPoison.Mock |> expect(:head, fn _url -> %HTTPoison.Response{} end)

res =
[{"name,file\ntoulouse,http", %{"id" => "bob"}}, {"stop,lon,lat\n1,48.8,2.3", %{"id" => "bobette"}}]
|> UrlExtractor.get_resources_with_url_from_csv()

assert res == {:ok, [%{"url" => "http", "title" => "http", "id" => "bob"}]}
end

test "get urls in CSV file : no url available" do
Transport.HTTPoison.Mock |> expect(:head, fn _url -> %HTTPoison.Response{} end)

assert UrlExtractor.get_resources_with_url_from_csv([{"stop,lon,lat\\n1,48.8,2.3", %{"id" => "bob"}}]) ==
{:error, "No url found"}
end
end

0 comments on commit 8a2d13e

Please sign in to comment.