Merge branch 'dev'

This commit is contained in:
gregoirenovel 2018-03-20 13:25:08 +01:00
commit 3a302ca280
13 changed files with 91 additions and 90 deletions

View file

@ -0,0 +1,52 @@
class ApiEntreprise::API
class << self
attr_accessor :token
end
ENTREPRISE_RESOURCE_NAME = "entreprises"
ETABLISSEMENT_RESOURCE_NAME = "etablissements"
EXERCICES_RESOURCE_NAME = "exercices"
RNA_RESOURCE_NAME = "associations"
def initialize
end
def self.entreprise(siren, procedure_id)
call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
end
def self.etablissement(siret, procedure_id)
call(ETABLISSEMENT_RESOURCE_NAME, siret, procedure_id)
end
def self.exercices(siret, procedure_id)
call(EXERCICES_RESOURCE_NAME, siret, procedure_id)
end
def self.rna(siret, procedure_id)
call(RNA_RESOURCE_NAME, siret, procedure_id)
end
def self.call(resource_name, siret_or_siren, procedure_id)
url = url(resource_name, siret_or_siren)
params = params(siret_or_siren, procedure_id)
verify_ssl_mode = OpenSSL::SSL::VERIFY_NONE
result = RestClient::Resource.new(url, verify_ssl: verify_ssl_mode).get(params: params)
JSON.parse(result, symbolize_names: true)
end
def self.url(resource_name, siret_or_siren)
[API_ENTREPRISE_URL, resource_name, siret_or_siren].join("/")
end
def self.params(siret_or_siren, procedure_id)
{
context: "demarches-simplifiees.fr",
recipient: siret_or_siren,
object: "procedure_id: #{procedure_id}",
token: SIADETOKEN
}
end
end

View file

