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

53 lines
1.1 KiB
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]
return {} if params.nil?
Sentry.with_scope do |scope|
scope.set_tags(siret: @siret)
scope.set_extras(source: params)
params = params.slice(*attr_to_fetch) if @depreciated
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)
params
else
{}
end
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