-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from retromeet/add_postgis_and_locations
Add postgis and locations
- Loading branch information
Showing
34 changed files
with
696 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module API | ||
module Entities | ||
# Represents the profile info entity for the API | ||
class LocationResult < Grape::Entity | ||
expose :latitude, documentation: { type: Float } | ||
expose :longitude, documentation: { type: Float } | ||
expose :display_name, documentation: { type: String } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
module Models | ||
LocationResult = Data.define(:latitude, :longitude, :display_name, :osm_id, :country_code, :language) do | ||
def initialize(latitude:, longitude:, display_name:, osm_id:, country_code:, language:) | ||
country_code = country_code.downcase | ||
super | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# This module automatically chooses the best Location Service based on the requested language | ||
module LocationServiceProxy | ||
class << self | ||
# @param query [String] A query to be sent to nominatim | ||
# @param limit [Integer] The max results to return | ||
# @param language [String] The language for the results | ||
# @return [Array<Models::LocationResult>] | ||
def search(query:, limit: nil, language: "en") | ||
if PhotonClient.language_supported?(language) | ||
limit = PhotonClient::MAX_SEARCH_RESULTS if limit.nil? | ||
PhotonClient.search(query:, limit:, language:) | ||
else | ||
limit = NominatimClient::MAX_SEARCH_RESULTS if limit.nil? | ||
NominatimClient.search(query:, limit:, language:) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
# This modules contains functions to interact with the {https://photon.komoot.io/ Photon} backend. | ||
# The backend is configured through ENV variables and will use the default backend otherwise. | ||
module PhotonClient | ||
MAX_SEARCH_RESULTS = 10 | ||
|
||
class << self | ||
# @param query [String] A query to be sent to nominatim | ||
# @param limit [Integer] The max results to return | ||
# @param language [String] The language for the results, can only be one of +supported_languages+. | ||
# @return [Array<Models::LocationResult>] | ||
def search(query:, limit: MAX_SEARCH_RESULTS, language: "en") | ||
language = normalize_language(language) | ||
params = { | ||
q: CGI.escape(query), | ||
lang: language, | ||
limit: | ||
} | ||
layers = "layer=state&layer=county&layer=city&layer=district" | ||
query_params = params.map { |k, v| "#{k}=#{v}" } | ||
query_params << layers | ||
query_params = query_params.join("&") | ||
results = Sync do | ||
response = client.get("/api?#{query_params}", headers: base_headers) | ||
JSON.parse(response.read, symbolize_names: true) | ||
ensure | ||
response&.close | ||
end | ||
results[:features].map do |place| | ||
components = place[:properties].slice(*AddressComposer::AllComponents) | ||
components[:country_code] = place[:properties][:countrycode] | ||
components[place[:properties][:osm_value]] = place[:properties][:name] | ||
longitude, latitude = place[:geometry][:coordinates] | ||
display_name = AddressComposer.compose(components) | ||
display_name.chomp! | ||
display_name.gsub!("\n", ", ") | ||
Models::LocationResult.new( | ||
latitude:, | ||
longitude:, | ||
display_name:, | ||
osm_id: place[:properties][:osm_id], | ||
country_code: components[:country_code], | ||
language: | ||
) | ||
end | ||
end | ||
|
||
# @param language (see .search) | ||
# @return [Boolean] | ||
def language_supported?(language) = supported_languages.include?(language) | ||
|
||
private | ||
|
||
# The public photon API only supports a few languages (en, fr, de) | ||
# to support more languages another photon instance has to be used with a custom import | ||
# This will normalize any language not supported by the default endpoint to one of the supported ones | ||
# you can override the supported languages with the env variable defined below | ||
# | ||
# @param language (see .search) | ||
# @return [String] | ||
def normalize_language(language) | ||
return language if language_supported?(language) | ||
|
||
supported_languages.first | ||
end | ||
|
||
# @return [Array<String>] | ||
def supported_languages | ||
@supported_languages ||= ENV.fetch("PHOTON_SUPPORTED_LANGUAGES", "en,fr,de").split(",") | ||
end | ||
|
||
# Returns the photon host to be used for requests | ||
# @return [Async::HTTP::Endpoint] | ||
def photon_host = @photon_host ||= Async::HTTP::Endpoint.parse(ENV.fetch("PHOTON_API_HOST", "https://photon.komoot.io")) | ||
|
||
# @return [Hash] Base headers to be used for requests | ||
def base_headers = @base_headers ||= { "Content-Type" => "application/json", "User-Agent": RetroMeet::Version.user_agent }.freeze | ||
|
||
# @return [Async::HTTP::Client] | ||
def client = Async::HTTP::Client.new(photon_host) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.