@ -1,15 +1,9 @@
class SIADE::EntrepriseAdapter class ApiEntreprise::EntrepriseAdapter
def initialize(siren, procedure_id) def initialize(siren, procedure_id)
@siren = siren @siren = siren
@procedure_id = procedure_id @procedure_id = procedure_id
end end
def data_source
@data_source ||= JSON.parse(SIADE::API.entreprise(@siren, @procedure_id), symbolize_names: true)
rescue
@data_source = nil
end
def success? def success?
data_source data_source
rescue rescue
@ -26,6 +20,12 @@ class SIADE::EntrepriseAdapter
private private
def data_source
@data_source ||= ApiEntreprise::API.entreprise(@siren, @procedure_id)
rescue
@data_source = nil
end
def attr_to_fetch def attr_to_fetch
[ [
:siren, :siren,

View file

@ -1,13 +1,9 @@
class SIADE::EtablissementAdapter class ApiEntreprise::EtablissementAdapter
def initialize(siret, procedure_id) def initialize(siret, procedure_id)
@siret = siret @siret = siret
@procedure_id = procedure_id @procedure_id = procedure_id
end end
def data_source
@data_source ||= JSON.parse(SIADE::API.etablissement(@siret, @procedure_id), symbolize_names: true)
end
def success? def success?
data_source data_source
rescue rescue
@ -26,6 +22,10 @@ class SIADE::EtablissementAdapter
private private
def data_source
@data_source ||= ApiEntreprise::API.etablissement(@siret, @procedure_id)
end
def attr_to_fetch def attr_to_fetch
[ [
:adresse, :adresse,

View file

@ -1,15 +1,9 @@
class SIADE::ExercicesAdapter class ApiEntreprise::ExercicesAdapter
def initialize(siret, procedure_id) def initialize(siret, procedure_id)
@siret = siret @siret = siret
@procedure_id = procedure_id @procedure_id = procedure_id
end end
def data_source
@data_source ||= JSON.parse(SIADE::API.exercices(@siret, @procedure_id), symbolize_names: true)
rescue
@data_source = nil
end
def to_params def to_params
data_source[:exercices].map do |exercice| data_source[:exercices].map do |exercice|
exercice.slice(*attr_to_fetch) exercice.slice(*attr_to_fetch)
@ -20,6 +14,12 @@ class SIADE::ExercicesAdapter
private private
def data_source
@data_source ||= ApiEntreprise::API.exercices(@siret, @procedure_id)
rescue
@data_source = nil
end
def attr_to_fetch def attr_to_fetch
[:ca, :date_fin_exercice, :date_fin_exercice_timestamp] [:ca, :date_fin_exercice, :date_fin_exercice_timestamp]
end end

View file

@ -1,13 +1,9 @@
class SIADE::RNAAdapter class ApiEntreprise::RNAAdapter
def initialize(siret, procedure_id) def initialize(siret, procedure_id)
@siret = siret @siret = siret
@procedure_id = procedure_id @procedure_id = procedure_id
end end
def data_source
@data_source ||= JSON.parse(SIADE::API.rna(@siret, @procedure_id), symbolize_names: true)
end
def to_params def to_params
if data_source[:association][:id].nil? if data_source[:association][:id].nil?
return nil return nil
@ -21,6 +17,10 @@ class SIADE::RNAAdapter
private private
def data_source
@data_source ||= ApiEntreprise::API.rna(@siret, @procedure_id)
end
def attr_to_fetch def attr_to_fetch
[ [
:titre, :titre,

View file

@ -1,51 +0,0 @@
class SIADE::API
class << self
attr_accessor :token
end
def initialize
end
def self.entreprise(siren, procedure_id)
endpoint = "/v2/entreprises/#{siren}"
call(base_url + endpoint, mandatory_params(siren, procedure_id))
end
def self.etablissement(siret, procedure_id)
endpoint = "/v2/etablissements/#{siret}"
call(base_url + endpoint, mandatory_params(siret, procedure_id))
end
def self.exercices(siret, procedure_id)
endpoint = "/v2/exercices/#{siret}"
call(base_url + endpoint, mandatory_params(siret, procedure_id))
end
def self.rna(siret, procedure_id)
endpoint = "/v2/associations/#{siret}"
call(base_url + endpoint, mandatory_params(siret, procedure_id))
end
def self.call(url, params = {})
params[:token] = SIADETOKEN
verify_ssl_mode = OpenSSL::SSL::VERIFY_NONE
RestClient::Resource.new(
url,
verify_ssl: verify_ssl_mode
).get(params: params)
end
def self.mandatory_params(siret_or_siren, procedure_id)
{
context: "demarches-simplifiees.fr",
recipient: siret_or_siren,
object: "procedure_id: #{procedure_id}"
}
end
def self.base_url
SIADEURL
end
end

View file

@ -1,11 +1,11 @@
class SIRETService class SIRETService
def self.fetch(siret, dossier = nil) def self.fetch(siret, dossier = nil)
etablissement = SIADE::EtablissementAdapter.new(siret, dossier&.procedure_id) etablissement = ApiEntreprise::EtablissementAdapter.new(siret, dossier&.procedure_id)
entreprise = SIADE::EntrepriseAdapter.new(siren(siret), dossier&.procedure_id) entreprise = ApiEntreprise::EntrepriseAdapter.new(siren(siret), dossier&.procedure_id)
if etablissement.success? && entreprise.success? if etablissement.success? && entreprise.success?
association = SIADE::RNAAdapter.new(siret, dossier&.procedure_id) association = ApiEntreprise::RNAAdapter.new(siret, dossier&.procedure_id)
exercices = SIADE::ExercicesAdapter.new(siret, dossier&.procedure_id) exercices = ApiEntreprise::ExercicesAdapter.new(siret, dossier&.procedure_id)
params = etablissement.to_params params = etablissement.to_params
.merge(entreprise.to_params.map { |k,v| ["entreprise_#{k}", v] }.to_h) .merge(entreprise.to_params.map { |k,v| ["entreprise_#{k}", v] }.to_h)

View file

@ -1,5 +1,5 @@
if Rails.env.production? if Rails.env.production?
SIADEURL = 'https://entreprise.api.gouv.fr' API_ENTREPRISE_URL = 'https://entreprise.api.gouv.fr/v2'
else else
SIADEURL = 'https://staging.entreprise.api.gouv.fr' API_ENTREPRISE_URL = 'https://staging.entreprise.api.gouv.fr/v2'
end end

View file

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe SIADE::API do describe ApiEntreprise::API do
let(:procedure_id) { 12 } let(:procedure_id) { 12 }
describe '.entreprise' do describe '.entreprise' do
subject { described_class.entreprise(siren, procedure_id) } subject { described_class.entreprise(siren, procedure_id) }
@ -23,7 +23,7 @@ describe SIADE::API do
let(:body) { File.read('spec/support/files/entreprise.json') } let(:body) { File.read('spec/support/files/entreprise.json') }
it 'returns response body' do it 'returns response body' do
expect(subject).to eq(body) expect(subject).to eq(JSON.parse(body, symbolize_names: true))
end end
end end
end end
@ -51,7 +51,7 @@ describe SIADE::API do
let(:body) { File.read('spec/support/files/etablissement.json') } let(:body) { File.read('spec/support/files/etablissement.json') }
it 'returns body' do it 'returns body' do
expect(subject).to eq(body) expect(subject).to eq(JSON.parse(body, symbolize_names: true))
end end
end end
end end
@ -82,7 +82,7 @@ describe SIADE::API do
let(:body) { File.read('spec/support/files/exercices.json') } let(:body) { File.read('spec/support/files/exercices.json') }
it 'raises RestClient::Unauthorized' do it 'raises RestClient::Unauthorized' do
expect(subject).to eq(body) expect(subject).to eq(JSON.parse(body, symbolize_names: true))
end end
end end
end end
@ -110,7 +110,7 @@ describe SIADE::API do
let(:status) { 200 } let(:status) { 200 }
let(:body) { File.read('spec/support/files/rna.json') } let(:body) { File.read('spec/support/files/rna.json') }
it{ expect(subject).to eq(body) } it { expect(subject).to eq(JSON.parse(body, symbolize_names: true)) }
end end
end end
end end

View file

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe SIADE::EntrepriseAdapter do describe ApiEntreprise::EntrepriseAdapter do
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure_id) { 22 } let(:procedure_id) { 22 }
let(:adapter) { described_class.new(siren, procedure_id) } let(:adapter) { described_class.new(siren, procedure_id) }

View file

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe SIADE::EtablissementAdapter do describe ApiEntreprise::EtablissementAdapter do
let(:procedure_id) { 33 } let(:procedure_id) { 33 }
context 'SIRET valide' do context 'SIRET valide' do

View file

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe SIADE::ExercicesAdapter do describe ApiEntreprise::ExercicesAdapter do
let(:siret) { '41816609600051' } let(:siret) { '41816609600051' }
let(:procedure_id) { 11 } let(:procedure_id) { 11 }
subject { described_class.new(siret, procedure_id).to_params } subject { described_class.new(siret, procedure_id).to_params }

View file

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe SIADE::RNAAdapter do describe ApiEntreprise::RNAAdapter do
let(:siret) { '50480511000013' } let(:siret) { '50480511000013' }
let(:procedure_id) { 22 } let(:procedure_id) { 22 }
let(:body) { File.read('spec/support/files/rna.json') } let(:body) { File.read('spec/support/files/rna.json') }