Class: Fastlane::Helper::MetadataDownloader
- Inherits:
-
Object
- Object
- Fastlane::Helper::MetadataDownloader
- Defined in:
- lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb
Instance Attribute Summary collapse
-
#target_files ⇒ Object
readonly
Returns the value of attribute target_files.
-
#target_folder ⇒ Object
readonly
Returns the value of attribute target_folder.
Instance Method Summary collapse
-
#delete_existing_metadata(target_locale) ⇒ Object
Some small helpers.
-
#download(target_locale, glotpress_url, is_source) ⇒ Object
Downloads data from GlotPress, in JSON format.
- #get_target_file_path(locale, file_name) ⇒ Object
-
#initialize(target_folder, target_files, auto_retry) ⇒ MetadataDownloader
constructor
A new instance of MetadataDownloader.
-
#parse_data(target_locale, loc_data, is_source) ⇒ Object
Parse JSON data and update the local files.
-
#reparse_alternates(target_locale, loc_data, is_source) ⇒ Object
Parse JSON data and update the local files.
-
#save_metadata(locale, file_name, content) ⇒ Object
Writes the downloaded content to the target file.
- #update_key(target_locale, key, file, data, msg) ⇒ Object
Constructor Details
#initialize(target_folder, target_files, auto_retry) ⇒ MetadataDownloader
Returns a new instance of MetadataDownloader.
12 13 14 15 16 17 |
# 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 = {} end |
Instance Attribute Details
#target_files ⇒ Object (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_folder ⇒ Object (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
96 97 98 99 100 101 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 96 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
20 21 22 23 24 25 26 27 28 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 20 def download(target_locale, glotpress_url, is_source) GlotPressDownloader.download( url: glotpress_url, locale: target_locale, auto_retry: @auto_retry ) do |response_body| handle_glotpress_response(response_body: response_body, locale: target_locale, is_source: is_source) end end |
#get_target_file_path(locale, file_name) ⇒ Object
103 104 105 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 103 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 31 def parse_data(target_locale, loc_data, is_source) (target_locale) if loc_data.nil? UI. "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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 54 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
86 87 88 89 90 91 92 93 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 86 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
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 71 def update_key(target_locale, key, file, data, msg) = msg.length if data.key?(:max_size) && (data[:max_size] != 0) && ( > data[:max_size]) if data.key?(:alternate_key) UI.("#{target_locale} translation for #{key} exceeds maximum length (#{}). Switching to the alternate translation.") @alternates[data[:alternate_key]] = { desc: data[:desc], max_size: 0 } else UI.("Rejecting #{target_locale} translation for #{key}: translation length: #{} - max allowed length: #{data[:max_size]}") end else (target_locale, file[1][:desc], msg) end end |