Class: PhraseAppUpdater::PhraseAppAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/phraseapp_updater/phraseapp_api.rb

Defined Under Namespace

Classes: BadAPIKeyError, BadProjectIDError, Locale

Instance Method Summary collapse

Constructor Details

#initialize(api_key, project_id, locale_file_class) ⇒ PhraseAppAPI

Returns a new instance of PhraseAppAPI.



8
9
10
11
12
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 8

def initialize(api_key, project_id, locale_file_class)
  @client            = PhraseApp::Client.new(PhraseApp::Auth::Credentials.new(token: api_key))
  @project_id        = project_id
  @locale_file_class = locale_file_class
end

Instance Method Details

#download_file(locale, skip_unverified) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 46

def download_file(locale, skip_unverified)
  download_params = PhraseApp::RequestParams::LocaleDownloadParams.new

  download_params.file_format                  = @locale_file_class.const_get(:PHRASEAPP_TYPE)
  download_params.skip_unverified_translations = skip_unverified

  phraseapp_request { @client.locale_download(@project_id, locale.id, download_params) }
end

#download_files(locales, skip_unverified) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 23

def download_files(locales, skip_unverified)
  threaded_request(locales) do |locale|
    puts "Downloading file for #{locale}"
    download_file(locale, skip_unverified)
  end.map do |locale, file_contents|
    @locale_file_class.new(locale.name, file_contents)
  end
end

#download_localesObject



14
15
16
17
18
19
20
21
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 14

def download_locales
  # This is a paginated API, however the maximum page size of 100
  # is well above our expected locale size,
  # so we take the first page only for now
  phraseapp_request { @client.locales_list(@project_id, 1, 100) }.map do |pa_locale|
    Locale.new(pa_locale)
  end
end

#remove_keys_not_in_upload(upload_id) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 69

def remove_keys_not_in_upload(upload_id)
  delete_params   = PhraseApp::RequestParams::KeysDeleteParams.new
  delete_params.q = "unmentioned_in_upload:#{upload_id}"

  begin
    phraseapp_request { @client.keys_delete(@project_id, delete_params) }
  rescue RuntimeError => e
    # PhraseApp will accept but mark invalid uploads, however the gem
    # returns the same response in both cases. If we call this API
    # with the ID of an upload of a bad file, it will fail.
    # This usually occurs when sending up an empty file, which is
    # a case we can ignore. However, it'd be better to have a way
    # to detect a bad upload and find the cause.
  end
end

#remove_keys_not_in_uploads(upload_ids) ⇒ Object



39
40
41
42
43
44
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 39

def remove_keys_not_in_uploads(upload_ids)
  threaded_request(upload_ids) do |upload_id|
    puts "Removing keys not in upload #{upload_id}"
    remove_keys_not_in_upload(upload_id)
  end
end

#upload_file(locale_file) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 55

def upload_file(locale_file)
  upload_params = create_upload_params(locale_file.name)

  # The PhraseApp gem only accepts a filename to upload,
  # so we need to write the file out and pass it the path
  Tempfile.create(["#{locale_file.name}", ".json"]) do |f|
    f.write(locale_file.content)
    f.close

    upload_params.file = f.path
    phraseapp_request { @client.upload_create(@project_id, upload_params) }.id
  end
end

#upload_files(locale_files) ⇒ Object



32
33
34
35
36
37
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 32

def upload_files(locale_files)
  threaded_request(locale_files) do |locale_file|
    puts "Uploading #{locale_file}"
    upload_file(locale_file)
  end.map { |locale_file, upload_id | upload_id }
end