Fix for distance (#16)

* Fix for distance

Fixes the `distance` to search not being passed through to `ImageReverse.images()`

`set_scraper_cache` passes the `distance` through into `params`
`create` normalizes and inserts the result into the params passed to `ImageReverse.images()`
`normalize_dist` checks if there is a `distance` set, adds a leading 0 to it (to account for values like `.5`, which would crash `Float.parse()`), and turns it back into a well-formatted string for `ImageReverse.images()`

* Fixed space
This commit is contained in:
SomewhatDamaged 2020-01-04 02:37:20 +11:00 committed by liamwhite
parent efb28184a3
commit 8e39c06940

View file

@ -10,6 +10,7 @@ defmodule PhilomenaWeb.Api.Json.Search.ReverseController do
def create(conn, %{"image" => image_params}) do def create(conn, %{"image" => image_params}) do
images = images =
image_params image_params
|> Map.put("distance", conn.params["distance"])
|> ImageReverse.images() |> ImageReverse.images()
|> Enum.map(&ImageJson.as_json(conn, &1)) |> Enum.map(&ImageJson.as_json(conn, &1))
@ -21,8 +22,17 @@ defmodule PhilomenaWeb.Api.Json.Search.ReverseController do
params = params =
conn.params conn.params
|> Map.put("image", %{}) |> Map.put("image", %{})
|> Map.put("distance", normalize_dist(conn.params))
|> Map.put("scraper_cache", conn.params["url"]) |> Map.put("scraper_cache", conn.params["url"])
%{conn | params: params} %{conn | params: params}
end end
defp normalize_dist(%{"distance" => distance}) do
"0" <> distance
|> Float.parse()
|> elem(0)
|> Float.to_string()
end
defp normalize_dist(_dist), do: "0.25"
end end