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

53 lines
1 KiB
Ruby
Raw Normal View History

2015-08-10 11:05:06 +02:00
class SIADE::EtablissementAdapter
def initialize(siret)
@siret = siret
end
def data_source
@data_source ||= JSON.parse(SIADE::API.etablissement(@siret), symbolize_names: true)
2015-08-10 11:05:06 +02:00
end
def to_params
params = {}
2015-08-20 16:46:46 +02:00
data_source[:etablissement].each do |k, v|
params[k] = v if attr_to_fetch.include?(k)
2015-08-10 11:05:06 +02:00
end
params[:adresse] = adresse
2015-08-20 16:46:46 +02:00
data_source[:etablissement][:adresse].each do |k, v|
params[k] = v if address_attribut_to_fetch.include?(k)
2015-08-10 11:05:06 +02:00
end
params
rescue
nil
2015-08-10 11:05:06 +02:00
end
def attr_to_fetch
[:siret,
:siege_social,
:naf,
2015-08-20 16:46:46 +02:00
:libelle_naf
]
2015-08-10 11:05:06 +02:00
end
def adresse
adresse = ''
2016-06-13 10:13:37 +02:00
[:l1, :l2, :l3, :l4, :l5, :l6, :l7].each do |line|
2018-01-11 19:04:39 +01:00
if data_source[:etablissement][:adresse][line].present?
adresse = adresse + data_source[:etablissement][:adresse][line] + "\r\n"
end
2015-08-10 11:05:06 +02:00
end
adresse
end
def address_attribut_to_fetch
[:numero_voie,
2015-08-20 16:46:46 +02:00
:type_voie,
:nom_voie,
:complement_adresse,
:code_postal,
:localite,
:code_insee_localite]
2015-08-10 11:05:06 +02:00
end
end