demarches-normaliennes/app/lib/api_entreprise/rna_adapter.rb

44 lines
988 B
Ruby
Raw Normal View History

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
private
def get_resource
api(@procedure_id).rna(@siret)
end
def process_params
params = data_source[:association]
params = params&.slice(*attr_to_fetch) if @depreciated
2022-09-22 17:13:53 +02:00
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)
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
[
:titre,
:objet,
:date_creation,
:date_declaration,
:date_publication
2015-12-11 10:36:16 +01:00
]
end
end