Class: GoodData::LCM2::SegmentsFilter

Inherits:
BaseAction show all
Defined in:
lib/gooddata/lcm/actions/segments_filter.rb

Constant Summary collapse

DESCRIPTION =
'Filter Segments'
PARAMS =
define_params(self) do
  description 'Segments to manage'
  param :segments, array_of(instance_of(Type::SegmentType)), required: true

  description 'Segments to provision'
  param :segments_filter, array_of(instance_of(Type::StringType)), required: false
end

Constants inherited from BaseAction

BaseAction::FAILED_CLIENTS, BaseAction::FAILED_PROJECTS, BaseAction::FAILED_SEGMENTS, BaseAction::SYNC_FAILED_LIST

Constants included from Dsl::Dsl

Dsl::Dsl::DEFAULT_OPTS, Dsl::Dsl::TYPES

Class Method Summary collapse

Methods inherited from BaseAction

add_failed_client, add_failed_project, add_failed_segment, add_new_clients_to_project_client_mapping, check_params, collect_synced_status, continue_on_error, print_result, process_failed_project, process_failed_projects, sync_failed_client, sync_failed_project, sync_failed_segment, without_check

Methods included from Dsl::Dsl

#define_params, #define_type, #process

Class Method Details

.call(params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gooddata/lcm/actions/segments_filter.rb', line 23

def call(params)
  filtered_segments = params.segments

  segment_ids = filtered_segments.map(&:segment_id)
  downcase_segment_ids = segment_ids.map(&:downcase)
  duplicated_segment_ids = segment_ids.select { |e| downcase_segment_ids.count(e.downcase) > 1 }.uniq
  fail "Parameter 'segments' contains duplicate segment id(s): #{duplicated_segment_ids.join(', ')}" if duplicated_segment_ids.any?

  if params.segments_filter
    fail 'Segments filter should be a non-empty array!' if !params.segments_filter.is_a?(Array) || params.segments_filter.empty?

    segments_filter = params.segments_filter.map(&:downcase)

    filtered_segments = params.segments.select do |segment|
      segments_filter.include?(segment.segment_id.downcase)
    end
  end

  # Return results
  {
    results: filtered_segments,
    params: {
      segments: filtered_segments
    }
  }
end