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



86
87
88
89
90
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 86

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:

  • Pass an array object to add validation errors when detecting errors. This will be mutated to add more error objects as validation detects errors.

Returns:

  • true if given screenshot is valid



41
42
43
44
45
46
47
48
49
50
51
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 41

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

  validate_screen_size(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_file_extension_and_format(screenshot, errors_found) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 61

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



53
54
55
56
57
58
59
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 53

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