web_translate_it 2.5.3 → 2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/wti +107 -107
- data/generators/webtranslateit/lib/insert_commands.rb +6 -8
- data/generators/webtranslateit/webtranslateit_generator.rb +6 -6
- data/history.md +22 -7
- data/lib/web_translate_it/auto_fetch.rb +2 -4
- data/lib/web_translate_it/command_line.rb +264 -230
- data/lib/web_translate_it/configuration.rb +32 -29
- data/lib/web_translate_it/connection.rb +22 -34
- data/lib/web_translate_it/project.rb +22 -25
- data/lib/web_translate_it/string.rb +85 -84
- data/lib/web_translate_it/term.rb +65 -65
- data/lib/web_translate_it/term_translation.rb +34 -36
- data/lib/web_translate_it/translation.rb +25 -32
- data/lib/web_translate_it/translation_file.rb +109 -106
- data/lib/web_translate_it/util/array_util.rb +8 -8
- data/lib/web_translate_it/util/hash_util.rb +5 -5
- data/lib/web_translate_it/util/string_util.rb +5 -8
- data/lib/web_translate_it/util.rb +51 -54
- data/lib/web_translate_it.rb +7 -7
- data/readme.md +13 -9
- data/spec/spec_helper.rb +2 -3
- data/spec/web_translate_it/auto_fetch_spec.rb +1 -2
- data/spec/web_translate_it/string_spec.rb +61 -62
- data/spec/web_translate_it/term_spec.rb +40 -41
- metadata +47 -12
@@ -1,11 +1,10 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module WebTranslateIt
|
3
|
-
class Term
|
2
|
+
class Term # rubocop:todo Metrics/ClassLength
|
4
3
|
require 'net/https'
|
5
4
|
require 'multi_json'
|
6
|
-
|
5
|
+
|
7
6
|
attr_accessor :id, :text, :description, :created_at, :updated_at, :translations, :new_record
|
8
|
-
|
7
|
+
|
9
8
|
# Initialize a new WebTranslateIt::Term
|
10
9
|
#
|
11
10
|
# Implementation Example:
|
@@ -19,18 +18,18 @@ module WebTranslateIt
|
|
19
18
|
# WebTranslateIt::Term.new({ "text" => "Hello", "translations" => [translation_es, translation_fr]})
|
20
19
|
#
|
21
20
|
# to instantiate a new Term with a Term Translations in Spanish and French.
|
22
|
-
|
21
|
+
|
23
22
|
def initialize(params = {})
|
24
23
|
params.stringify_keys!
|
25
|
-
self.id = params[
|
26
|
-
self.text = params[
|
27
|
-
self.description = params[
|
28
|
-
self.created_at = params[
|
29
|
-
self.updated_at = params[
|
30
|
-
self.translations = params[
|
24
|
+
self.id = params['id'] || nil
|
25
|
+
self.text = params['text'] || nil
|
26
|
+
self.description = params['description'] || nil
|
27
|
+
self.created_at = params['created_at'] || nil
|
28
|
+
self.updated_at = params['updated_at'] || nil
|
29
|
+
self.translations = params['translations'] || []
|
31
30
|
self.new_record = true
|
32
31
|
end
|
33
|
-
|
32
|
+
|
34
33
|
# Fetch all terms
|
35
34
|
#
|
36
35
|
# Implementation Example:
|
@@ -40,27 +39,27 @@ module WebTranslateIt
|
|
40
39
|
# end
|
41
40
|
#
|
42
41
|
# puts terms.inspect #=> An array of WebTranslateIt::Term objects
|
43
|
-
|
44
|
-
def self.find_all(params = {})
|
42
|
+
|
43
|
+
def self.find_all(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
45
44
|
success = true
|
46
45
|
tries ||= 3
|
47
46
|
params.stringify_keys!
|
48
47
|
url = "/api/projects/#{Connection.api_key}/terms.yaml"
|
49
|
-
url +=
|
48
|
+
url += "?#{HashUtil.to_params(params)}" unless params.empty?
|
50
49
|
|
51
50
|
request = Net::HTTP::Get.new(url)
|
52
51
|
WebTranslateIt::Util.add_fields(request)
|
53
52
|
begin
|
54
53
|
terms = []
|
55
|
-
while
|
54
|
+
while request
|
56
55
|
response = Connection.http_connection.request(request)
|
57
56
|
YAML.load(response.body).each do |term_response|
|
58
57
|
term = WebTranslateIt::Term.new(term_response)
|
59
58
|
term.new_record = false
|
60
59
|
terms.push(term)
|
61
60
|
end
|
62
|
-
if response[
|
63
|
-
url = response[
|
61
|
+
if response['Link']&.include?('rel="next"')
|
62
|
+
url = response['Link'].match(/<(.*)>; rel="next"/)[1]
|
64
63
|
request = Net::HTTP::Get.new(url)
|
65
64
|
WebTranslateIt::Util.add_fields(request)
|
66
65
|
else
|
@@ -69,8 +68,8 @@ module WebTranslateIt
|
|
69
68
|
end
|
70
69
|
return terms
|
71
70
|
rescue Timeout::Error
|
72
|
-
puts
|
73
|
-
if (tries -= 1)
|
71
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
72
|
+
if (tries -= 1).positive?
|
74
73
|
sleep(5)
|
75
74
|
retry
|
76
75
|
else
|
@@ -79,7 +78,7 @@ module WebTranslateIt
|
|
79
78
|
end
|
80
79
|
success
|
81
80
|
end
|
82
|
-
|
81
|
+
|
83
82
|
# Find a Term based on its ID
|
84
83
|
# Returns a Term object or nil if not found.
|
85
84
|
#
|
@@ -93,8 +92,8 @@ module WebTranslateIt
|
|
93
92
|
#
|
94
93
|
# to find and instantiate the Term which ID is `1234`.
|
95
94
|
#
|
96
|
-
|
97
|
-
def self.find(term_id)
|
95
|
+
|
96
|
+
def self.find(term_id) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
|
98
97
|
success = true
|
99
98
|
tries ||= 3
|
100
99
|
request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/terms/#{term_id}.yaml")
|
@@ -102,12 +101,13 @@ module WebTranslateIt
|
|
102
101
|
begin
|
103
102
|
response = Connection.http_connection.request(request)
|
104
103
|
return nil if response.code.to_i == 404
|
104
|
+
|
105
105
|
term = WebTranslateIt::Term.new(YAML.load(response.body))
|
106
106
|
term.new_record = false
|
107
107
|
return term
|
108
108
|
rescue Timeout::Error
|
109
|
-
puts
|
110
|
-
if (tries -= 1)
|
109
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
110
|
+
if (tries -= 1).positive?
|
111
111
|
sleep(5)
|
112
112
|
retry
|
113
113
|
else
|
@@ -129,9 +129,9 @@ module WebTranslateIt
|
|
129
129
|
#
|
130
130
|
|
131
131
|
def save
|
132
|
-
|
132
|
+
new_record ? create : update
|
133
133
|
end
|
134
|
-
|
134
|
+
|
135
135
|
# Delete a Term on WebTranslateIt.com
|
136
136
|
#
|
137
137
|
# Implementation Example:
|
@@ -141,17 +141,17 @@ module WebTranslateIt
|
|
141
141
|
# term.delete
|
142
142
|
# end
|
143
143
|
#
|
144
|
-
|
145
|
-
def delete
|
144
|
+
|
145
|
+
def delete # rubocop:todo Metrics/MethodLength
|
146
146
|
success = true
|
147
147
|
tries ||= 3
|
148
|
-
request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/terms/#{
|
148
|
+
request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/terms/#{id}")
|
149
149
|
WebTranslateIt::Util.add_fields(request)
|
150
150
|
begin
|
151
151
|
Util.handle_response(Connection.http_connection.request(request), true, true)
|
152
152
|
rescue Timeout::Error
|
153
|
-
puts
|
154
|
-
if (tries -= 1)
|
153
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
154
|
+
if (tries -= 1).positive?
|
155
155
|
sleep(5)
|
156
156
|
retry
|
157
157
|
else
|
@@ -160,7 +160,7 @@ module WebTranslateIt
|
|
160
160
|
end
|
161
161
|
success
|
162
162
|
end
|
163
|
-
|
163
|
+
|
164
164
|
# Gets a Translation for a Term
|
165
165
|
#
|
166
166
|
# Implementation Example:
|
@@ -170,29 +170,30 @@ module WebTranslateIt
|
|
170
170
|
# puts term.translation_for("fr") #=> A TermTranslation object
|
171
171
|
# end
|
172
172
|
#
|
173
|
-
|
174
|
-
def translation_for(locale)
|
173
|
+
|
174
|
+
def translation_for(locale) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
175
175
|
success = true
|
176
176
|
tries ||= 3
|
177
|
-
translation =
|
177
|
+
translation = translations.detect { |t| t.locale == locale }
|
178
178
|
return translation if translation
|
179
|
-
return nil if
|
180
|
-
|
179
|
+
return nil if new_record
|
180
|
+
|
181
|
+
request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/terms/#{id}/locales/#{locale}/translations.yaml")
|
181
182
|
WebTranslateIt::Util.add_fields(request)
|
182
183
|
begin
|
183
184
|
response = Util.handle_response(Connection.http_connection.request(request), true, true)
|
184
185
|
array = YAML.load(response)
|
185
186
|
return nil if array.empty?
|
187
|
+
|
186
188
|
translations = []
|
187
|
-
array.each do |
|
188
|
-
term_translation = WebTranslateIt::TermTranslation.new(
|
189
|
+
array.each do |trans|
|
190
|
+
term_translation = WebTranslateIt::TermTranslation.new(trans)
|
189
191
|
translations.push(term_translation)
|
190
192
|
end
|
191
193
|
return translations
|
192
|
-
|
193
194
|
rescue Timeout::Error
|
194
|
-
puts
|
195
|
-
if (tries -= 1)
|
195
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
196
|
+
if (tries -= 1).positive?
|
196
197
|
sleep(5)
|
197
198
|
retry
|
198
199
|
else
|
@@ -204,23 +205,23 @@ module WebTranslateIt
|
|
204
205
|
|
205
206
|
protected
|
206
207
|
|
207
|
-
def update
|
208
|
+
def update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
208
209
|
success = true
|
209
210
|
tries ||= 3
|
210
|
-
request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{
|
211
|
+
request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{id}.yaml")
|
211
212
|
WebTranslateIt::Util.add_fields(request)
|
212
|
-
request.body =
|
213
|
+
request.body = to_json
|
213
214
|
|
214
|
-
|
215
|
-
translation.term_id =
|
215
|
+
translations.each do |translation|
|
216
|
+
translation.term_id = id
|
216
217
|
translation.save
|
217
218
|
end
|
218
219
|
|
219
220
|
begin
|
220
221
|
Util.handle_response(Connection.http_connection.request(request), true, true)
|
221
222
|
rescue Timeout::Error
|
222
|
-
puts
|
223
|
-
if (tries -= 1)
|
223
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
224
|
+
if (tries -= 1).positive?
|
224
225
|
sleep(5)
|
225
226
|
retry
|
226
227
|
else
|
@@ -229,23 +230,22 @@ module WebTranslateIt
|
|
229
230
|
end
|
230
231
|
success
|
231
232
|
end
|
232
|
-
|
233
|
-
def create
|
233
|
+
|
234
|
+
def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
234
235
|
success = true
|
235
236
|
tries ||= 3
|
236
237
|
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/terms")
|
237
238
|
WebTranslateIt::Util.add_fields(request)
|
238
|
-
request.body =
|
239
|
+
request.body = to_json(true)
|
239
240
|
|
240
241
|
begin
|
241
242
|
response = YAML.load(Util.handle_response(Connection.http_connection.request(request), true, true))
|
242
|
-
self.id = response[
|
243
|
+
self.id = response['id']
|
243
244
|
self.new_record = false
|
244
245
|
return true
|
245
|
-
|
246
246
|
rescue Timeout::Error
|
247
|
-
puts
|
248
|
-
if (tries -= 1)
|
247
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
248
|
+
if (tries -= 1).positive?
|
249
249
|
sleep(5)
|
250
250
|
retry
|
251
251
|
else
|
@@ -254,17 +254,17 @@ module WebTranslateIt
|
|
254
254
|
end
|
255
255
|
success
|
256
256
|
end
|
257
|
-
|
258
|
-
def to_json(with_translations = false)
|
257
|
+
|
258
|
+
def to_json(with_translations = false) # rubocop:todo Metrics/MethodLength
|
259
259
|
hash = {
|
260
|
-
|
261
|
-
|
262
|
-
|
260
|
+
'id' => id,
|
261
|
+
'text' => text,
|
262
|
+
'description' => description
|
263
263
|
}
|
264
|
-
if
|
265
|
-
hash.update({
|
264
|
+
if translations.any? && with_translations
|
265
|
+
hash.update({ 'translations' => [] })
|
266
266
|
translations.each do |translation|
|
267
|
-
hash[
|
267
|
+
hash['translations'].push(translation.to_hash)
|
268
268
|
end
|
269
269
|
end
|
270
270
|
MultiJson.dump(hash)
|
@@ -1,11 +1,10 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module WebTranslateIt
|
3
2
|
class TermTranslation
|
4
3
|
require 'net/https'
|
5
4
|
require 'multi_json'
|
6
|
-
|
5
|
+
|
7
6
|
attr_accessor :id, :locale, :text, :description, :status, :new_record, :term_id
|
8
|
-
|
7
|
+
|
9
8
|
# Initialize a new WebTranslateIt::TermTranslation
|
10
9
|
#
|
11
10
|
# Implementation Example:
|
@@ -14,18 +13,18 @@ module WebTranslateIt
|
|
14
13
|
#
|
15
14
|
# to instantiate a new TermTranslation.
|
16
15
|
#
|
17
|
-
|
16
|
+
|
18
17
|
def initialize(params = {})
|
19
18
|
params.stringify_keys!
|
20
|
-
self.id = params[
|
21
|
-
self.locale = params[
|
22
|
-
self.text = params[
|
23
|
-
self.description = params[
|
24
|
-
self.status = params[
|
25
|
-
self.term_id = params[
|
19
|
+
self.id = params['id'] || nil
|
20
|
+
self.locale = params['locale'] || nil
|
21
|
+
self.text = params['text'] || nil
|
22
|
+
self.description = params['description'] || nil
|
23
|
+
self.status = params['status'] || nil
|
24
|
+
self.term_id = params['term_id'] || nil
|
26
25
|
self.new_record = true
|
27
26
|
end
|
28
|
-
|
27
|
+
|
29
28
|
# Update or Create a WebTranslateIt::TermTranslation
|
30
29
|
#
|
31
30
|
# Implementation Example:
|
@@ -35,43 +34,42 @@ module WebTranslateIt
|
|
35
34
|
# translation.save
|
36
35
|
# end
|
37
36
|
#
|
38
|
-
|
37
|
+
|
39
38
|
def save
|
40
|
-
|
39
|
+
new_record ? create : update
|
41
40
|
end
|
42
|
-
|
41
|
+
|
43
42
|
def to_hash
|
44
43
|
{
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
44
|
+
'id' => id,
|
45
|
+
'locale' => locale,
|
46
|
+
'text' => text,
|
47
|
+
'description' => description,
|
48
|
+
'status' => status
|
50
49
|
}
|
51
50
|
end
|
52
51
|
|
53
|
-
def to_json
|
54
|
-
MultiJson.dump(
|
52
|
+
def to_json(*_args)
|
53
|
+
MultiJson.dump(to_hash)
|
55
54
|
end
|
56
|
-
|
55
|
+
|
57
56
|
protected
|
58
|
-
|
59
|
-
def create
|
57
|
+
|
58
|
+
def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
|
60
59
|
success = true
|
61
60
|
tries ||= 3
|
62
|
-
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/terms/#{
|
61
|
+
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/terms/#{term_id}/locales/#{locale}/translations")
|
63
62
|
WebTranslateIt::Util.add_fields(request)
|
64
|
-
request.body =
|
63
|
+
request.body = to_json
|
65
64
|
|
66
65
|
begin
|
67
66
|
response = YAML.load(Util.handle_response(Connection.http_connection.request(request), true, true))
|
68
|
-
self.id = response[
|
67
|
+
self.id = response['id']
|
69
68
|
self.new_record = false
|
70
69
|
return true
|
71
|
-
|
72
70
|
rescue Timeout::Error
|
73
|
-
puts
|
74
|
-
if (tries -= 1)
|
71
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
72
|
+
if (tries -= 1).positive?
|
75
73
|
sleep(5)
|
76
74
|
retry
|
77
75
|
else
|
@@ -81,17 +79,17 @@ module WebTranslateIt
|
|
81
79
|
success
|
82
80
|
end
|
83
81
|
|
84
|
-
def update
|
82
|
+
def update # rubocop:todo Metrics/MethodLength
|
85
83
|
success = true
|
86
84
|
tries ||= 3
|
87
|
-
request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{
|
85
|
+
request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{id}/locales/#{locale}/translations/#{id}")
|
88
86
|
WebTranslateIt::Util.add_fields(request)
|
89
|
-
request.body =
|
87
|
+
request.body = to_json
|
90
88
|
begin
|
91
|
-
Util.handle_response(Connection.http_connection.request(request), true, true)
|
89
|
+
Util.handle_response(Connection.http_connection.request(request), true, true)
|
92
90
|
rescue Timeout::Error
|
93
|
-
puts
|
94
|
-
if (tries -= 1)
|
91
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
92
|
+
if (tries -= 1).positive?
|
95
93
|
sleep(5)
|
96
94
|
retry
|
97
95
|
else
|
@@ -1,11 +1,10 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module WebTranslateIt
|
3
2
|
class Translation
|
4
3
|
require 'net/https'
|
5
4
|
require 'multi_json'
|
6
|
-
|
5
|
+
|
7
6
|
attr_accessor :id, :locale, :text, :status, :created_at, :updated_at, :version, :string_id
|
8
|
-
|
7
|
+
|
9
8
|
# Initialize a new WebTranslateIt::Translation
|
10
9
|
#
|
11
10
|
# Implementation Example:
|
@@ -14,23 +13,19 @@ module WebTranslateIt
|
|
14
13
|
#
|
15
14
|
# to instantiate a new Translation without any text.
|
16
15
|
#
|
17
|
-
|
18
|
-
def initialize(params = {})
|
16
|
+
|
17
|
+
def initialize(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity
|
19
18
|
params.stringify_keys!
|
20
|
-
self.id = params[
|
21
|
-
self.locale = params[
|
22
|
-
self.text = params[
|
23
|
-
self.status = params[
|
24
|
-
self.created_at = params[
|
25
|
-
self.updated_at = params[
|
26
|
-
self.version = params[
|
27
|
-
if params[
|
28
|
-
self.string_id = params["string"]["id"]
|
29
|
-
else
|
30
|
-
self.string_id = nil
|
31
|
-
end
|
19
|
+
self.id = params['id'] || nil
|
20
|
+
self.locale = params['locale'] || nil
|
21
|
+
self.text = params['text'] || nil
|
22
|
+
self.status = params['status'] || 'status_unproofread'
|
23
|
+
self.created_at = params['created_at'] || nil
|
24
|
+
self.updated_at = params['updated_at'] || nil
|
25
|
+
self.version = params['version'] || nil
|
26
|
+
self.string_id = (params['string']['id'] if params['string'])
|
32
27
|
end
|
33
|
-
|
28
|
+
|
34
29
|
# Save a WebTranslateIt::Translation
|
35
30
|
#
|
36
31
|
# Implementation Example:
|
@@ -40,35 +35,33 @@ module WebTranslateIt
|
|
40
35
|
# translation.save
|
41
36
|
# end
|
42
37
|
#
|
43
|
-
|
44
|
-
def save
|
38
|
+
|
39
|
+
def save # rubocop:todo Metrics/MethodLength
|
45
40
|
tries ||= 3
|
46
|
-
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/strings/#{
|
41
|
+
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/strings/#{string_id}/locales/#{locale}/translations")
|
47
42
|
WebTranslateIt::Util.add_fields(request)
|
48
|
-
request.body =
|
43
|
+
request.body = to_json
|
49
44
|
begin
|
50
45
|
Util.handle_response(Connection.http_connection.request(request), true, true)
|
51
46
|
rescue Timeout::Error
|
52
|
-
puts
|
53
|
-
if (tries -= 1)
|
47
|
+
puts 'Request timeout. Will retry in 5 seconds.'
|
48
|
+
if (tries -= 1).positive?
|
54
49
|
sleep(5)
|
55
50
|
retry
|
56
|
-
else
|
57
|
-
success = false
|
58
51
|
end
|
59
52
|
end
|
60
53
|
end
|
61
|
-
|
54
|
+
|
62
55
|
def to_hash
|
63
56
|
{
|
64
|
-
|
65
|
-
|
66
|
-
|
57
|
+
'locale' => locale,
|
58
|
+
'text' => text,
|
59
|
+
'status' => status
|
67
60
|
}
|
68
61
|
end
|
69
62
|
|
70
|
-
def to_json
|
71
|
-
MultiJson.dump(
|
63
|
+
def to_json(*_args)
|
64
|
+
MultiJson.dump(to_hash)
|
72
65
|
end
|
73
66
|
end
|
74
67
|
end
|