Class: Deliver::AppScreenshotValidator

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

Defined Under Namespace

Classes: ValidationError

Constant Summary collapse

ALLOWED_SCREENSHOT_FILE_EXTENSION =

Access each array by symbol returned from FastImage.type

{ png: ['png', 'PNG'], jpeg: ['jpg', 'JPG', 'jpeg', 'JPEG'] }.freeze
APP_SCREENSHOT_SPEC_URL =
'https://help.apple.com/app-store-connect/#/devd274dd925'.freeze

Class Method Summary collapse

Class Method Details

.get_formatted_size(screenshot) ⇒ Object



102
103
104
105
106
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 102

def self.get_formatted_size(screenshot)
  size = FastImage.size(screenshot.path)
  return size.join('x') if size
  nil
end

.validate(screenshot, errors) ⇒ Boolean

Validate a screenshot and inform an error message via ‘errors` parameter. `errors` is mutated to append the messages and each message should contain the corresponding path to let users know which file is throwing the error.

Parameters:

Returns:

  • (Boolean)

    true if given screenshot is valid



44
45
46
47
48
49
50
51
52
53
54
55
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 44

def self.validate(screenshot, errors)
  # Given screenshot will be diagnosed and errors found are accumulated
  errors_found = []

  validate_screen_size(screenshot, errors_found)
  validate_device_type(screenshot, errors_found)
  validate_file_extension_and_format(screenshot, errors_found)

  # Merge errors found into given errors array
  errors_found.each { |error| errors.push(error) }
  errors_found.empty?
end

.validate_device_type(screenshot, errors_found) ⇒ Object

Checking if the device type exists in spaceship Ex: iPhone 6.1 inch isn’t supported in App Store Connect but need to have it in there for frameit support



68
69
70
71
72
73
74
75
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 68

def self.validate_device_type(screenshot, errors_found)
  if !screenshot.screen_size.nil? && screenshot.device_type.nil?
    errors_found << ValidationError.new(type: ValidationError::UNACCEPTABLE_DEVICE,
                                        path: screenshot.path,
                                        debug_info: "Screen size #{screenshot.screen_size} is not accepted. See the specifications to fix #{APP_SCREENSHOT_SPEC_URL}",
                                        to_skip: true)
  end
end

.validate_file_extension_and_format(screenshot, errors_found) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 77

def self.validate_file_extension_and_format(screenshot, errors_found)
  extension = File.extname(screenshot.path).delete('.')
  valid_file_extensions = ALLOWED_SCREENSHOT_FILE_EXTENSION.values.flatten
  is_valid_extension = valid_file_extensions.include?(extension)

  unless is_valid_extension
    errors_found << ValidationError.new(type: ValidationError::INVALID_FILE_EXTENSION,
                                        path: screenshot.path,
                                        debug_info: "Only #{valid_file_extensions.join(', ')} are allowed")
  end

  format = FastImage.type(screenshot.path)
  is_extension_matched = ALLOWED_SCREENSHOT_FILE_EXTENSION[format] &&
                         ALLOWED_SCREENSHOT_FILE_EXTENSION[format].include?(extension)

  # This error only appears when file extension is valid
  if is_valid_extension && !is_extension_matched
    expected_extension = ALLOWED_SCREENSHOT_FILE_EXTENSION[format].first
    expected_filename = File.basename(screenshot.path, File.extname(screenshot.path)) + ".#{expected_extension}"
    errors_found << ValidationError.new(type: ValidationError::FILE_EXTENSION_MISMATCH,
                                        path: screenshot.path,
                                        debug_info: %(Actual format is "#{format}". Rename the filename to "#{expected_filename}".))
  end
end

.validate_screen_size(screenshot, errors_found) ⇒ Object



57
58
59
60
61
62
63
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 57

def self.validate_screen_size(screenshot, errors_found)
  if screenshot.screen_size.nil?
    errors_found << ValidationError.new(type: ValidationError::INVALID_SCREEN_SIZE,
                                        path: screenshot.path,
                                        debug_info: "Actual size is #{get_formatted_size(screenshot)}. See the specifications to fix #{APP_SCREENSHOT_SPEC_URL}")
  end
end