Skip to main content

Company Parser Implementation Guide

Overview

The InpiAPI::Parsers::CompanyParser class handles parsing and processing of company data from external APIs. This guide documents the current implementation structure.

Class Structure

Constants

The parser uses several constants to define field processing rules:

  • ROOT_FIELDS_TO_EXTRACT - Fields extracted from root data
  • ROOT_FIELDS_TO_DELETE - Fields removed after processing
  • FORMALITY_FIELDS_TO_EXTRACT - Formality-specific fields
  • FORMALITY_FIELDS_TO_DELETE - Formality fields to clean up
  • ZERO_VALUE_FIELDS - Fields with zero values to remove
  • ENTERPRISE_CONTENT_FIELDS - Enterprise content fields
  • ENTERPRISE_CONTENT_FIELDS_TO_DELETE - Enterprise fields to clean up

Key Methods

Data Extraction Methods

  • extract_root_data - Main orchestrator for root data extraction
  • extract_root_fields - Extracts specific fields from raw data
  • remove_processed_root_fields - Cleans up processed fields
  • merge_formality_fields - Handles formality data merging
  • remove_processed_formality_fields - Cleans up formality fields

Identity Processing Methods

  • identite - Main identity processing method
  • person_has_identite? - Validates identity data presence
  • prepare_identite_data - Prepares data for processing
  • build_enterprise_data - Builds enterprise data structure
  • merge_description - Handles description merging
  • merge_content_fields - Processes content field operations

Notes about updated fields

There is 3 fields related to update time

  • updated in companies table is storing the fierld value from the api
  • api_updated_at. is for the last time the companyParser ran successfully for that record
  • updated_at is the rails fields storing the last change on comapnies/companies_infos for that record

Data Normalization

  • normalize_diffusion_insee - Processes INSEE diffusion flags
  • remove_zero_value_fields - Removes fields with zero values

Usage Guidelines

Basic Usage

parser = InpiAPI::Parsers::CompanyParser.new(raw_data, company)
parser.update
if parser.validate_extracted_data
# Processing successful
else
# Handle errors: parser.errors
end

Data Processing Flow

  1. Extract root data using extract_root_data
  2. Process identity information via identite
  3. Normalize and clean data
  4. Validate extracted data
  5. Update company record

Error Handling

The parser includes comprehensive error handling:

  • Validation of required data fields
  • Graceful handling of missing or malformed data
  • Detailed error reporting via parser.errors
  • Rollback capabilities for failed operations

Best Practices

  1. Always validate data before processing
  2. Check parser.errors after processing
  3. Use appropriate error handling for your use case
  4. Test with various data formats to ensure robustness

Testing

When testing the parser:

RSpec.describe InpiAPI::Parsers::CompanyParser do
let(:parser) { described_class.new(valid_data, company) }

it 'processes valid data successfully' do
parser.update
expect(parser.validate_extracted_data).to be true
end

it 'handles invalid data gracefully' do
invalid_parser = described_class.new(invalid_data, company)
invalid_parser.update
expect(invalid_parser.validate_extracted_data).to be false
expect(invalid_parser.errors).to be_present
end
end

Integration

The parser integrates with:

  • UpdateCompanyJob for background processing
  • ParserJob system for job management
  • Company models for data persistence
  • Various association models (facilities, rights, etc.)