Class: Woods::Notion::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/woods/notion/exporter.rb

Overview

Orchestrates syncing Woods extraction data to Notion databases.

Reads extraction output from disk via IndexReader, maps model and column data to Notion page properties, and pushes via the Notion API. All syncs are idempotent —existing pages are updated, new pages are created.

Examples:

exporter = Exporter.new(index_dir: "tmp/woods")
stats = exporter.sync_all
# => { data_models: 10, columns: 45, errors: [] }

Constant Summary collapse

MAX_ERRORS =
100

Instance Method Summary collapse

Constructor Details

#initialize(index_dir:, config: Woods.configuration, client: nil, reader: nil) ⇒ Exporter

Returns a new instance of Exporter.

Parameters:

  • index_dir (String)

    Path to extraction output directory

  • config (Configuration) (defaults to: Woods.configuration)

    Woods configuration (default: global config)

  • client (Client, nil) (defaults to: nil)

    Notion API client (auto-created from config if nil)

  • reader (Object, nil) (defaults to: nil)

    IndexReader instance (auto-created from index_dir if nil)

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/woods/notion/exporter.rb', line 27

def initialize(index_dir:, config: Woods.configuration, client: nil, reader: nil)
  api_token = config.notion_api_token
  raise ConfigurationError, 'notion_api_token is required for Notion export' unless api_token

  @database_ids = config.notion_database_ids || {}
  @client = client || Client.new(api_token: api_token)
  @reader = reader || build_reader(index_dir)
  @page_id_cache = {}
end

Instance Method Details

#sync_allHash

Sync all configured databases. Idempotent — safe to re-run.

Returns:

  • (Hash)

    { data_models: Integer, columns: Integer, errors: Array<String> }



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/woods/notion/exporter.rb', line 40

def sync_all
  model_stats = @database_ids[:data_models] ? sync_data_models : empty_stats
  column_stats = @database_ids[:columns] && @database_ids[:data_models] ? sync_columns : empty_stats

  all_errors = model_stats[:errors] + column_stats[:errors]

  {
    data_models: model_stats[:synced],
    columns: column_stats[:synced],
    errors: cap_errors(all_errors)
  }
end

#sync_columnsHash

Sync column data to the Columns Notion database.

Returns:

  • (Hash)

    { synced: Integer, errors: Array<String> }



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/woods/notion/exporter.rb', line 71

def sync_columns
  database_id = @database_ids[:columns]
  return empty_stats unless database_id

  synced = 0
  errors = []

  each_model_unit do |entry, unit_data|
    synced_count, unit_errors = sync_model_columns(entry, unit_data, database_id)
    synced += synced_count
    errors.concat(unit_errors)
  end

  { synced: synced, errors: errors }
end

#sync_data_modelsHash

Sync model units to the Data Models Notion database.

Returns:

  • (Hash)

    { synced: Integer, errors: Array<String> }



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/woods/notion/exporter.rb', line 56

def sync_data_models
  database_id = @database_ids[:data_models]
  return empty_stats unless database_id

  migration_dates = load_migration_dates
  sync_units('model', database_id, 'Table Name') do |unit_data|
    properties = Mappers::ModelMapper.new.map(unit_data)
    enrich_with_migration_date(properties, migration_dates)
    properties
  end
end