Class: Labimotion::ExportTpaSegments

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/export_tpa_segments.rb

Overview

ExportTpaSegments Builds the segment section of the reaction-variations payload handed to a third-party app (TPA). Segment values are element-wide -- one set per element, not per variation -- so they travel once, next to the variations.

Only the segments and layers named in segment_layers (by default Constants::Tpa::SEGMENT_LAYERS) are exported, each field reduced to what the app needs:

{ "<segment klass label>" => { "<layer key>" => { "<field>" => { label:, value:, unit:, type: } } } }

A segment the element does not carry, or a layer its segment does not carry, is left out.

Constant Summary collapse

LAYERS =

Mirrors Labimotion::Prop::LAYERS / ::FIELDS. Held locally because requiring labimotion/utils/prop here would race the gem's autoload.

'layers'
FIELDS =
'fields'

Class Method Summary collapse

Class Method Details

.call(element, segment_layers: Labimotion::Constants::Tpa::SEGMENT_LAYERS) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/labimotion/libs/export_tpa_segments.rb', line 26

def call(element, segment_layers: Labimotion::Constants::Tpa::SEGMENT_LAYERS)
  segment_layers.each_with_object({}) do |(segment_label, layer_keys), payload|
    segment = element.segments.detect { |s| s.segment_klass&.label == segment_label }
    next unless segment

    payload[segment_label] = layers(segment, layer_keys)
  end
end