Class: PhraseAppUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/phraseapp_updater.rb,
lib/phraseapp_updater/differ.rb,
lib/phraseapp_updater/version.rb,
lib/phraseapp_updater/index_by.rb,
lib/phraseapp_updater/locale_file.rb,
lib/phraseapp_updater/phraseapp_api.rb,
lib/phraseapp_updater/yml_config_loader.rb,
lib/phraseapp_updater/locale_file/loader.rb,
lib/phraseapp_updater/locale_file/json_file.rb,
lib/phraseapp_updater/locale_file/yaml_file.rb

Defined Under Namespace

Modules: IndexBy Classes: Differ, LocaleFile, LocaleFileUpdates, PhraseAppAPI, YMLConfigLoader

Constant Summary collapse

VERSION =
"0.1.7"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phraseapp_api_key, phraseapp_project_id, file_format) ⇒ PhraseAppUpdater

Returns a new instance of PhraseAppUpdater.



12
13
14
15
# File 'lib/phraseapp_updater.rb', line 12

def initialize(phraseapp_api_key, phraseapp_project_id, file_format)
  @locale_file_class = LocaleFile.class_for_file_format(file_format)
  @phraseapp_api     = PhraseAppAPI.new(phraseapp_api_key, phraseapp_project_id, @locale_file_class)
end

Class Method Details

.load_config(config_file_path) ⇒ Object



17
18
19
# File 'lib/phraseapp_updater.rb', line 17

def self.load_config(config_file_path)
  YMLConfigLoader.new(config_file_path)
end

Instance Method Details

#pull(fallback_locales_path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/phraseapp_updater.rb', line 72

def pull(fallback_locales_path)
  phraseapp_locales = @phraseapp_api.download_locales

  phraseapp_files_without_unverified = load_phraseapp_files(phraseapp_locales, true)
  phraseapp_files_with_unverified    = load_phraseapp_files(phraseapp_locales, false)
  fallback_files                     = load_locale_files(fallback_locales_path).first

  validate_files!([phraseapp_files_with_unverified, phraseapp_files_without_unverified, fallback_files])

  phraseapp_files_with_unverified = phraseapp_files_with_unverified.index_by(&:name)
  fallback_files                  = fallback_files.index_by(&:name)

  # Clean empty strings from the data and merge the fallback data in:
  # we want to replace unverified keys with their values in the fallback
  phraseapp_files_without_unverified.map do |phraseapp_without_unverified_file|
    without_unverified = clear_empty_strings!(phraseapp_without_unverified_file.parsed_content)
    with_unverified    = clear_empty_strings!(phraseapp_files_with_unverified[phraseapp_without_unverified_file.name].parsed_content)
    fallback           = clear_empty_strings!(fallback_files[phraseapp_without_unverified_file.name].parsed_content)

    restore_unverified_originals!(fallback, with_unverified, without_unverified)
    @locale_file_class.from_hash(phraseapp_without_unverified_file.name, without_unverified)
  end
end

#push(previous_locales_path, new_locales_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/phraseapp_updater.rb', line 21

def push(previous_locales_path, new_locales_path)
  phraseapp_locales = @phraseapp_api.download_locales

  phraseapp_files = load_phraseapp_files(phraseapp_locales, false)

  (previous_locale_files, new_locale_files) =
    load_locale_files(previous_locales_path, new_locales_path)

  validate_files!([phraseapp_files, previous_locale_files, new_locale_files])

  new_locale_files = new_locale_files.index_by(&:name)
  phraseapp_files  = phraseapp_files.index_by(&:name)

  resolved_files = previous_locale_files.map do |previous_locale_file|
    new_locale_file = new_locale_files.fetch(previous_locale_file.name)
    phraseapp_file  = phraseapp_files.fetch(previous_locale_file.name)

    resolved_content = Differ.resolve!(original: previous_locale_file.parsed_content,
                                       primary: new_locale_file.parsed_content,
                                       secondary: phraseapp_file.parsed_content)

    @locale_file_class.from_hash(previous_locale_file.name, resolved_content)
  end

  # Upload all of the secondary languages first,
  # so that the missing keys in them get filled in
  # with blanks on PhraseApp by the default locale.
  # If we do the clean up after uploading the default
  # locale file, these blanks will get cleaned because
  # they're not mentioned in the secondary locale files.

  default_locale_file = find_default_locale_file(phraseapp_locales, resolved_files)

  resolved_files.delete(default_locale_file)

  changed_files = resolved_files.select do |file|
    file.parsed_content != phraseapp_files[file.name].parsed_content
  end

  upload_ids = @phraseapp_api.upload_files(changed_files)
  @phraseapp_api.remove_keys_not_in_uploads(upload_ids)

  puts "Uploading #{default_locale_file}"
  upload_id = @phraseapp_api.upload_file(default_locale_file)

  puts "Removing keys not in upload #{upload_id}"
  @phraseapp_api.remove_keys_not_in_upload(upload_id)

  LocaleFileUpdates.new(phraseapp_files.values, [default_locale_file] + resolved_files)
end