Skip to content
Open
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: 10 additions & 0 deletions lib/open_cagex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ defmodule OpenCagex do
end
end

@doc """
Returns details from opencagedata request for the given place name
"""
def detail(place_name) do
case Api.get_by_place_name(place_name) do
{:ok, response} -> Parser.detail(response)
{:error, error} -> {:error, error}
end
end

@doc """
Sets an API key
"""
Expand Down
26 changes: 25 additions & 1 deletion lib/open_cagex/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ defmodule OpenCagex.Parser do

def geocode(results), do: get_by_key(results, "geometry")

def detail(results) do
with {:ok, geometry} <- get_by_key(results, "geometry"),
{:ok, callingcode} <- get_by_key_from_annotations(results, "callingcode"),
{:ok, currency} <- get_by_key_from_annotations(results, "currency"),
{:ok, timezone} <- get_by_key_from_annotations(results, "timezone"),
{:ok, roadinfo} <- get_by_key_from_annotations(results, "roadinfo"),
{:ok, sun} <- get_by_key_from_annotations(results, "sun")
do
{:ok, %{
geometry: geometry,
callingcode: callingcode,
currency: currency,
timezone: timezone,
roadinfo: roadinfo,
sun: sun
}}
end
end

def formatted_address(results), do: get_by_key(results, "formatted")

defp get_by_key(results, key) do
Expand All @@ -16,10 +35,15 @@ defmodule OpenCagex.Parser do
{:ok, value}
end

defp get_by_key_from_annotations(results, key) do
{:ok, annotations} = get_by_key(results, "annotations")
{:ok, annotations |> get_key(key)}
end

defp parse_results(%{"results" => results}), do: results

defp first_result(results), do: Enum.at(results, 0)

defp get_key(nil, key), do: nil
defp get_key(nil, _key), do: nil
defp get_key(results, key), do: Map.get(results, key)
end
91 changes: 90 additions & 1 deletion test/opencagex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,100 @@ defmodule OpenCagexTest do
describe "when given a place name" do
test "returns a geocode" do
result = %{"lat" => 41.377655, "lng" => 2.1780917}

with_request_mock %{geometry: result} do
expected_result = {:ok, result}
assert expected_result == OpenCagex.geocode("Passatge de la Pau, Barcelona")
end
end

test "returns full details" do
response = %{
geometry: %{"lat" => 41.377655, "lng" => 2.1780917},
annotations: %{
currency: %{
"alternate_symbols" => [],
"decimal_mark" => ",",
"html_entity" => "&#x20AC;",
"iso_code" => "EUR",
"iso_numeric" => "978",
"name" => "Euro",
"smallest_denomination" => 1,
"subunit" => "Cent",
"subunit_to_unit" => 100,
"symbol" => "€",
"symbol_first" => 0,
"thousands_separator" => "."
},
callingcode: 34,
timezone: %{
"name" => "Europe/Madrid",
"now_in_dst" => 0,
"offset_sec" => 3600,
"offset_string" => "+0100",
"short_name" => "CET"
},
roadinfo: %{"drive_on" => "right", "speed_in" => "km/h"},
sun: %{
"rise" => %{
"apparent" => 1606892400,
"astronomical" => 1606886520,
"civil" => 1606890540,
"nautical" => 1606888500
},
"set" => %{
"apparent" => 1606926060,
"astronomical" => 1606932000,
"civil" => 1606927920,
"nautical" => 1606929960
}
}
}
}
result = %{
geometry: %{"lat" => 41.377655, "lng" => 2.1780917},
currency: %{
"alternate_symbols" => [],
"decimal_mark" => ",",
"html_entity" => "&#x20AC;",
"iso_code" => "EUR",
"iso_numeric" => "978",
"name" => "Euro",
"smallest_denomination" => 1,
"subunit" => "Cent",
"subunit_to_unit" => 100,
"symbol" => "€",
"symbol_first" => 0,
"thousands_separator" => "."
},
callingcode: 34,
timezone: %{
"name" => "Europe/Madrid",
"now_in_dst" => 0,
"offset_sec" => 3600,
"offset_string" => "+0100",
"short_name" => "CET"
},
roadinfo: %{"drive_on" => "right", "speed_in" => "km/h"},
sun: %{
"rise" => %{
"apparent" => 1606892400,
"astronomical" => 1606886520,
"civil" => 1606890540,
"nautical" => 1606888500
},
"set" => %{
"apparent" => 1606926060,
"astronomical" => 1606932000,
"civil" => 1606927920,
"nautical" => 1606929960
}
}
}

with_request_mock response do
expected_result = {:ok, result}
assert expected_result == OpenCagex.detail("Passatge de la Pau, Barcelona")
end
end
end
end