Class: PhraseAppUpdater::PhraseAppAPI
- Inherits:
-
Object
- Object
- PhraseAppUpdater::PhraseAppAPI
show all
- Defined in:
- lib/phraseapp_updater/phraseapp_api.rb
Defined Under Namespace
Classes: BadAPIKeyError, BadProjectIDError, Locale, MissingGitParentError, ProjectNameTakenError, ProjectNotFoundError
Constant Summary
collapse
- GIT_TAG_PREFIX =
'gitancestor_'
- PAGE_SIZE =
100
Instance Method Summary
collapse
Constructor Details
#initialize(api_key, project_id, locale_file_class) ⇒ PhraseAppAPI
Returns a new instance of PhraseAppAPI.
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 16
def initialize(api_key, project_id, locale_file_class)
config = Phrase::Configuration.new do |c|
c.api_key['Authorization'] = api_key
c.api_key_prefix['Authorization'] = 'token'
c.debugging = false
end
@client = Phrase::ApiClient.new(config)
@project_id = project_id
@locale_file_class = locale_file_class
end
|
Instance Method Details
#create_locale(name, default: false) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 74
def create_locale(name, default: false)
params = Phrase::LocaleCreateParameters.new(
name: name,
code: name,
default: default,
)
phraseapp_request(Phrase::LocalesApi, :locale_create, @project_id, params)
end
|
#create_project(name, parent_commit) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 28
def create_project(name, parent_commit)
params = Phrase::ProjectCreateParameters.new(
name: name,
main_format: @locale_file_class.phraseapp_type)
project = phraseapp_request(Phrase::ProjectsApi, :project_create, params)
STDERR.puts "Created project #{name} for #{parent_commit}"
@project_id = project.id
store_parent_commit(parent_commit)
project.id
end
|
#download_files(locales, skip_unverified:) ⇒ Object
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 83
def download_files(locales, skip_unverified:)
results = threaded_request(locales) do |locale|
STDERR.puts "Downloading file for #{locale}"
download_locale(locale, skip_unverified)
end
locales.zip(results).map do |locale, file_contents|
@locale_file_class.from_file_content(locale.name, file_contents)
end
end
|
#download_locale(locale, skip_unverified) ⇒ Object
162
163
164
165
166
167
168
169
170
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 162
def download_locale(locale, skip_unverified)
opts = {
file_format: @locale_file_class.phraseapp_type,
skip_unverified_translations: skip_unverified,
}
phraseapp_request(Phrase::LocalesApi, :locale_download, @project_id, locale.id, return_type: 'String', **opts)
end
|
#fetch_locales ⇒ Object
69
70
71
72
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 69
def fetch_locales
locales = paginated_request(Phrase::LocalesApi, :locales_list, @project_id)
locales.map { |pa_locale| Locale.new(pa_locale) }
end
|
#lookup_project_id(name) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 43
def lookup_project_id(name)
result, = paginated_request(Phrase::ProjectsApi, :projects_list, per_page: PAGE_SIZE, limit: 1) { |p| p.name == name }
raise ProjectNotFoundError.new(name) if result.nil?
result.id
end
|
#read_parent_commit ⇒ Object
We mark projects with their parent git commit using a tag with a well-known prefix. We only allow one tag with this prefix at once.
53
54
55
56
57
58
59
60
61
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 53
def read_parent_commit
git_tag, = paginated_request(Phrase::TagsApi, :tags_list, @project_id, limit: 1) do |t|
t.name.start_with?(GIT_TAG_PREFIX)
end
raise MissingGitParentError.new if git_tag.nil?
git_tag.name.delete_prefix(GIT_TAG_PREFIX)
end
|
#remove_keys_not_in_upload(upload_id) ⇒ Object
195
196
197
198
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 195
def remove_keys_not_in_upload(upload_id)
delete_pattern = "unmentioned_in_upload:#{upload_id}"
phraseapp_request(Phrase::KeysApi, :keys_delete_collection, @project_id, q: delete_pattern)
end
|
#remove_keys_not_in_uploads(upload_ids) ⇒ Object
155
156
157
158
159
160
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 155
def remove_keys_not_in_uploads(upload_ids)
threaded_request(upload_ids) do |upload_id|
STDERR.puts "Removing keys not in upload #{upload_id}"
remove_keys_not_in_upload(upload_id)
end
end
|
#update_parent_commit(commit_hash) ⇒ Object
63
64
65
66
67
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 63
def update_parent_commit(commit_hash)
previous_parent = read_parent_commit
phraseapp_request(Phrase::TagsApi, :tag_delete, @project_id, GIT_TAG_PREFIX + previous_parent)
store_parent_commit(commit_hash)
end
|
#upload_file(locale_file) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 172
def upload_file(locale_file)
Tempfile.create([locale_file.locale_name, ".json"]) do |f|
f.write(locale_file.content)
f.close
opts = {
file: f,
file_encoding: 'UTF-8',
file_format: @locale_file_class.phraseapp_type,
locale_id: locale_file.locale_name,
skip_unverification: false,
update_translations: true,
tags: [generate_upload_tag],
}
result = phraseapp_request(Phrase::UploadsApi, :upload_create, @project_id, **opts)
result.id
end
end
|
#upload_files(locale_files, default_locale:) ⇒ Object
Empirically, PhraseApp fails to parse the uploaded files when uploaded in parallel. Give it a better chance by uploading them one at a time.
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 96
def upload_files(locale_files, default_locale:)
is_default = ->(l) { l.locale_name == default_locale }
STDERR.puts('Creating locales')
known_locales = fetch_locales.index_by(&:name)
threaded_request(locale_files) do |locale_file|
unless known_locales.has_key?(locale_file.locale_name)
create_locale(locale_file.locale_name, default: is_default.(locale_file))
end
end
locale_files.sort! do |a, b|
next -1 if is_default.(a)
next 1 if is_default.(b)
a.locale_name <=> b.locale_name
end
uploads = {}
uploads = locale_files.to_h do |locale_file|
STDERR.puts("Uploading #{locale_file}")
upload_id = upload_file(locale_file)
[upload_id, locale_file]
end
successful_upload_ids = {}
STDERR.puts('Verifying uploads...')
until uploads.empty?
threaded_request(uploads.to_a) do |upload_id, locale_file|
upload = phraseapp_request(Phrase::UploadsApi, :upload_show, @project_id, upload_id)
case upload.state
when "enqueued", "processing"
STDERR.puts("#{locale_file}: still processing")
when "success"
STDERR.puts("#{locale_file}: success")
successful_upload_ids[locale_file.locale_name] = upload_id
uploads.delete(upload_id)
when "error"
STDERR.puts("#{locale_file}: upload failure, retrying")
new_upload_id = upload_file(locale_file)
uploads.delete(upload_id)
uploads[new_upload_id] = locale_file
else
raise RuntimeError.new("Unknown upload state: #{upload.state}")
end
end
sleep(2) unless uploads.empty?
end
successful_upload_ids
end
|