Module: Einvoicing::FR::SiretLookup
- Defined in:
- lib/einvoicing/fr/siret_lookup.rb
Constant Summary collapse
- API_URL =
"https://recherche-entreprises.api.gouv.fr/search"
Class Method Summary collapse
-
.enrich!(party) ⇒ Object
Enrich a Party object by fetching and setting its SIRET from the API.
-
.find(siren) ⇒ Object
Find SIRET for a given SIREN using the French government Sirene API.
Class Method Details
.enrich!(party) ⇒ Object
Enrich a Party object by fetching and setting its SIRET from the API. Only calls the API if party.siren is present and party.siret is blank. Returns the party.
43 44 45 46 47 48 49 50 51 |
# File 'lib/einvoicing/fr/siret_lookup.rb', line 43 def self.enrich!(party) return party if party.siren.to_s.strip.empty? return party if party.respond_to?(:siret) && !party.siret.to_s.strip.empty? result = find(party.siren.to_s.gsub(/\s/, "")) return party unless result&.dig(:siret) party.with(siret: result[:siret]) end |
.find(siren) ⇒ Object
Find SIRET for a given SIREN using the French government Sirene API. Returns { siret:, name:, address: } or nil on any error.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/einvoicing/fr/siret_lookup.rb', line 14 def self.find(siren) return nil unless siren.to_s.match?(/\A\d{9}\z/) uri = URI(API_URL) uri.query = URI.encode_www_form(q: siren.to_s, mtq: "true") response = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 5, read_timeout: 10) do |http| http.get(uri.request_uri) end return nil unless response.code == "200" data = JSON.parse(response.body) result = data["results"]&.first return nil unless result siege = result["siege"] || {} siret = siege["siret"] return nil if siret.nil? || siret.empty? { siret: siret, name: result["nom_complet"], address: siege["adresse"] } rescue StandardError nil end |