demarches-normaliennes/app/services/api_recherche_entreprises_service.rb

36 lines
901 B
Ruby
Raw Normal View History

class APIRechercheEntreprisesService
include Dry::Monads[:result]
def self.collectivite_territoriale?(siret:)
response = APIRechercheEntreprisesService.new.call(siret:)
return false if response.failure?
response.success&.dig(:complements, :collectivite_territoriale).present?
end
def call(siret:)
result = API::Client.new.(url: "#{url}?q=#{siret}")
return result if result.failure?
body = result.success.body
return Success(nil) if body[:results].empty?
# the api returns the matching structure in the first element if it exists
structure = body[:results][0]
# safety check : the api does fuzzy matching, so we need to check that the siret matches
return Failure() if structure[:matching_etablissements].all? { _1[:siret] != siret }
Success(structure)
end
private
def url
"#{API_RECHERCHE_ENTREPRISE_URL}/search"
end
end