add CNAFAdapter

This commit is contained in:
simon lehericey 2021-09-22 11:00:03 +02:00
parent a7651e3772
commit 75043070da
5 changed files with 204 additions and 0 deletions

View file

@ -0,0 +1,29 @@
class APIParticulier::CNAFAdapter
def initialize(api_particulier_token, numero_allocataire, code_postal, requested_sources)
@api = APIParticulier::API.new(api_particulier_token)
@numero_allocataire = numero_allocataire
@code_postal = code_postal
@requested_sources = requested_sources
end
def to_params
@api.composition_familiale(@numero_allocataire, @code_postal)
.then { |d| extract_requested_sources(d) }
end
private
def extract_requested_sources(data)
@requested_sources['cnaf']&.map do |(scope, sources)|
case scope
when 'enfants', 'allocataires'
{ scope => data[scope].map { |s| s.slice(*sources) } }
when 'quotient_familial'
{ scope => data.slice(*sources) }
else
{ scope => data[scope].slice(*sources) }
end
end
&.reduce(&:deep_merge) || {}
end
end

View file

@ -14,6 +14,7 @@ ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'JSON' inflect.acronym 'JSON'
inflect.acronym 'RNA' inflect.acronym 'RNA'
inflect.acronym 'URL' inflect.acronym 'URL'
inflect.acronym 'CNAF'
inflect.irregular 'type_de_champ', 'types_de_champ' inflect.irregular 'type_de_champ', 'types_de_champ'
inflect.irregular 'type_de_champ_private', 'types_de_champ_private' inflect.irregular 'type_de_champ_private', 'types_de_champ_private'
inflect.irregular 'procedure_revision_type_de_champ', 'procedure_revision_types_de_champ' inflect.irregular 'procedure_revision_type_de_champ', 'procedure_revision_types_de_champ'

View file

@ -0,0 +1,79 @@
---
http_interactions:
- request:
method: get
uri: https://particulier-test.api.gouv.fr/api/v2/composition-familiale?codePostal=92110&numeroAllocataire=5843972
body:
encoding: US-ASCII
string: ''
headers:
User-Agent:
- demarches-simplifiees.fr
Accept:
- application/json
X-Api-Key:
- 29eb50b65f64e8e00c0847a8bbcbd150e1f847
Content-Type:
- text/html; charset=utf-8
Expect:
- ''
response:
status:
code: 200
message: ''
headers:
Server:
- nginx
Date:
- Mon, 20 Sep 2021 20:01:39 GMT
Content-Type:
- application/json; charset=utf-8
Content-Length:
- '387'
X-Powered-By:
- Express
Vary:
- Origin
Etag:
- W/"183-5m9o/ng+PfdMOPeC0VBn5lqbziQ"
Strict-Transport-Security:
- max-age=15724800; includeSubdomains
body:
encoding: UTF-8
string: '
{
"adresse": {
"codePostalVille": "92110 Clichy",
"identite": "Mr SNOW Eric",
"complementIdentite": "ne connait rien",
"numeroRue": "109 rue La Boétie",
"pays": "FRANCE",
"complementIdentiteGeo": "au nord de paris",
"lieuDit": "glagla"
},
"allocataires": [
{
"nomPrenom": "ERIC SNOW",
"dateDeNaissance": "07011991",
"sexe": "M"
},
{
"nomPrenom": "SANSA SNOW",
"dateDeNaissance": "15011992",
"sexe": "F"
}
],
"enfants": [
{
"nomPrenom": "PAUL SNOW",
"dateDeNaissance": "04012018",
"sexe": "M"
}
],
"quotientFamilial": 1856,
"annee": 2021,
"mois": 6
}
'
recorded_at: Mon, 20 Sep 2021 20:01:39 GMT
recorded_with: VCR 6.0.0

View file

@ -0,0 +1,35 @@
{
"allocataires": [
{
"dateDeNaissance": "07011991",
"nomPrenom": "ERIC SNOW",
"sexe": "M"
},
{
"dateDeNaissance": "15011992",
"nomPrenom": "SANSA SNOW",
"sexe": "F"
}
],
"enfants": [
{
"nomPrenom": "PAUL SNOW",
"dateDeNaissance": "04012018",
"sexe": "M"
}
],
"quotient_familial" : {
"quotientFamilial": 1856,
"annee": 2021,
"mois": 6
},
"adresse": {
"codePostalVille": "92110 Clichy",
"identite": "Mr SNOW Eric",
"complementIdentite": "ne connait rien",
"numeroRue": "109 rue La Boétie",
"pays": "FRANCE",
"complementIdentiteGeo": "au nord de paris",
"lieuDit": "glagla"
}
}

View file

@ -0,0 +1,60 @@
describe APIParticulier::CNAFAdapter do
let(:adapter) { described_class.new(api_particulier_token, numero_allocataire, code_postal, requested_sources) }
before { stub_const("API_PARTICULIER_URL", "https://particulier-test.api.gouv.fr/api") }
describe '#to_params' do
subject do
VCR.use_cassette("api_particulier/success/composition_familiale") do
adapter.to_params
end
end
context 'when the token has all the cnaf scopes' do
let(:api_particulier_token) { '29eb50b65f64e8e00c0847a8bbcbd150e1f847' }
let(:numero_allocataire) { '5843972' }
let(:code_postal) { '92110' }
context 'and all the sources are requested' do
let(:requested_sources) do
{
'cnaf' => {
'allocataires' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
'enfants' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
'adresse' => ['identite', 'complementIdentite', 'complementIdentiteGeo', 'numeroRue', 'lieuDit', 'codePostalVille', 'pays'],
'quotient_familial' => ['quotientFamilial', 'annee', 'mois']
}
}
end
let(:result) { JSON.parse(File.read('spec/fixtures/files/api_particulier/composition_familiale.json')) }
it { is_expected.to eq(result) }
end
context 'when no sources is requested' do
let(:requested_sources) { {} }
it { is_expected.to eq({}) }
end
context 'when a scalar is requested' do
let(:requested_sources) { { 'cnaf' => { 'adresse' => ['pays'] } } }
it { is_expected.to eq({ "adresse" => { "pays" => "FRANCE" } }) }
end
context 'when a quotient_familial is requested' do
let(:requested_sources) { { 'cnaf' => { 'quotient_familial' => ['annee'] } } }
it { is_expected.to eq({ "quotient_familial" => { "annee" => 2021 } }) }
end
context 'when a vector is requested' do
let(:requested_sources) { { 'cnaf' => { 'allocataires' => ['nomPrenom'] } } }
it { is_expected.to eq({ "allocataires" => [{ "nomPrenom" => "ERIC SNOW" }, { "nomPrenom" => "SANSA SNOW" }] }) }
end
end
end
end