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
Constant Summary
collapse
- GIT_TAG_PREFIX =
'gitancestor_'
Instance Method Summary
collapse
Constructor Details
#initialize(api_key, project_id, locale_file_class) ⇒ PhraseAppAPI
Returns a new instance of PhraseAppAPI.
14
15
16
17
18
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 14
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
#create_locale(name, default: false) ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 59
def create_locale(name, default: false)
phraseapp_request do
params = PhraseApp::RequestParams::LocaleParams.new(
name: name,
code: name,
default: default,
)
@client.locale_create(@project_id, params)
end
end
|
#create_project(name, parent_commit) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 20
def create_project(name, parent_commit)
params = PhraseApp::RequestParams::ProjectParams.new(
name: name,
main_format: @locale_file_class.phraseapp_type)
project = phraseapp_request { @client.project_create(params) }
STDERR.puts "Created project #{name} for #{parent_commit}"
@project_id = project.id
store_parent_commit(parent_commit)
project.id
end
|
#download_file(locale, skip_unverified) ⇒ Object
102
103
104
105
106
107
108
109
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 102
def download_file(locale, skip_unverified)
download_params = PhraseApp::RequestParams::LocaleDownloadParams.new
download_params.file_format = @locale_file_class.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
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 70
def download_files(locales, skip_unverified:)
results = threaded_request(locales) do |locale|
STDERR.puts "Downloading file for #{locale}"
download_file(locale, skip_unverified)
end
locales.zip(results).map do |locale, file_contents|
@locale_file_class.from_file_content(locale.name, file_contents)
end
end
|
#fetch_locales ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 51
def fetch_locales
phraseapp_request { @client.locales_list(@project_id, 1, 100) }.map do |pa_locale|
Locale.new(pa_locale)
end
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.
35
36
37
38
39
40
41
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 35
def read_parent_commit
tags = phraseapp_request { @client.tags_list(@project_id, 1, 100) }
git_tag = tags.detect { |t| t.name.start_with?(GIT_TAG_PREFIX) }
raise MissingGitParentError.new if git_tag.nil?
git_tag.name.delete_prefix(GIT_TAG_PREFIX)
end
|
#remove_keys_not_in_upload(upload_id) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 125
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
end
end
|
#remove_keys_not_in_uploads(upload_ids) ⇒ Object
95
96
97
98
99
100
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 95
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
43
44
45
46
47
48
49
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 43
def update_parent_commit(commit_hash)
previous_parent = read_parent_commit
phraseapp_request do
@client.tag_delete(@project_id, GIT_TAG_PREFIX + previous_parent)
end
store_parent_commit(commit_hash)
end
|
#upload_file(locale_file) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 111
def upload_file(locale_file)
upload_params = create_upload_params(locale_file.locale_name)
Tempfile.create([locale_file.locale_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, default_locale:) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/phraseapp_updater/phraseapp_api.rb', line 81
def upload_files(locale_files, default_locale:)
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: (locale_file.locale_name == default_locale))
end
STDERR.puts "Uploading #{locale_file}"
upload_file(locale_file)
end
end
|