Class: Deliver::AppScreenshotIterator

Inherits:
Object
  • Object
show all
Defined in:
deliver/lib/deliver/app_screenshot_iterator.rb

Overview

This is a convenient class that enumerates app store connect’s screenshots in various degrees.

Constant Summary collapse

NUMBER_OF_THREADS =
Helper.test? ? 1 : [ENV.fetch("DELIVER_NUMBER_OF_THREADS", 10).to_i, 10].min

Instance Method Summary collapse

Constructor Details

#initialize(localizations) ⇒ AppScreenshotIterator

Returns a new instance of AppScreenshotIterator.



7
8
9
# File 'deliver/lib/deliver/app_screenshot_iterator.rb', line 7

def initialize(localizations)
  @localizations = localizations
end

Instance Method Details

#each_app_screenshot {|localization, app_screenshot_set, app_screenshot| ... } ⇒ Object

Iterate app_screenshot over localizations and app_screenshot_sets

Yields:

  • (localization, app_screenshot_set, app_screenshot)

Yield Parameters:



45
46
47
48
49
50
51
52
53
# File 'deliver/lib/deliver/app_screenshot_iterator.rb', line 45

def each_app_screenshot(&block)
  return enum_for(__method__) unless block_given?

  each_app_screenshot_set do |localization, app_screenshot_set|
    app_screenshot_set.app_screenshots.each do |app_screenshot|
      yield(localization, app_screenshot_set, app_screenshot)
    end
  end
end

#each_app_screenshot_set(localizations = @localizations) {|localization, app_screenshot_set| ... } ⇒ Object

Iterate app_screenshot_set over localizations

Yields:

  • (localization, app_screenshot_set)

Yield Parameters:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'deliver/lib/deliver/app_screenshot_iterator.rb', line 16

def each_app_screenshot_set(localizations = @localizations, &block)
  return enum_for(__method__, localizations) unless block_given?

  # Collect app_screenshot_sets from localizations in parallel but
  # limit the number of threads working at a time with using `lazy` and `force` controls
  # to not attack App Store Connect
  results = localizations.each_slice(NUMBER_OF_THREADS).lazy.map do |localizations_grouped|
    localizations_grouped.map do |localization|
      Thread.new do
        [localization, localization.get_app_screenshot_sets]
      end
    end
  end.flat_map do |threads|
    threads.map { |t| t.join.value }
  end.force

  results.each do |localization, app_screenshot_sets|
    app_screenshot_sets.each do |app_screenshot_set|
      yield(localization, app_screenshot_set)
    end
  end
end

#each_local_screenshot(screenshots_per_language) {|localization, app_screenshot_set, app_screenshot| ... } ⇒ Object

Iterate given local app_screenshot over localizations and app_screenshot_sets

Parameters:

Yields:

  • (localization, app_screenshot_set, app_screenshot)

Yield Parameters:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'deliver/lib/deliver/app_screenshot_iterator.rb', line 63

def each_local_screenshot(screenshots_per_language, &block)
  return enum_for(__method__, screenshots_per_language) unless block_given?

  # filter unnecessary localizations
  supported_localizations = @localizations.reject { |l| screenshots_per_language[l.locale].nil? }

  # build a hash that can access app_screenshot_set corresponding to given locale and display_type
  # via parallelized each_app_screenshot_set to gain performance
  app_screenshot_set_per_locale_and_display_type = each_app_screenshot_set(supported_localizations)
                                                   .each_with_object({}) do |(localization, app_screenshot_set), hash|
    hash[localization.locale] ||= {}
    hash[localization.locale][app_screenshot_set.screenshot_display_type] = app_screenshot_set
  end

  # iterate over screenshots per localization
  screenshots_per_language.each do |language, screenshots_for_language|
    localization = supported_localizations.find { |l| l.locale == language }
    screenshots_per_display_type = screenshots_for_language.reject { |screenshot| screenshot.device_type.nil? }.group_by(&:device_type)

    screenshots_per_display_type.each do |display_type, screenshots|
      # create AppScreenshotSet for given display_type if it doesn't exist
      app_screenshot_set = (app_screenshot_set_per_locale_and_display_type[language] || {})[display_type]
      app_screenshot_set ||= localization.create_app_screenshot_set(attributes: { screenshotDisplayType: display_type })

      # iterate over screenshots per display size with index
      screenshots.each.with_index do |screenshot, index|
        yield(localization, app_screenshot_set, screenshot, index)
      end
    end
  end
end