Refactor siade adapters

This commit is contained in:
Paul Chavard 2018-02-21 16:29:22 +01:00
parent 0b2ad653fa
commit da14cceadc
9 changed files with 66 additions and 53 deletions

View file

@ -9,19 +9,29 @@ class SIADE::EntrepriseAdapter
@data_source = nil @data_source = nil
end end
def success?
data_source
rescue
false
end
def to_params def to_params
params = {} params = data_source[:entreprise].slice(*attr_to_fetch)
data_source[:entreprise].each do |k, v|
params[k] = v if attr_to_fetch.include?(k)
end
params[:date_creation] = Time.at(params[:date_creation]).to_datetime params[:date_creation] = Time.at(params[:date_creation]).to_datetime
params params
rescue rescue
nil nil
end end
def mandataires_sociaux
data_source[:entreprise].fetch(:mandataires_sociaux)
rescue
nil
end
private
def attr_to_fetch def attr_to_fetch
[ [
:siren, :siren,
@ -38,10 +48,4 @@ class SIADE::EntrepriseAdapter
:prenom :prenom
] ]
end end
def mandataires_sociaux
data_source[:entreprise][:mandataires_sociaux]
rescue
nil
end
end end

View file

@ -7,23 +7,27 @@ class SIADE::EtablissementAdapter
@data_source ||= JSON.parse(SIADE::API.etablissement(@siret), symbolize_names: true) @data_source ||= JSON.parse(SIADE::API.etablissement(@siret), symbolize_names: true)
end end
def to_params def success?
params = {} data_source
rescue => e
false
end
data_source[:etablissement].each do |k, v| def to_params
params[k] = v if attr_to_fetch.include?(k) params = data_source[:etablissement].slice(*attr_to_fetch)
end adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n")
params[:adresse] = adresse params.merge!(params[:adresse].slice(*address_attr_to_fetch))
data_source[:etablissement][:adresse].each do |k, v| params[:adresse] = adresse_line
params[k] = v if address_attribut_to_fetch.include?(k)
end
params params
rescue rescue
nil nil
end end
private
def attr_to_fetch def attr_to_fetch
[ [
:adresse,
:siret, :siret,
:siege_social, :siege_social,
:naf, :naf,
@ -31,13 +35,7 @@ class SIADE::EtablissementAdapter
] ]
end end
def adresse def address_attr_to_fetch
[:l1, :l2, :l3, :l4, :l5, :l6, :l7].map do |line|
data_source[:etablissement][:adresse][line]
end.compact.join("\r\n")
end
def address_attribut_to_fetch
[ [
:numero_voie, :numero_voie,
:type_voie, :type_voie,
@ -48,4 +46,8 @@ class SIADE::EtablissementAdapter
:code_insee_localite :code_insee_localite
] ]
end end
def address_lines_to_fetch
[:l1, :l2, :l3, :l4, :l5, :l6, :l7]
end
end end

View file

@ -11,13 +11,15 @@ class SIADE::ExercicesAdapter
def to_params def to_params
data_source[:exercices].map do |exercice| data_source[:exercices].map do |exercice|
{ exercice.slice(*attr_to_fetch)
ca: exercice[:ca],
dateFinExercice: exercice[:date_fin_exercice],
date_fin_exercice_timestamp: exercice[:date_fin_exercice_timestamp]
}
end end
rescue rescue
nil []
end
private
def attr_to_fetch
[:ca, :date_fin_exercice, :date_fin_exercice_timestamp]
end end
end end

View file

