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 dataROOT_FIELDS_TO_DELETE- Fields removed after processingFORMALITY_FIELDS_TO_EXTRACT- Formality-specific fieldsFORMALITY_FIELDS_TO_DELETE- Formality fields to clean upZERO_VALUE_FIELDS- Fields with zero values to removeENTERPRISE_CONTENT_FIELDS- Enterprise content fieldsENTERPRISE_CONTENT_FIELDS_TO_DELETE- Enterprise fields to clean up
Key Methods
Data Extraction Methods
extract_root_data- Main orchestrator for root data extractionextract_root_fields- Extracts specific fields from raw dataremove_processed_root_fields- Cleans up processed fieldsmerge_formality_fields- Handles formality data mergingremove_processed_formality_fields- Cleans up formality fields
Identity Processing Methods
identite- Main identity processing methodperson_has_identite?- Validates identity data presenceprepare_identite_data- Prepares data for processingbuild_enterprise_data- Builds enterprise data structuremerge_description- Handles description mergingmerge_content_fields- Processes content field operations
Notes about updated fields
There is 3 fields related to update time
updatedin companies table is storing the fierld value from the apiapi_updated_at. is for the last time the companyParser ran successfully for that recordupdated_atis the rails fields storing the last change on comapnies/companies_infos for that record
Data Normalization
normalize_diffusion_insee- Processes INSEE diffusion flagsremove_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
- Extract root data using
extract_root_data - Process identity information via
identite - Normalize and clean data
- Validate extracted data
- 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
- Always validate data before processing
- Check parser.errors after processing
- Use appropriate error handling for your use case
- 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:
UpdateCompanyJobfor background processingParserJobsystem for job management- Company models for data persistence
- Various association models (facilities, rights, etc.)