2022-09-22 17:13:53 +02:00
|
|
|
require 'json_schemer'
|
|
|
|
|
2020-08-05 18:40:47 +02:00
|
|
|
class APIEntreprise::RNAAdapter < APIEntreprise::Adapter
|
2022-09-22 17:13:53 +02:00
|
|
|
class InvalidSchemaError < ::StandardError
|
|
|
|
def initialize(errors)
|
|
|
|
super(errors.map(&:to_json).join("\n"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-19 15:45:55 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def get_resource
|
2022-07-20 14:52:17 +02:00
|
|
|
api(@procedure_id).rna(@siret)
|
2018-03-19 15:45:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_params
|
2022-09-22 17:13:53 +02:00
|
|
|
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)
|
2018-10-04 16:22:19 +02:00
|
|
|
|
2022-09-22 17:13:53 +02:00
|
|
|
params
|
|
|
|
else
|
|
|
|
{}
|
2018-02-28 17:16:03 +01:00
|
|
|
end
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
|
2022-09-22 17:13:53 +02:00
|
|
|
def schemer
|
|
|
|
@schemer ||= JSONSchemer.schema(Rails.root.join('app/schemas/association.json'))
|
|
|
|
end
|
|
|
|
|
2015-12-11 10:36:16 +01:00
|
|
|
def attr_to_fetch
|
2018-01-15 14:42:48 +01:00
|
|
|
[
|
|
|
|
:titre,
|
|
|
|
:objet,
|
|
|
|
:date_creation,
|
|
|
|
:date_declaration,
|
|
|
|
:date_publication
|
2015-12-11 10:36:16 +01:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|