Merge branch 'dev'
This commit is contained in:
commit
4edad8d0a0
8 changed files with 80 additions and 91 deletions
20
app/lib/api_entreprise/adapter.rb
Normal file
20
app/lib/api_entreprise/adapter.rb
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
class ApiEntreprise::Adapter
|
||||||
|
def initialize(siret_or_siren, procedure_id)
|
||||||
|
@siret_or_siren = siret_or_siren
|
||||||
|
@procedure_id = procedure_id
|
||||||
|
end
|
||||||
|
|
||||||
|
def data_source
|
||||||
|
@data_source ||= get_resource
|
||||||
|
rescue
|
||||||
|
@data_source = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_params
|
||||||
|
if data_source.present?
|
||||||
|
process_params
|
||||||
|
else
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -38,6 +38,9 @@ class ApiEntreprise::API
|
||||||
params: params,
|
params: params,
|
||||||
timeout: TIMEOUT)
|
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? && response.code != 206
|
||||||
JSON.parse(response.body, symbolize_names: true)
|
JSON.parse(response.body, symbolize_names: true)
|
||||||
else
|
else
|
||||||
|
|
|
@ -1,25 +1,14 @@
|
||||||
class ApiEntreprise::EntrepriseAdapter
|
class ApiEntreprise::EntrepriseAdapter < ApiEntreprise::Adapter
|
||||||
def initialize(siren, procedure_id)
|
private
|
||||||
@siren = siren
|
|
||||||
@procedure_id = procedure_id
|
def get_resource
|
||||||
|
ApiEntreprise::API.entreprise(@siret_or_siren, @procedure_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_params
|
def process_params
|
||||||
if data_source.present?
|
|
||||||
params = data_source[:entreprise].slice(*attr_to_fetch)
|
params = data_source[:entreprise].slice(*attr_to_fetch)
|
||||||
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
|
params[:date_creation] = Time.at(params[:date_creation]).to_datetime
|
||||||
params
|
params
|
||||||
else
|
|
||||||
{}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def data_source
|
|
||||||
@data_source ||= ApiEntreprise::API.entreprise(@siren, @procedure_id)
|
|
||||||
rescue
|
|
||||||
@data_source = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def attr_to_fetch
|
def attr_to_fetch
|
||||||
|
|
|
@ -1,27 +1,16 @@
|
||||||
class ApiEntreprise::EtablissementAdapter
|
class ApiEntreprise::EtablissementAdapter < ApiEntreprise::Adapter
|
||||||
def initialize(siret, procedure_id)
|
private
|
||||||
@siret = siret
|
|
||||||
@procedure_id = procedure_id
|
def get_resource
|
||||||
|
ApiEntreprise::API.etablissement(@siret_or_siren, @procedure_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_params
|
def process_params
|
||||||
if data_source.present?
|
|
||||||
params = data_source[:etablissement].slice(*attr_to_fetch)
|
params = data_source[:etablissement].slice(*attr_to_fetch)
|
||||||
adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n")
|
adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n")
|
||||||
params.merge!(params[:adresse].slice(*address_attr_to_fetch))
|
params.merge!(params[:adresse].slice(*address_attr_to_fetch))
|
||||||
params[:adresse] = adresse_line
|
params[:adresse] = adresse_line
|
||||||
params
|
params
|
||||||
else
|
|
||||||
{}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def data_source
|
|
||||||
@data_source ||= ApiEntreprise::API.etablissement(@siret, @procedure_id)
|
|
||||||
rescue
|
|
||||||
@data_source = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def attr_to_fetch
|
def attr_to_fetch
|
||||||
|
|
|
@ -1,25 +1,16 @@
|
||||||
class ApiEntreprise::ExercicesAdapter
|
class ApiEntreprise::ExercicesAdapter < ApiEntreprise::Adapter
|
||||||
def initialize(siret, procedure_id)
|
|
||||||
@siret = siret
|
|
||||||
@procedure_id = procedure_id
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_array
|
|
||||||
if data_source.present?
|
|
||||||
data_source[:exercices].map do |exercice|
|
|
||||||
exercice.slice(*attr_to_fetch)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def data_source
|
def get_resource
|
||||||
@data_source ||= ApiEntreprise::API.exercices(@siret, @procedure_id)
|
ApiEntreprise::API.exercices(@siret_or_siren, @procedure_id)
|
||||||
rescue
|
end
|
||||||
@data_source = nil
|
|
||||||
|
def process_params
|
||||||
|
exercices_array = data_source[:exercices].map do |exercice|
|
||||||
|
exercice.slice(*attr_to_fetch)
|
||||||
|
end
|
||||||
|
|
||||||
|
{ exercices_attributes: exercices_array }
|
||||||
end
|
end
|
||||||
|
|
||||||
def attr_to_fetch
|
def attr_to_fetch
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
class ApiEntreprise::RNAAdapter
|
class ApiEntreprise::RNAAdapter < ApiEntreprise::Adapter
|
||||||
def initialize(siret, procedure_id)
|
private
|
||||||
@siret = siret
|
|
||||||
@procedure_id = procedure_id
|
def get_resource
|
||||||
|
ApiEntreprise::API.rna(@siret_or_siren, @procedure_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_params
|
def process_params
|
||||||
if data_source.present? && data_source[:association][:id].present?
|
if data_source[:association][:id].present?
|
||||||
params = data_source[:association].slice(*attr_to_fetch)
|
params = data_source[:association].slice(*attr_to_fetch)
|
||||||
params[:rna] = data_source[:association][:id]
|
params[:rna] = data_source[:association][:id]
|
||||||
params
|
params
|
||||||
|
@ -14,14 +15,6 @@ class ApiEntreprise::RNAAdapter
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def data_source
|
|
||||||
@data_source ||= ApiEntreprise::API.rna(@siret, @procedure_id)
|
|
||||||
rescue
|
|
||||||
@data_source = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def attr_to_fetch
|
def attr_to_fetch
|
||||||
[
|
[
|
||||||
:titre,
|
:titre,
|
||||||
|
|
|
@ -7,21 +7,16 @@ class SIRETService
|
||||||
|
|
||||||
if etablissement_params.present? && entreprise_params.present?
|
if etablissement_params.present? && entreprise_params.present?
|
||||||
association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params
|
association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params
|
||||||
exercices_array = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_array
|
exercices_params = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_params
|
||||||
|
|
||||||
params = etablissement_params
|
params = etablissement_params
|
||||||
.merge(entreprise_params.transform_keys { |k| "entreprise_#{k}" })
|
.merge(entreprise_params.transform_keys { |k| "entreprise_#{k}" })
|
||||||
.merge(association_params.transform_keys { |k| "association_#{k}" })
|
.merge(association_params.transform_keys { |k| "association_#{k}" })
|
||||||
.merge(exercices_attributes: exercices_array)
|
.merge(exercices_params)
|
||||||
|
|
||||||
# This is to fill legacy models and relationships
|
# This is to fill legacy models and relationships
|
||||||
if dossier.present?
|
if dossier.present?
|
||||||
params[:entreprise_attributes] = entreprise_params.merge(
|
handle_legacy_models!(params, entreprise_params, dossier, association_params)
|
||||||
{
|
|
||||||
dossier: dossier,
|
|
||||||
rna_information_attributes: association_params.presence
|
|
||||||
}.compact
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
params
|
params
|
||||||
|
@ -31,4 +26,13 @@ class SIRETService
|
||||||
def self.siren(siret)
|
def self.siren(siret)
|
||||||
siret[0..8]
|
siret[0..8]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.handle_legacy_models!(params, entreprise_params, dossier, association_params)
|
||||||
|
params[:entreprise_attributes] = entreprise_params.merge(
|
||||||
|
{
|
||||||
|
dossier: dossier,
|
||||||
|
rna_information_attributes: association_params.presence
|
||||||
|
}.compact
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,32 +3,32 @@ require 'spec_helper'
|
||||||
describe ApiEntreprise::ExercicesAdapter do
|
describe ApiEntreprise::ExercicesAdapter do
|
||||||
let(:siret) { '41816609600051' }
|
let(:siret) { '41816609600051' }
|
||||||
let(:procedure_id) { 11 }
|
let(:procedure_id) { 11 }
|
||||||
subject { described_class.new(siret, procedure_id).to_array }
|
subject { described_class.new(siret, procedure_id).to_params }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
stub_request(:get, /https:\/\/staging.entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
|
stub_request(:get, /https:\/\/staging.entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)
|
||||||
.to_return(body: File.read('spec/support/files/exercices.json', status: 200))
|
.to_return(body: File.read('spec/support/files/exercices.json', status: 200))
|
||||||
end
|
end
|
||||||
|
|
||||||
it '#to_array class est un Array ?' do
|
it '#to_params class est un Hash ?' do
|
||||||
expect(subject).to be_an_instance_of(Array)
|
expect(subject).to be_an_instance_of(Hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'have 3 exercices' do
|
it 'have 3 exercices' do
|
||||||
expect(subject.size).to eq(3)
|
expect(subject[:exercices_attributes].size).to eq(3)
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'Attributs Exercices' do
|
context 'Attributs Exercices' do
|
||||||
it 'L\'exercice contient bien un ca' do
|
it 'L\'exercice contient bien un ca' do
|
||||||
expect(subject[0][:ca]).to eq('21009417')
|
expect(subject[:exercices_attributes][0][:ca]).to eq('21009417')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'L\'exercice contient bien une date de fin d\'exercice' do
|
it 'L\'exercice contient bien une date de fin d\'exercice' do
|
||||||
expect(subject[0][:date_fin_exercice]).to eq("2013-12-31T00:00:00+01:00")
|
expect(subject[:exercices_attributes][0][:date_fin_exercice]).to eq("2013-12-31T00:00:00+01:00")
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'L\'exercice contient bien une date_fin_exercice_timestamp' do
|
it 'L\'exercice contient bien une date_fin_exercice_timestamp' do
|
||||||
expect(subject[0][:date_fin_exercice_timestamp]).to eq(1388444400)
|
expect(subject[:exercices_attributes][0][:date_fin_exercice_timestamp]).to eq(1388444400)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue