feat(api particulier): add Pole Emploi adapter
This commit is contained in:
parent
2e05a3fde2
commit
4cfc9f7d86
7 changed files with 382 additions and 0 deletions
|
@ -4,6 +4,7 @@ class APIParticulier::API
|
|||
INTROSPECT_RESOURCE_NAME = "introspect"
|
||||
COMPOSITION_FAMILIALE_RESOURCE_NAME = "v2/composition-familiale"
|
||||
AVIS_IMPOSITION_RESOURCE_NAME = "v2/avis-imposition"
|
||||
SITUATION_POLE_EMPLOI = "v2/situations-pole-emploi"
|
||||
|
||||
TIMEOUT = 20
|
||||
|
||||
|
@ -29,6 +30,10 @@ class APIParticulier::API
|
|||
referenceAvis: reference_avis.to_i.to_s.rjust(13, "0"))
|
||||
end
|
||||
|
||||
def situation_pole_emploi(identifiant)
|
||||
get(SITUATION_POLE_EMPLOI, identifiant: identifiant)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get(resource_name, params = {})
|
||||
|
|
46
app/lib/api_particulier/pole_emploi_adapter.rb
Normal file
46
app/lib/api_particulier/pole_emploi_adapter.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
class APIParticulier::PoleEmploiAdapter
|
||||
class InvalidSchemaError < ::StandardError
|
||||
def initialize(errors)
|
||||
super(errors.map(&:to_json).join("\n"))
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(api_particulier_token, identifiant, requested_sources)
|
||||
@api = APIParticulier::API.new(api_particulier_token)
|
||||
@identifiant = identifiant
|
||||
@requested_sources = requested_sources
|
||||
end
|
||||
|
||||
def to_params
|
||||
@api.situation_pole_emploi(@identifiant)
|
||||
.tap { |d| ensure_valid_schema!(d) }
|
||||
.then { |d| extract_requested_sources(d) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_valid_schema!(data)
|
||||
if !schemer.valid?(data)
|
||||
errors = schemer.validate(data).to_a
|
||||
raise InvalidSchemaError.new(errors)
|
||||
end
|
||||
end
|
||||
|
||||
def schemer
|
||||
@schemer ||= JSONSchemer.schema(Rails.root.join('app/schemas/situation-pole-emploi.json'))
|
||||
end
|
||||
|
||||
def extract_requested_sources(data)
|
||||
@requested_sources['pole_emploi']&.map do |(scope, sources)|
|
||||
case scope
|
||||
when 'adresse'
|
||||
sources.map { |source| { scope => data[scope].slice(*source) } }
|
||||
when 'identifiant', 'contact', 'inscription'
|
||||
sources.map { |source| { scope => data.slice(*source) } }
|
||||
else
|
||||
{ scope => data.slice(*sources) }
|
||||
end
|
||||
end
|
||||
&.flatten&.reduce(&:deep_merge) || {}
|
||||
end
|
||||
end
|
87
app/schemas/situation-pole-emploi.json
Normal file
87
app/schemas/situation-pole-emploi.json
Normal file
|
@ -0,0 +1,87 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"$id": "http://demarches-simplifiees.fr/situation-pole-emploi.schema.json",
|
||||
"title": "situation pole emploi",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"identifiant": {
|
||||
"type": "string"
|
||||
},
|
||||
"civilite": {
|
||||
"type": "string"
|
||||
},
|
||||
"nom": {
|
||||
"type": "string"
|
||||
},
|
||||
"nomUsage": {
|
||||
"type": "string"
|
||||
},
|
||||
"prenom": {
|
||||
"type": "string"
|
||||
},
|
||||
"sexe": {
|
||||
"enum": ["F", "M"]
|
||||
},
|
||||
"dateNaissance": {
|
||||
"format": "date",
|
||||
"type": "string"
|
||||
},
|
||||
"codeCertificationCNAV": {
|
||||
"type": "string"
|
||||
},
|
||||
"telephone": {
|
||||
"type": "string"
|
||||
},
|
||||
"telephone2": {
|
||||
"type": "string"
|
||||
},
|
||||
"email": {
|
||||
"type": "string"
|
||||
},
|
||||
"$defs": {
|
||||
"adresse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"codePostal": {
|
||||
"type": "string"
|
||||
},
|
||||
"INSEECommune": {
|
||||
"type": "string"
|
||||
},
|
||||
"localite": {
|
||||
"type": "string"
|
||||
},
|
||||
"ligneVoie": {
|
||||
"type": "string"
|
||||
},
|
||||
"ligneComplementDestinataire": {
|
||||
"type": "string"
|
||||
},
|
||||
"ligneComplementAdresse": {
|
||||
"type": "string"
|
||||
},
|
||||
"ligneComplementDistribution": {
|
||||
"type": "string"
|
||||
},
|
||||
"ligneNom": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"dateInscription": {
|
||||
"format": "date",
|
||||
"type": "string"
|
||||
},
|
||||
"dateCessationInscription": {
|
||||
"format": "date",
|
||||
"type": "string"
|
||||
},
|
||||
"codeCategorieInscription": {
|
||||
"type": "integer"
|
||||
},
|
||||
"libelleCategroiieInscription": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue