demarches-normaliennes/app/models/concerns/rna_champ_association_fetchable_concern.rb

30 lines
864 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module RNAChampAssociationFetchableConcern
extend ActiveSupport::Concern
2023-01-14 11:53:55 +01:00
attr_reader :association_fetch_error_key
def fetch_association!(rna)
self.value = rna
2023-01-14 11:53:55 +01:00
return clear_association!(:empty) if rna.empty?
return clear_association!(:invalid) unless valid_champ_value?
2023-01-14 11:53:55 +01:00
return clear_association!(:not_found) if (data = APIEntreprise::RNAAdapter.new(rna, procedure_id).to_params).blank?
update!(data: data, value_json: APIGeoService.parse_rna_address(data['adresse']))
2023-01-14 11:53:55 +01:00
rescue APIEntreprise::API::Error => error
2024-04-05 12:08:40 +02:00
error_key = :network_error if error.try(:network_error?) && !APIEntrepriseService.api_djepva_up?
2023-01-14 11:53:55 +01:00
clear_association!(error_key)
end
private
def clear_association!(error)
@association_fetch_error_key = error
self.data = nil
save(validate: false)
2023-01-14 11:53:55 +01:00
false
end
end