demarches-normaliennes/app/lib/api_entreprise/etablissement_adapter.rb

58 lines
1.6 KiB
Ruby
Raw Normal View History

2020-08-05 18:40:47 +02:00
class APIEntreprise::EtablissementAdapter < APIEntreprise::Adapter
# Doc Métier : https://entreprise.api.gouv.fr/catalogue/insee/etablissements
# Swagger : https://entreprise.api.gouv.fr/developpeurs/openapi#tag/Informations-generales/paths/~1v3~1insee~1sirene~1etablissements~1%7Bsiret%7D/get
2018-02-21 16:29:22 +01:00
private
def get_resource
api(@procedure_id).etablissement(@siret)
2018-03-15 14:30:10 +01:00
end
def process_params
raw_data = data_source[:data]
Sentry.with_scope do |scope|
scope.set_tags(siret: @siret)
scope.set_extras(source: raw_data)
params = raw_data.slice(*attr_to_fetch)
params[:naf] = raw_data.dig(:activite_principale, :code)
params[:libelle_naf] = raw_data.dig(:activite_principale, :libelle)
if valid_params?(params)
adresse_line = raw_data[:adresse][:acheminement_postal].slice(*address_lines_to_fetch).values.compact.join("\r\n")
params.merge!(params[:adresse].slice(*address_attr_to_fetch))
params[:nom_voie] = raw_data[:adresse][:libelle_voie]
params[:code_insee_localite] = raw_data[:adresse][:code_commune]
params[:localite] = raw_data[:adresse][:libelle_commune]
params[:adresse] = adresse_line
params
else
{}
end
end
end
2015-08-10 11:05:06 +02:00
def attr_to_fetch
[
2018-02-21 16:29:22 +01:00
:adresse,
:siret,
:siege_social,
:enseigne,
:diffusable_commercialement
2015-08-20 16:46:46 +02:00
]
2015-08-10 11:05:06 +02:00
end
2018-02-21 16:29:22 +01:00
def address_attr_to_fetch
[
:numero_voie,
:type_voie,
:complement_adresse,
:code_postal
]
2015-08-10 11:05:06 +02:00
end
2018-02-21 16:29:22 +01:00
def address_lines_to_fetch
[:l1, :l2, :l3, :l4, :l5, :l6, :l7]
end
2015-08-10 11:05:06 +02:00
end