Mutualize some code on ApiEntreprise::Adapter

This commit is contained in:
gregoirenovel 2018-03-19 15:45:55 +01:00
parent 5f1394cd7f
commit 67cf614879
5 changed files with 38 additions and 42 deletions

View file

@ -9,4 +9,12 @@ class ApiEntreprise::Adapter
rescue
@data_source = nil
end
def to_params
if data_source.present?
process_params
else
{}
end
end
end

View file

@ -1,20 +1,16 @@
class ApiEntreprise::EntrepriseAdapter < ApiEntreprise::Adapter
def to_params
if data_source.present?
params = data_source[:entreprise].slice(*attr_to_fetch)
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
params
else
{}
end
end
private
def get_resource
ApiEntreprise::API.entreprise(@siret_or_siren, @procedure_id)
end
def process_params
params = data_source[:entreprise].slice(*attr_to_fetch)
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
params
end
def attr_to_fetch
[
:siren,

View file

@ -1,22 +1,18 @@
class ApiEntreprise::EtablissementAdapter < ApiEntreprise::Adapter
def to_params
if data_source.present?
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
else
{}
end
end
private
def get_resource
ApiEntreprise::API.etablissement(@siret_or_siren, @procedure_id)
end
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
end
def attr_to_fetch
[
:adresse,

View file

@ -1,22 +1,18 @@
class ApiEntreprise::ExercicesAdapter < ApiEntreprise::Adapter
def to_params
if data_source.present?
exercices_array = data_source[:exercices].map do |exercice|
exercice.slice(*attr_to_fetch)
end
{ exercices_attributes: exercices_array }
else
{}
end
end
private
def get_resource
ApiEntreprise::API.exercices(@siret_or_siren, @procedure_id)
end
def process_params
exercices_array = data_source[:exercices].map do |exercice|
exercice.slice(*attr_to_fetch)
end
{ exercices_attributes: exercices_array }
end
def attr_to_fetch
[
:ca,

View file

@ -1,6 +1,12 @@
class ApiEntreprise::RNAAdapter < ApiEntreprise::Adapter
def to_params
if data_source.present? && data_source[:association][:id].present?
private
def get_resource
ApiEntreprise::API.rna(@siret_or_siren, @procedure_id)
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
@ -9,12 +15,6 @@ class ApiEntreprise::RNAAdapter < ApiEntreprise::Adapter
end
end
private
def get_resource
ApiEntreprise::API.rna(@siret_or_siren, @procedure_id)
end
def attr_to_fetch
[
:titre,