Class: Fastlane::Helper::MetadataDownloader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_folder, target_files) ⇒ MetadataDownloader

Returns a new instance of MetadataDownloader.



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

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

Instance Attribute Details

#target_filesObject (readonly)

Returns the value of attribute target_files.



7
8
9
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 7

def target_files
  @target_files
end

#target_folderObject (readonly)

Returns the value of attribute target_folder.



7
8
9
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 7

def target_folder
  @target_folder
end

Instance Method Details

#delete_existing_metadata(target_locale) ⇒ Object

Some small helpers



88
89
90
91
92
93
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 88

def (target_locale)
  @target_files.each do |file|
    file_path = get_target_file_path(target_locale, file[1][:desc])
    File.delete(file_path) if File.exist? file_path
  end
end

#download(target_locale, glotpress_url, is_source) ⇒ Object

Downloads data from GlotPress, in JSON format



16
17
18
19
20
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 16

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



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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 23

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|
      if 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
end

#reparse_alternates(target_locale, loc_data, is_source) ⇒ Object

Parse JSON data and update the local files



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 46

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}"
      if 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
end

#save_metadata(locale, file_name, content) ⇒ Object

Writes the downloaded content to the target file



78
79
80
81
82
83
84
85
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 78

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) unless File.exist?(dir_path)

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

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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/metadata_download_helper.rb', line 63

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