Fix code readability issues
This commit is contained in:
committed by
Thomas Citharel
parent
20dfce5c83
commit
aed824f1aa
@@ -2,9 +2,11 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
@moduledoc """
|
||||
[Addok](https://github.com/addok/addok) backend.
|
||||
"""
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
require Logger
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
|
||||
require Logger
|
||||
|
||||
@behaviour Provider
|
||||
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
defmodule Mobilizon.Service.Geospatial do
|
||||
@moduledoc """
|
||||
Module to load the service adapter defined inside the configuration
|
||||
Module to load the service adapter defined inside the configuration.
|
||||
|
||||
See `Mobilizon.Service.Geospatial.Provider`
|
||||
See `Mobilizon.Service.Geospatial.Provider`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Returns the appropriate service adapter
|
||||
Returns the appropriate service adapter.
|
||||
|
||||
According to the config behind `config :mobilizon, Mobilizon.Service.Geospatial, service: Mobilizon.Service.Geospatial.Module`
|
||||
According to the config behind
|
||||
`config :mobilizon, Mobilizon.Service.Geospatial,
|
||||
service: Mobilizon.Service.Geospatial.Module`
|
||||
"""
|
||||
@spec service() :: module()
|
||||
def service(), do: Application.get_env(:mobilizon, __MODULE__) |> get_in([:service])
|
||||
@spec service :: module
|
||||
def service, do: get_in(Application.get_env(:mobilizon, __MODULE__), [:service])
|
||||
end
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
@moduledoc """
|
||||
Google Maps [Geocoding service](https://developers.google.com/maps/documentation/geocoding/intro)
|
||||
Google Maps [Geocoding service](https://developers.google.com/maps/documentation/geocoding/intro).
|
||||
|
||||
Note: Endpoint is hardcoded to Google Maps API
|
||||
Note: Endpoint is hardcoded to Google Maps API.
|
||||
"""
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
|
||||
require Logger
|
||||
|
||||
@behaviour Provider
|
||||
|
||||
@@ -8,8 +8,10 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
* `:open_data` Whether to use [Open Data or Licenced Data](https://developer.mapquest.com/documentation/open/).
|
||||
Defaults to `true`
|
||||
"""
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
|
||||
require Logger
|
||||
|
||||
@behaviour Provider
|
||||
|
||||
@@ -2,8 +2,10 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
@moduledoc """
|
||||
[Nominatim](https://wiki.openstreetmap.org/wiki/Nominatim) backend.
|
||||
"""
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
|
||||
require Logger
|
||||
|
||||
@behaviour Provider
|
||||
@@ -66,23 +68,22 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
|
||||
@spec process_data(map()) :: Address.t()
|
||||
defp process_data(%{"address" => address} = body) do
|
||||
try do
|
||||
%Address{
|
||||
country: Map.get(address, "country"),
|
||||
locality: Map.get(address, "city"),
|
||||
region: Map.get(address, "state"),
|
||||
description: description(body),
|
||||
floor: Map.get(address, "floor"),
|
||||
geom: [Map.get(body, "lon"), Map.get(body, "lat")] |> Provider.coordinates(),
|
||||
postal_code: Map.get(address, "postcode"),
|
||||
street: street_address(address),
|
||||
origin_id: "osm:" <> to_string(Map.get(body, "osm_id"))
|
||||
}
|
||||
rescue
|
||||
e in ArgumentError ->
|
||||
Logger.warn(inspect(e))
|
||||
nil
|
||||
end
|
||||
%Address{
|
||||
country: Map.get(address, "country"),
|
||||
locality: Map.get(address, "city"),
|
||||
region: Map.get(address, "state"),
|
||||
description: description(body),
|
||||
floor: Map.get(address, "floor"),
|
||||
geom: [Map.get(body, "lon"), Map.get(body, "lat")] |> Provider.coordinates(),
|
||||
postal_code: Map.get(address, "postcode"),
|
||||
street: street_address(address),
|
||||
origin_id: "osm:" <> to_string(Map.get(body, "osm_id"))
|
||||
}
|
||||
rescue
|
||||
error in ArgumentError ->
|
||||
Logger.warn(inspect(error))
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
@spec street_address(map()) :: String.t()
|
||||
|
||||
@@ -2,9 +2,11 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
@moduledoc """
|
||||
[Photon](https://photon.komoot.de) backend.
|
||||
"""
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
require Logger
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
|
||||
require Logger
|
||||
|
||||
@behaviour Provider
|
||||
|
||||
|
||||
@@ -14,11 +14,13 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
## Shared options
|
||||
|
||||
* `:user_agent` User-Agent string to send to the backend. Defaults to `"Mobilizon"`
|
||||
* `:lang` Lang in which to prefer results. Used as a request parameter or through an `Accept-Language` HTTP header.
|
||||
Defaults to `"en"`.
|
||||
* `:limit` Maximum limit for the number of results returned by the backend. Defaults to `10`
|
||||
* `:api_key` Allows to override the API key (if the backend requires one) set inside the configuration.
|
||||
* `:endpoint` Allows to override the endpoint set inside the configuration
|
||||
* `:lang` Lang in which to prefer results. Used as a request parameter or
|
||||
through an `Accept-Language` HTTP header. Defaults to `"en"`.
|
||||
* `:limit` Maximum limit for the number of results returned by the backend.
|
||||
Defaults to `10`
|
||||
* `:api_key` Allows to override the API key (if the backend requires one) set
|
||||
inside the configuration.
|
||||
* `:endpoint` Allows to override the endpoint set inside the configuration.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
@@ -35,38 +37,40 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
iex> geocode(48.11, -1.77)
|
||||
%Address{}
|
||||
"""
|
||||
@callback geocode(longitude :: number(), latitude :: number(), options :: keyword()) ::
|
||||
list(Address.t())
|
||||
@callback geocode(longitude :: number, latitude :: number, options :: keyword) :: [Address.t()]
|
||||
|
||||
@doc """
|
||||
Search for an address
|
||||
|
||||
## Options
|
||||
|
||||
In addition to [the shared options](#module-shared-options), `c:search/2` also accepts the following options:
|
||||
In addition to [the shared options](#module-shared-options), `c:search/2` also
|
||||
accepts the following options:
|
||||
|
||||
* `coords` Map of coordinates (ex: `%{lon: 48.11, lat: -1.77}`) allowing to give a geographic priority to the search.
|
||||
Defaults to `nil`
|
||||
* `coords` Map of coordinates (ex: `%{lon: 48.11, lat: -1.77}`) allowing to
|
||||
give a geographic priority to the search. Defaults to `nil`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> search("10 rue Jangot")
|
||||
%Address{}
|
||||
"""
|
||||
@callback search(address :: String.t(), options :: keyword()) :: list(Address.t())
|
||||
@callback search(address :: String.t(), options :: keyword) :: [Address.t()]
|
||||
|
||||
@doc """
|
||||
Returns a `Geo.Point` for given coordinates
|
||||
"""
|
||||
@spec coordinates(list(number()), number()) :: Geo.Point.t()
|
||||
@spec coordinates([number], number) :: Geo.Point.t()
|
||||
def coordinates(coords, srid \\ 4326)
|
||||
|
||||
def coordinates([x, y], srid) when is_number(x) and is_number(y),
|
||||
do: %Geo.Point{coordinates: {x, y}, srid: srid}
|
||||
def coordinates([x, y], srid) when is_number(x) and is_number(y) do
|
||||
%Geo.Point{coordinates: {x, y}, srid: srid}
|
||||
end
|
||||
|
||||
def coordinates([x, y], srid) when is_bitstring(x) and is_bitstring(y),
|
||||
do: %Geo.Point{coordinates: {String.to_float(x), String.to_float(y)}, srid: srid}
|
||||
def coordinates([x, y], srid) when is_bitstring(x) and is_bitstring(y) do
|
||||
%Geo.Point{coordinates: {String.to_float(x), String.to_float(y)}, srid: srid}
|
||||
end
|
||||
|
||||
@spec coordinates(any()) :: nil
|
||||
@spec coordinates(any) :: nil
|
||||
def coordinates(_, _), do: nil
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user