Class: Fastlane::Helper::MetadataDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb

Constant Summary collapse

AUTO_RETRY_SLEEP_TIME =
20
MAX_AUTO_RETRY_ATTEMPTS =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_folder, target_files, auto_retry) ⇒ MetadataDownloader

Returns a new instance of MetadataDownloader.



12
13
14
15
16
17
18
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 12

def initialize(target_folder, target_files, auto_retry)
  @target_folder = target_folder
  @target_files = target_files
  @auto_retry = auto_retry
  @alternates = {}
  @auto_retry_attempt_counter = 0
end

Instance Attribute Details

#target_filesObject (readonly)

Returns the value of attribute target_files.



10
11
12
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 10

def target_files
  @target_files
end

#target_folderObject (readonly)

Returns the value of attribute target_folder.



10
11
12
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 10

def target_folder
  @target_folder
end

Instance Method Details

#delete_existing_metadata(target_locale) ⇒ Object

Some small helpers



93
94
95
96
97
98
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 93

def (target_locale)
  @target_files.each do |file|
    file_path = get_target_file_path(target_locale, file[1][:desc])
    FileUtils.rm_f(file_path)
  end
end

#download(target_locale, glotpress_url, is_source) ⇒ Object

Downloads data from GlotPress, in JSON format



21
22
23
24
25
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 21

def download(target_locale, glotpress_url, is_source)
  uri = URI(glotpress_url)
  response = Net::HTTP.get_response(uri)
  handle_glotpress_download(response: response, locale: target_locale, is_source: is_source)
end

#get_target_file_path(locale, file_name) ⇒ Object



100
101
102
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 100

def get_target_file_path(locale, file_name)
  "#{@target_folder}/#{locale}/#{file_name}"
end

#parse_data(target_locale, loc_data, is_source) ⇒ Object

Parse JSON data and update the local files



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 28

def parse_data(target_locale, loc_data, is_source)
  (target_locale)

  if loc_data.nil?
    UI.message "No translation available for #{target_locale}"
    return
  end

  loc_data.each do |d|
    key = d[0].split("\u0004").first
    source = d[0].split("\u0004").last

    target_files.each do |file|
      next unless file[0].to_s == key

      data = file[1]
      msg = is_source ? source : d[1].first || '' # In the JSON, each Hash value is an array, with zero or one entry
      update_key(target_locale, key, file, data, msg)
    end
  end
end

#reparse_alternates(target_locale, loc_data, is_source) ⇒ Object

Parse JSON data and update the local files



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 51

def reparse_alternates(target_locale, loc_data, is_source)
  loc_data.each do |d|
    key = d[0].split("\u0004").first
    source = d[0].split("\u0004").last

    @alternates.each do |file|
      puts "Data: #{file[0]} - key: #{key}"
      next unless file[0].to_s == key

      puts "Alternate: #{key}"
      data = file[1]
      msg = is_source ? source : d[1].first || '' # In the JSON, each Hash value is an array, with zero or one entry
      update_key(target_locale, key, file, data, msg)
    end
  end
end

#save_metadata(locale, file_name, content) ⇒ Object

Writes the downloaded content to the target file



83
84
85
86
87
88
89
90
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 83

def (locale, file_name, content)
  file_path = get_target_file_path(locale, file_name)

  dir_path = File.dirname(file_path)
  FileUtils.mkdir_p(dir_path)

  File.open(file_path, 'w') { |file| file.puts(content) }
end

#update_key(target_locale, key, file, data, msg) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 68

def update_key(target_locale, key, file, data, msg)
  message_len = msg.length
  if data.key?(:max_size) && (data[:max_size] != 0) && (message_len > data[:max_size])
    if data.key?(:alternate_key)
      UI.message("#{target_locale} translation for #{key} exceeds maximum length (#{message_len}). Switching to the alternate translation.")
      @alternates[data[:alternate_key]] = { desc: data[:desc], max_size: 0 }
    else
      UI.message("Rejecting #{target_locale} translation for #{key}: translation length: #{message_len} - max allowed length: #{data[:max_size]}")
    end
  else
    (target_locale, file[1][:desc], msg)
  end
end