Class: ElasticGraph::Indexer::Operation::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/indexer/operation/factory.rb

Instance Method Summary collapse

Instance Method Details

#build(event) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/elastic_graph/indexer/operation/factory.rb', line 29

def build(event)
  event = prepare_event(event)

  selected_json_schema_version = select_json_schema_version(event) { |failure| return failure }

  # Because the `select_json_schema_version` picks the closest-matching json schema version, the incoming
  # event might not match the expected json_schema_version value in the json schema (which is a `const` field).
  # This is by design, since we're picking a schema based on best-effort, so to avoid that by-design validation error,
  # performing the envelope validation on a "patched" version of the event.
  event_with_patched_envelope = event.merge({JSON_SCHEMA_VERSION_KEY => selected_json_schema_version})

  if (error_message = validator(EVENT_ENVELOPE_JSON_SCHEMA_NAME, selected_json_schema_version).validate_with_error_message(event_with_patched_envelope))
    return build_failed_result(event, "event payload", error_message)
  end

  failed_result = validate_record_returning_failure(event, selected_json_schema_version)
  failed_result || BuildResult.success(build_all_operations_for(
    event,
    record_preparer_factory.for_json_schema_version(selected_json_schema_version)
  ))
end