review(maj): strip la valeur recherché sur les api chorus avant de le soumettre à l'API, et quelques maj de style

Co-authored-by: Colin Darie <colin@darie.eu>
This commit is contained in:
mfo 2023-10-19 15:19:23 +02:00 committed by Martin
parent a4ef3cdf23
commit 0922e0987a
4 changed files with 38 additions and 48 deletions

View file

@ -2,48 +2,32 @@ class DataSources::ChorusController < ApplicationController
before_action :authenticate_administrateur!
def search_domaine_fonct
@result = APIBretagneService.new.search_domaine_fonct(code_or_label: params[:q])
result_json = @result.map do |item|
{
label: ChorusConfiguration.format_domaine_fonctionnel_label(item),
value: "#{item[:label]} - #{item[:code_programme]}",
data: item
}
end
render json: result_json
result_json = APIBretagneService.new.search_domaine_fonct(code_or_label: params[:q])
render json: format_result(result_json:,
label_formatter: ChorusConfiguration.method(:format_domaine_fonctionnel_label))
end
def search_centre_couts
@result = APIBretagneService.new.search_centre_couts(code_or_label: params[:q])
result_json = @result.map do |item|
{
label: ChorusConfiguration.format_domaine_fonctionnel_label(item),
value: "#{item[:label]} - #{item[:code_programme]}",
data: item
}
end
render json: result_json
result_json = APIBretagneService.new.search_centre_couts(code_or_label: params[:q])
render json: format_result(result_json:,
label_formatter: ChorusConfiguration.method(:format_centre_de_coup_label))
end
def search_ref_programmation
@result = APIBretagneService.new.search_ref_programmation(code_or_label: params[:q])
result_json = @result.map do |item|
result_json = APIBretagneService.new.search_ref_programmation(code_or_label: params[:q])
render json: format_result(result_json:,
label_formatter: ChorusConfiguration.method(:format_ref_programmation_label))
end
private
def format_result(result_json:, label_formatter:)
result_json.map do |item|
{
label: ChorusConfiguration.format_domaine_fonctionnel_label(item),
label: label_formatter.call(item),
value: "#{item[:label]} - #{item[:code_programme]}",
data: item
}
end
render json: result_json
end
# def search
# if params[:q].present? && params[:q].length > 3
# response = Typhoeus.get("#{API_ADRESSE_URL}/search", params: { q: params[:q], limit: 10 })
# result = JSON.parse(response.body, symbolize_names: true)
# render json: result[:features].map { { label: _1[:properties][:label], value: _1[:properties][:label] } }
# else
# render json: []
# end
# end
end
end