Merge pull request #1669 from betagouv/api-entreprise-refactor

Api entreprise refactor
This commit is contained in:
Paul Chavard 2018-03-28 14:57:16 +02:00 committed by GitHub
commit 9749c05686
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 60 additions and 58 deletions

View file

@ -4,18 +4,14 @@ class ApiEntreprise::EntrepriseAdapter
@procedure_id = procedure_id
end
def success?
data_source
rescue
false
end
def to_params
params = data_source[:entreprise].slice(*attr_to_fetch)
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
params
rescue
nil
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

View file

@ -4,26 +4,24 @@ class ApiEntreprise::EtablissementAdapter
@procedure_id = procedure_id
end
def success?
data_source
rescue
false
end
def to_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
rescue
nil
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 data_source
@data_source ||= ApiEntreprise::API.etablissement(@siret, @procedure_id)
rescue
@data_source = nil
end
def attr_to_fetch

View file

@ -4,12 +4,14 @@ class ApiEntreprise::ExercicesAdapter
@procedure_id = procedure_id
end
def to_params
data_source[:exercices].map do |exercice|
exercice.slice(*attr_to_fetch)
def to_array
if data_source.present?
data_source[:exercices].map do |exercice|
exercice.slice(*attr_to_fetch)
end
else
[]
end
rescue
[]
end
private
@ -21,6 +23,10 @@ class ApiEntreprise::ExercicesAdapter
end
def attr_to_fetch
[:ca, :date_fin_exercice, :date_fin_exercice_timestamp]
[
:ca,
:date_fin_exercice,
:date_fin_exercice_timestamp
]
end
end

View file

@ -5,20 +5,21 @@ class ApiEntreprise::RNAAdapter
end
def to_params
if data_source[:association][:id].nil?
return nil
if data_source.present? && data_source[:association][:id].present?
params = data_source[:association].slice(*attr_to_fetch)
params[:rna] = data_source[:association][:id]
params
else
{}
end
params = data_source[:association].slice(*attr_to_fetch)
params[:rna] = data_source[:association][:id]
params
rescue
nil
end
private
def data_source
@data_source ||= ApiEntreprise::API.rna(@siret, @procedure_id)
rescue
@data_source = nil
end
def attr_to_fetch

View file

@ -1,29 +1,30 @@
class SIRETService
def self.fetch(siret, dossier = nil)
etablissement = ApiEntreprise::EtablissementAdapter.new(siret, dossier&.procedure_id)
entreprise = ApiEntreprise::EntrepriseAdapter.new(siren(siret), dossier&.procedure_id)
procedure_id = dossier&.procedure_id
if etablissement.success? && entreprise.success?
association = ApiEntreprise::RNAAdapter.new(siret, dossier&.procedure_id)
exercices = ApiEntreprise::ExercicesAdapter.new(siret, dossier&.procedure_id)
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
entreprise_params = ApiEntreprise::EntrepriseAdapter.new(siren(siret), procedure_id).to_params
params = etablissement.to_params
.merge(entreprise.to_params.map { |k,v| ["entreprise_#{k}", v] }.to_h)
.merge(association.to_params&.map { |k,v| ["association_#{k}", v] }.to_h)
.merge(exercices_attributes: exercices.to_params)
if etablissement_params.present? && entreprise_params.present?
association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params
exercices_array = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_array
params = etablissement_params
.merge(entreprise_params.transform_keys { |k| "entreprise_#{k}" })
.merge(association_params.transform_keys { |k| "association_#{k}" })
.merge(exercices_attributes: exercices_array)
# This is to fill legacy models and relationships
if dossier.present?
return params.merge(
entreprise_attributes: entreprise.to_params
.merge({
dossier: dossier,
rna_information_attributes: association.to_params
}.compact)
params[:entreprise_attributes] = entreprise_params.merge(
{
dossier: dossier,
rna_information_attributes: association_params.presence
}.compact
)
else
return params
end
params
end
end

View file

@ -79,6 +79,6 @@ describe ApiEntreprise::EtablissementAdapter do
.to_return(body: 'Fake body', status: 404)
end
it { expect(subject).to be_nil }
it { expect(subject).to eq({}) }
end
end

View file

@ -3,14 +3,14 @@ require 'spec_helper'
describe ApiEntreprise::ExercicesAdapter do
let(:siret) { '41816609600051' }
let(:procedure_id) { 11 }
subject { described_class.new(siret, procedure_id).to_params }
subject { described_class.new(siret, procedure_id).to_array }
before do
stub_request(:get, /https:\/\/staging.entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
.to_return(body: File.read('spec/support/files/exercices.json', status: 200))
end
it '#to_params class est une Hash ?' do
it '#to_array class est un Array ?' do
expect(subject).to be_an_instance_of(Array)
end

View file

@ -19,7 +19,7 @@ describe ApiEntreprise::RNAAdapter do
let(:body) { '' }
let(:status) { '404' }
it { is_expected.to eq(nil) }
it { is_expected.to eq({}) }
end
it { expect(subject).to be_an_instance_of(Hash) }