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

View file

@ -7,23 +7,27 @@ class SIADE::EtablissementAdapter
@data_source ||= JSON.parse(SIADE::API.etablissement(@siret), symbolize_names: true)
end
def to_params
params = {}
def success?
data_source
rescue => e
false
end
data_source[:etablissement].each do |k, v|
params[k] = v if attr_to_fetch.include?(k)
end
params[:adresse] = adresse
data_source[:etablissement][:adresse].each do |k, v|
params[k] = v if address_attribut_to_fetch.include?(k)
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
end
private
def attr_to_fetch
[
:adresse,
:siret,
:siege_social,
:naf,
@ -31,13 +35,7 @@ class SIADE::EtablissementAdapter
]
end
def adresse
[: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
def address_attr_to_fetch
[
:numero_voie,
:type_voie,
@ -48,4 +46,8 @@ class SIADE::EtablissementAdapter
:code_insee_localite
]
end
def address_lines_to_fetch
[:l1, :l2, :l3, :l4, :l5, :l6, :l7]
end
end

View file

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

View file

@ -8,23 +8,17 @@ class SIADE::RNAAdapter
end
def to_params
params = {}
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 = data_source[:association].slice(*attr_to_fetch)
params[:rna] = data_source[:association][:id]
params
rescue
nil
end
private
def attr_to_fetch
[
:id,
:titre,
:objet,
:date_creation,

View file

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

View file

@ -1,10 +1,12 @@
require 'spec_helper'
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
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))
end
@ -14,7 +16,7 @@ describe SIADE::EntrepriseAdapter do
context 'Attributs Entreprises' do
it 'L\'entreprise contient bien un siren' do
expect(subject[:siren]).to eq('418166096')
expect(subject[:siren]).to eq(siren)
end
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
expect(subject[:prenom]).to eq('test_prenom')
end
it 'L\'entreprise contient bien les mandataires_sociaux' do
expect(subject[:mandataires_sociaux]).to be_an_instance_of(Array)
end
end
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
expect(subject).to be_an_instance_of(Array)

View file

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

View file

@ -23,7 +23,7 @@ describe SIADE::ExercicesAdapter do
end
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
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(:body) { File.read('spec/support/files/rna.json') }
let(:status) { 200 }
let(:adapter) { described_class.new(siret) }
subject { described_class.new(siret).to_params }
subject { adapter.to_params }
before do
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) }
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') }