Improve and connect RNA Adapter
This commit is contained in:
parent
2791871c9a
commit
542c5b518f
6 changed files with 147 additions and 29 deletions
|
@ -1,4 +1,12 @@
|
|||
require 'json_schemer'
|
||||
|
||||
class APIEntreprise::RNAAdapter < APIEntreprise::Adapter
|
||||
class InvalidSchemaError < ::StandardError
|
||||
def initialize(errors)
|
||||
super(errors.map(&:to_json).join("\n"))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_resource
|
||||
|
@ -6,26 +14,22 @@ class APIEntreprise::RNAAdapter < APIEntreprise::Adapter
|
|||
end
|
||||
|
||||
def process_params
|
||||
# Sometimes the associations endpoints responses with a 206,
|
||||
# and these response are often useable as the they only
|
||||
# contain an error message.
|
||||
# Therefore here we make sure that our response seems valid
|
||||
# by checking that there is an association attribute.
|
||||
if !data_source.key?(:association)
|
||||
{}
|
||||
else
|
||||
association_id = data_source[:association][:id]
|
||||
params = data_source[:association].slice(*attr_to_fetch)
|
||||
params = data_source[:association]&.slice(*attr_to_fetch)
|
||||
params[:rna] = data_source.dig(:association, :id)
|
||||
if params[:rna].present? && valid_params?(params)
|
||||
params = params.transform_keys { |k| :"association_#{k}" }.deep_stringify_keys
|
||||
raise InvalidSchemaError.new(schemer.validate(params).to_a) unless schemer.valid?(params)
|
||||
|
||||
if association_id.present? && valid_params?(params)
|
||||
params[:rna] = association_id
|
||||
params.transform_keys { |k| :"association_#{k}" }
|
||||
else
|
||||
{}
|
||||
end
|
||||
params
|
||||
else
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
def schemer
|
||||
@schemer ||= JSONSchemer.schema(Rails.root.join('app/schemas/association.json'))
|
||||
end
|
||||
|
||||
def attr_to_fetch
|
||||
[
|
||||
:titre,
|
||||
|
|
|
@ -23,4 +23,27 @@ class Champs::RNAChamp < Champ
|
|||
validates :value, allow_blank: true, format: {
|
||||
with: /\AW[0-9]{9}\z/, message: I18n.t(:not_a_rna, scope: 'activerecord.errors.messages')
|
||||
}
|
||||
after_validation :update_external_id, if: -> { value.present? && external_id.blank? }
|
||||
|
||||
delegate :id, to: :procedure, prefix: true
|
||||
|
||||
def for_export
|
||||
data&.dig("association_titre")&.present? ? "#{value} (#{data.dig("association_titre")})" : value
|
||||
end
|
||||
|
||||
def search_terms
|
||||
etablissement.present? ? etablissement.search_terms : [value]
|
||||
end
|
||||
|
||||
def fetch_external_data?
|
||||
true
|
||||
end
|
||||
|
||||
def fetch_external_data
|
||||
APIEntreprise::RNAAdapter.new(external_id, procedure_id).to_params
|
||||
end
|
||||
|
||||
def update_external_id
|
||||
update(external_id: value)
|
||||
end
|
||||
end
|
||||
|
|
33
app/schemas/association.json
Normal file
33
app/schemas/association.json
Normal file
|
@ -0,0 +1,33 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"$id": "http://demarches-simplifiees.fr/association.schema.json",
|
||||
"title": "Association",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"association_titre": {
|
||||
"type": "string"
|
||||
},
|
||||
"association_objet": {
|
||||
"type": "string"
|
||||
},
|
||||
"association_rna": {
|
||||
"type": "string"
|
||||
},
|
||||
"association_date_creation": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"association_date_declaration": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"association_date_publication": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"association_titre",
|
||||
"association_rna"
|
||||
]
|
||||
}
|
30
spec/fixtures/files/api_entreprise/associations_invalid.json
vendored
Normal file
30
spec/fixtures/files/api_entreprise/associations_invalid.json
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"association": {
|
||||
"id": "W595001988",
|
||||
"titre": "UN SUR QUATRE",
|
||||
"objet": "valoriser, transmettre et partager auprès des publics les plus larges possibles, les bienfaits de l'immigration, la richesse de la diversité et la curiosité de l'autre autrement",
|
||||
"siret": null,
|
||||
"date_creation": "test",
|
||||
"date_declaration": "2014-01-24",
|
||||
"date_publication": "2014-02-08",
|
||||
"date_dissolution": "0001-01-01",
|
||||
"adresse_siege": {
|
||||
"complement": "",
|
||||
"numero_voie": "61",
|
||||
"type_voie": "RUE",
|
||||
"libelle_voie": "des Noyers",
|
||||
"distribution": "_",
|
||||
"code_insee": "93063",
|
||||
"code_postal": "93230",
|
||||
"commune": "Romainville"
|
||||
},
|
||||
"code_civilite_dirigeant": "PM",
|
||||
"civilite_dirigeant": "Monsieur le Président",
|
||||
"code_etat": "A",
|
||||
"etat": "Active",
|
||||
"code_groupement": "S",
|
||||
"groupement": "simple",
|
||||
"mise_a_jour": 1392295833
|
||||
},
|
||||
"date_extraction_donnees": 1427210585
|
||||
}
|
|
@ -2,8 +2,6 @@ describe APIEntreprise::RNAAdapter do
|
|||
let(:siret) { '50480511000013' }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let(:procedure_id) { procedure.id }
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||
let(:status) { 200 }
|
||||
let(:adapter) { described_class.new(siret, procedure_id) }
|
||||
|
||||
subject { adapter.to_params }
|
||||
|
@ -22,19 +20,27 @@ describe APIEntreprise::RNAAdapter do
|
|||
it { is_expected.to eq({}) }
|
||||
end
|
||||
|
||||
it { expect(subject).to be_an_instance_of(Hash) }
|
||||
context "when responds with valid schema" do
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
|
||||
let(:status) { 200 }
|
||||
|
||||
describe 'Attributs Associations' do
|
||||
it { expect(subject[:association_rna]).to eq('W595001988') }
|
||||
it '#to_params return vaid hash' do
|
||||
expect(subject).to be_an_instance_of(Hash)
|
||||
expect(subject["association_rna"]).to eq('W595001988')
|
||||
expect(subject["association_titre"]).to eq('UN SUR QUATRE')
|
||||
expect(subject["association_objet"]).to eq("valoriser, transmettre et partager auprès des publics les plus larges possibles, les bienfaits de l'immigration, la richesse de la diversité et la curiosité de l'autre autrement")
|
||||
expect(subject["association_date_creation"]).to eq('2014-01-23')
|
||||
expect(subject["association_date_declaration"]).to eq('2014-01-24')
|
||||
expect(subject["association_date_publication"]).to eq('2014-02-08')
|
||||
end
|
||||
end
|
||||
|
||||
it { expect(subject[:association_titre]).to eq('UN SUR QUATRE') }
|
||||
context "when responds with invalid schema" do
|
||||
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations_invalid.json') }
|
||||
let(:status) { 200 }
|
||||
|
||||
it { expect(subject[:association_objet]).to eq("valoriser, transmettre et partager auprès des publics les plus larges possibles, les bienfaits de l'immigration, la richesse de la diversité et la curiosité de l'autre autrement") }
|
||||
|
||||
it { expect(subject[:association_date_creation]).to eq('2014-01-23') }
|
||||
|
||||
it { expect(subject[:association_date_declaration]).to eq('2014-01-24') }
|
||||
|
||||
it { expect(subject[:association_date_publication]).to eq('2014-02-08') }
|
||||
it '#to_params raise exception' do
|
||||
expect { subject }.to raise_exception(APIEntreprise::RNAAdapter::InvalidSchemaError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
describe Champs::RNAChamp do
|
||||
let(:champ) { create(:champ_rna, value: "W182736273") }
|
||||
|
||||
describe '#valid?' do
|
||||
it do
|
||||
expect(build(:champ_rna, value: nil)).to be_valid
|
||||
|
@ -8,4 +10,24 @@ describe Champs::RNAChamp do
|
|||
expect(build(:champ_rna, value: "W182736273")).to be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "#export" do
|
||||
context "with association title" do
|
||||
before do
|
||||
champ.update(data: { association_titre: "Super asso" })
|
||||
end
|
||||
|
||||
it { expect(champ.for_export).to eq("W182736273 (Super asso)") }
|
||||
end
|
||||
|
||||
context "no association title" do
|
||||
it { expect(champ.for_export).to eq("W182736273") }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'external_id' do
|
||||
context 'when all data required for an external fetch are given' do
|
||||
it { expect(champ.external_id).to eq("W182736273") }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue