Module: Einvoicing::SiretLookup

Defined in:
lib/einvoicing/siret_lookup.rb

Overview

Looks up SIRET and company name from a SIREN number using the free French government API (no authentication required).

Constant Summary collapse

API_URL =
"https://recherche-entreprises.api.gouv.fr/search"
SIREN_RE =
/\A\d{9}\z/

Class Method Summary collapse

Class Method Details

.find(siren) ⇒ Hash?

Find company info for a given SIREN number.

Parameters:

  • siren (String, nil)

    9-digit SIREN number

Returns:

  • (Hash, nil)

    { siret:, name:, address: } or nil on any error



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/einvoicing/siret_lookup.rb', line 18

def self.find(siren)
  return nil unless siren.to_s.match?(SIREN_RE)

  uri = URI(API_URL)
  uri.query = URI.encode_www_form(q: siren, mtq: "true")

  response = fetch(uri)
  return nil if response.nil?

  parse(response)
rescue StandardError
  nil
end