Only reject api entreprise data if required fields are missing

This commit is contained in:
Paul Chavard 2018-10-04 16:22:19 +02:00
parent 9e737992c4
commit 1302b68d91
6 changed files with 42 additions and 16 deletions

View file

@ -1,4 +1,6 @@
class ApiEntreprise::Adapter
UNAVAILABLE = 'Donnée indisponible'
def initialize(siret, procedure_id)
@siret = siret
@procedure_id = procedure_id
@ -17,4 +19,8 @@ class ApiEntreprise::Adapter
{}
end
end
def valid_params?(params)
!params.has_value?(UNAVAILABLE)
end
end

View file

@ -34,10 +34,7 @@ class ApiEntreprise::API
params: params,
timeout: TIMEOUT)
# Responses with a 206 codes are sometimes not useable,
# as the RNA calls often return a 206 with an error message,
# not a partial response
if response.success? && response.code != 206
if response.success?
JSON.parse(response.body, symbolize_names: true)
else
raise RestClient::ResourceNotFound

View file

@ -8,8 +8,13 @@ class ApiEntreprise::EntrepriseAdapter < ApiEntreprise::Adapter
def process_params
params = data_source[:entreprise].slice(*attr_to_fetch)
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
params.transform_keys { |k| :"entreprise_#{k}" }
if valid_params?(params)
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
params.transform_keys { |k| :"entreprise_#{k}" }
else
{}
end
end
def attr_to_fetch

View file

@ -7,10 +7,15 @@ class ApiEntreprise::EtablissementAdapter < ApiEntreprise::Adapter
def process_params
params = data_source[:etablissement].slice(*attr_to_fetch)
adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n")
params.merge!(params[:adresse].slice(*address_attr_to_fetch))
params[:adresse] = adresse_line
params
if valid_params?(params)
adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n")
params.merge!(params[:adresse].slice(*address_attr_to_fetch))
params[:adresse] = adresse_line
params
else
{}
end
end
def attr_to_fetch

View file

@ -10,7 +10,11 @@ class ApiEntreprise::ExercicesAdapter < ApiEntreprise::Adapter
exercice.slice(*attr_to_fetch)
end
{ exercices_attributes: exercices_array }
if exercices_array == exercices_array.select { |params| valid_params?(params) }
{ exercices_attributes: exercices_array }
else
{}
end
end
def attr_to_fetch

View file

@ -6,17 +6,26 @@ class ApiEntreprise::RNAAdapter < ApiEntreprise::Adapter
end
def process_params
if data_source[:association][:id].present?
params = data_source[:association].slice(*attr_to_fetch)
params[:rna] = data_source[:association][:id]
params.transform_keys { |k| :"association_#{k}" }
else
# Responses with a 206 codes are sometimes not useable,
# as the RNA calls often return a 206 with an error message,
# not a partial response
if !data_source.key?(:association)
{}
else
params = data_source[:association].slice(*attr_to_fetch)
if params[:id].present? && valid_params?(params)
params[:rna] = params[:id]
params.except(:id).transform_keys { |k| :"association_#{k}" }
else
{}
end
end
end
def attr_to_fetch
[
:id,
:titre,
:objet,
:date_creation,