2018-03-15 16:56:21 +01:00
|
|
|
class ApiEntreprise::API
|
2018-03-15 14:02:29 +01:00
|
|
|
ENTREPRISE_RESOURCE_NAME = "entreprises"
|
|
|
|
ETABLISSEMENT_RESOURCE_NAME = "etablissements"
|
|
|
|
EXERCICES_RESOURCE_NAME = "exercices"
|
|
|
|
RNA_RESOURCE_NAME = "associations"
|
|
|
|
|
2018-03-27 17:59:51 +02:00
|
|
|
TIMEOUT = 15
|
2018-03-27 17:16:56 +02:00
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
def self.entreprise(siren, procedure_id)
|
2018-03-15 14:02:29 +01:00
|
|
|
call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
def self.etablissement(siret, procedure_id)
|
2018-03-15 14:02:29 +01:00
|
|
|
call(ETABLISSEMENT_RESOURCE_NAME, siret, procedure_id)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
def self.exercices(siret, procedure_id)
|
2018-03-15 14:02:29 +01:00
|
|
|
call(EXERCICES_RESOURCE_NAME, siret, procedure_id)
|
2015-11-16 11:23:29 +01:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:02:45 +01:00
|
|
|
def self.rna(siret, procedure_id)
|
2018-03-15 14:02:29 +01:00
|
|
|
call(RNA_RESOURCE_NAME, siret, procedure_id)
|
2015-12-11 10:36:16 +01:00
|
|
|
end
|
|
|
|
|
2018-12-07 10:27:40 +01:00
|
|
|
private
|
|
|
|
|
2018-03-15 14:02:29 +01:00
|
|
|
def self.call(resource_name, siret_or_siren, procedure_id)
|
2018-03-15 14:15:32 +01:00
|
|
|
url = url(resource_name, siret_or_siren)
|
2018-03-15 14:14:50 +01:00
|
|
|
params = params(siret_or_siren, procedure_id)
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2018-03-26 16:38:03 +02:00
|
|
|
response = Typhoeus.get(url,
|
|
|
|
params: params,
|
2018-03-27 17:16:56 +02:00
|
|
|
timeout: TIMEOUT)
|
2018-03-15 14:23:57 +01:00
|
|
|
|
2018-10-04 16:22:19 +02:00
|
|
|
if response.success?
|
2018-03-26 16:38:03 +02:00
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
else
|
|
|
|
raise RestClient::ResourceNotFound
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 14:17:57 +01:00
|
|
|
def self.url(resource_name, siret_or_siren)
|
2018-08-27 16:12:34 +02:00
|
|
|
[API_ENTREPRISE_URL, resource_name, siret_or_siren].join("/")
|
2018-03-15 14:17:57 +01:00
|
|
|
end
|
|
|
|
|
2018-03-15 14:14:50 +01:00
|
|
|
def self.params(siret_or_siren, procedure_id)
|
2018-03-15 12:02:45 +01:00
|
|
|
{
|
|
|
|
context: "demarches-simplifiees.fr",
|
|
|
|
recipient: siret_or_siren,
|
2018-03-15 14:14:50 +01:00
|
|
|
object: "procedure_id: #{procedure_id}",
|
2018-03-16 13:07:20 +01:00
|
|
|
token: token
|
2018-03-15 12:02:45 +01:00
|
|
|
}
|
|
|
|
end
|
2018-03-16 13:07:20 +01:00
|
|
|
|
|
|
|
def self.token
|
|
|
|
Rails.application.secrets.api_entreprise[:key]
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|