@ -8,23 +8,17 @@ class SIADE::RNAAdapter
end end
def to_params def to_params
params = {} params = data_source[:association].slice(*attr_to_fetch)
params[:rna] = data_source[:association][:id]
data_source[:association].each do |k, v|
params[k] = v if attr_to_fetch.include?(k)
end
params[:association_id] = params[:id]
params.delete(:id)
params params
rescue rescue
nil nil
end end
private
def attr_to_fetch def attr_to_fetch
[ [
:id,
:titre, :titre,
:objet, :objet,
:date_creation, :date_creation,

View file

@ -2,4 +2,8 @@ class RNAInformation < ActiveRecord::Base
belongs_to :entreprise belongs_to :entreprise
validates :association_id, presence: true, allow_blank: false, allow_nil: false validates :association_id, presence: true, allow_blank: false, allow_nil: false
def rna=(id)
write_attribute(:association_id, id)
end
end end

View file

@ -1,10 +1,12 @@
require 'spec_helper' require 'spec_helper'
describe SIADE::EntrepriseAdapter do describe SIADE::EntrepriseAdapter do
subject { described_class.new('418166096').to_params } let(:siren) { '418166096' }
let(:adapter) { described_class.new(siren) }
subject { adapter.to_params }
before do before do
stub_request(:get, "https://staging.entreprise.api.gouv.fr/v2/entreprises/418166096?token=#{SIADETOKEN}") stub_request(:get, "https://staging.entreprise.api.gouv.fr/v2/entreprises/#{siren}?token=#{SIADETOKEN}")
.to_return(body: File.read('spec/support/files/entreprise.json', status: 200)) .to_return(body: File.read('spec/support/files/entreprise.json', status: 200))
end end
@ -14,7 +16,7 @@ describe SIADE::EntrepriseAdapter do
context 'Attributs Entreprises' do context 'Attributs Entreprises' do
it 'L\'entreprise contient bien un siren' do it 'L\'entreprise contient bien un siren' do
expect(subject[:siren]).to eq('418166096') expect(subject[:siren]).to eq(siren)
end end
it 'L\'entreprise contient bien un capital_social' do it 'L\'entreprise contient bien un capital_social' do
@ -60,10 +62,14 @@ describe SIADE::EntrepriseAdapter do
it 'L\'entreprise contient bien un prenom' do it 'L\'entreprise contient bien un prenom' do
expect(subject[:prenom]).to eq('test_prenom') expect(subject[:prenom]).to eq('test_prenom')
end end
it 'L\'entreprise contient bien les mandataires_sociaux' do
expect(subject[:mandataires_sociaux]).to be_an_instance_of(Array)
end
end end
context 'Mandataire sociaux' do context 'Mandataire sociaux' do
subject { described_class.new('418166096').mandataires_sociaux } subject { described_class.new(siren).to_params[:mandataires_sociaux] }
it '#to_params class est une Hash ?' do it '#to_params class est une Hash ?' do
expect(subject).to be_an_instance_of(Array) expect(subject).to be_an_instance_of(Array)

View file

@ -2,7 +2,7 @@ require 'spec_helper'
describe SIADE::EtablissementAdapter do describe SIADE::EtablissementAdapter do
context 'SIRET valide' do context 'SIRET valide' do
let(:siret) { 41_816_609_600_051 } let(:siret) { '41816609600051' }
subject { described_class.new(siret).to_params } subject { described_class.new(siret).to_params }
before do before do
@ -16,7 +16,7 @@ describe SIADE::EtablissementAdapter do
context 'Attributs Etablissements' do context 'Attributs Etablissements' do
it 'L\'entreprise contient bien un siret' do it 'L\'entreprise contient bien un siret' do
expect(subject[:siret]).to eq('41816609600051') expect(subject[:siret]).to eq(siret)
end end
it 'L\'entreprise contient bien un siege_social' do it 'L\'entreprise contient bien un siege_social' do

View file

@ -23,7 +23,7 @@ describe SIADE::ExercicesAdapter do
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][:dateFinExercice]).to eq("2013-12-31T00:00:00+01:00") expect(subject[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

View file

@ -4,8 +4,9 @@ describe SIADE::RNAAdapter do
let(:siret) { '50480511000013' } let(:siret) { '50480511000013' }
let(:body) { File.read('spec/support/files/rna.json') } let(:body) { File.read('spec/support/files/rna.json') }
let(:status) { 200 } let(:status) { 200 }
let(:adapter) { described_class.new(siret) }
subject { described_class.new(siret).to_params } subject { adapter.to_params }
before do before do
stub_request(:get, /https:\/\/staging.entreprise.api.gouv.fr\/v2\/associations\/.*token=/) stub_request(:get, /https:\/\/staging.entreprise.api.gouv.fr\/v2\/associations\/.*token=/)
@ -23,7 +24,7 @@ describe SIADE::RNAAdapter do
it { expect(subject).to be_an_instance_of(Hash) } it { expect(subject).to be_an_instance_of(Hash) }
describe 'Attributs Associations' do describe 'Attributs Associations' do
it { expect(subject[:association_id]).to eq('W595001988') } it { expect(subject[:rna]).to eq('W595001988') }
it { expect(subject[:titre]).to eq('UN SUR QUATRE') } it { expect(subject[:titre]).to eq('UN SUR QUATRE') }