2023-10-03 18:14:42 +02:00
|
|
|
class DataSources::AdresseController < ApplicationController
|
|
|
|
def search
|
|
|
|
if params[:q].present? && params[:q].length > 3
|
2023-10-13 16:06:00 +02:00
|
|
|
response = fetch_results
|
|
|
|
|
|
|
|
if response.success?
|
|
|
|
results = JSON.parse(response.body, symbolize_names: true)
|
|
|
|
|
|
|
|
render json: format_results(results)
|
|
|
|
else
|
|
|
|
render json: []
|
|
|
|
end
|
2023-10-03 18:14:42 +02:00
|
|
|
else
|
|
|
|
render json: []
|
|
|
|
end
|
|
|
|
end
|
2023-10-13 16:06:00 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_results
|
|
|
|
Typhoeus.get("#{API_ADRESSE_URL}/search", params: { q: params[:q], limit: 10 })
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_results(results)
|
|
|
|
results[:features].map do
|
|
|
|
{
|
|
|
|
label: _1[:properties][:label],
|
|
|
|
value: _1[:properties][:label],
|
2024-02-13 13:40:33 +01:00
|
|
|
data: _1
|
2023-10-13 16:06:00 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2023-10-03 18:14:42 +02:00
|
|
|
end
|