web_translate_it 3.2.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62888b8fc7d40d82a556bbe69e049b5f8ca713d79208c1a65540c9fa3893bdad
4
- data.tar.gz: e3f9e96f09e669f20d2235314842bfc0b2865cf67816b4bc48d2e369c4b13bdf
3
+ metadata.gz: d9a027a46383d8932b3776d7c614592e3bb186bed328954f0a39bd2e168f2ba3
4
+ data.tar.gz: 75bd8ab7c9129e6fb4f6c3c982fee30b1793a01ce2ba8219aa4e2480cb491246
5
5
  SHA512:
6
- metadata.gz: '099f8ebe73c79ad7a57a56beab8e8a2f1aa5688b57fa84c6020439d11fa2e836047cc22ebfcd33363db8ff751dbbffd9c27fe952a3930786acfda715d8bebdfd'
7
- data.tar.gz: 8f2611c67e384f751d617a863675e66a9c0c4ce0f254516f42a3a7284aa784a9f6938895bb5e3b9d5bb005ccc1831b59b6f8159b5edbd1cb648305f692cae080
6
+ metadata.gz: 20ce007c03f2a00b5eb2c4ba019f37b814ad5184792a1d14a6231351402f0c5988d470a6f64e1b732a2409c74a8f9696b542b3b12aa7ab9c267f0f1c94c5f3ef
7
+ data.tar.gz: 6aafb08ddff729b498093150eddafd55410f45a1f8d0239d7a11a5c1a2a06ccff802bd53996d442ef89da955ba38b290b25aad6e0f8bb7182e15527faec2d418
data/history.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Version 3.2.1 / 2026-03-02
2
+
3
+ * Refactor `Connection` class to eliminate class variables. #254
4
+ * Fix `find_all` to guard against non-2xx API responses. #355
5
+
1
6
  ## Version 3.2.0 / 2026-01-14
2
7
 
3
8
  * Add `wti diff` command.
@@ -8,7 +8,7 @@ module WebTranslateIt
8
8
 
9
9
  attr_accessor :configuration, :global_options, :command_options, :parameters
10
10
 
11
- def initialize(command, command_options, _global_options, parameters, project_path) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/MethodLength
11
+ def initialize(command, command_options, _global_options, parameters, project_path) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/AbcSize
12
12
  self.command_options = command_options
13
13
  self.parameters = parameters
14
14
  unless command == 'init'
@@ -68,9 +68,9 @@ module WebTranslateIt
68
68
  next if file_array.empty?
69
69
 
70
70
  threads << Thread.new(file_array) do |f_array|
71
- WebTranslateIt::Connection.new(configuration.api_key) do |http|
71
+ WebTranslateIt::Connection.new(configuration.api_key) do |conn|
72
72
  f_array.each do |file|
73
- success = file.fetch(http, command_options.force)
73
+ success = file.fetch(conn.http_connection, command_options.force)
74
74
  complete_success = false unless success
75
75
  end
76
76
  end
@@ -111,7 +111,7 @@ module WebTranslateIt
111
111
  complete_success = true
112
112
  $stdout.sync = true
113
113
  before_push_hook
114
- WebTranslateIt::Connection.new(configuration.api_key) do |http|
114
+ WebTranslateIt::Connection.new(configuration.api_key) do |conn|
115
115
  fetch_locales_to_push(configuration).each do |locale|
116
116
  files = if parameters.any?
117
117
  configuration.files.find_all { |file| parameters.include?(file.file_path) }.sort { |a, b| a.file_path <=> b.file_path }
@@ -122,7 +122,7 @@ module WebTranslateIt
122
122
  puts "Couldn't find any local files registered on WebTranslateIt to push."
123
123
  else
124
124
  files.each do |file|
125
- success = file.upload(http, command_options[:merge], command_options.ignore_missing, command_options.label, command_options[:minor], command_options.force)
125
+ success = file.upload(conn.http_connection, command_options[:merge], command_options.ignore_missing, command_options.label, command_options[:minor], command_options.force)
126
126
  complete_success = false unless success
127
127
  end
128
128
  end
@@ -154,21 +154,21 @@ module WebTranslateIt
154
154
  end
155
155
  end
156
156
 
157
- def diff # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
157
+ def diff # rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
158
158
  complete_success = true
159
159
  $stdout.sync = true
160
- WebTranslateIt::Connection.new(configuration.api_key) do |http|
160
+ WebTranslateIt::Connection.new(configuration.api_key) do |conn| # rubocop:todo Metrics/BlockLength
161
161
  files = if parameters.any?
162
- configuration.files.find_all { |file| parameters.include?(file.file_path) }.sort { |a, b| a.file_path <=> b.file_path }
163
- else
164
- configuration.files.find_all { |file| file.locale == configuration.source_locale }.sort { |a, b| a.file_path <=> b.file_path }
165
- end
162
+ configuration.files.find_all { |file| parameters.include?(file.file_path) }.sort { |a, b| a.file_path <=> b.file_path }
163
+ else
164
+ configuration.files.find_all { |file| file.locale == configuration.source_locale }.sort { |a, b| a.file_path <=> b.file_path }
165
+ end
166
166
  if files.empty?
167
167
  puts "Couldn't find any local files registered on WebTranslateIt to diff."
168
168
  else
169
169
  files.each do |file|
170
170
  if File.exist?(file.file_path)
171
- remote_content = file.fetch_remote_content(http)
171
+ remote_content = file.fetch_remote_content(conn.http_connection)
172
172
  if remote_content
173
173
  temp_file = Tempfile.new('wti')
174
174
  temp_file.write(remote_content)
@@ -198,13 +198,13 @@ module WebTranslateIt
198
198
  puts 'Usage: wti add path/to/master_file_1 path/to/master_file_2 ...'
199
199
  exit
200
200
  end
201
- WebTranslateIt::Connection.new(configuration.api_key) do |http|
201
+ WebTranslateIt::Connection.new(configuration.api_key) do |conn|
202
202
  added = configuration.files.find_all { |file| file.locale == configuration.source_locale }.to_set { |file| File.expand_path(file.file_path) }
203
203
  to_add = parameters.reject { |param| added.include?(File.expand_path(param)) }
204
204
  if to_add.any?
205
205
  to_add.each do |param|
206
206
  file = TranslationFile.new(nil, param.gsub(/ /, '\\ '), nil, configuration.api_key)
207
- success = file.create(http)
207
+ success = file.create(conn.http_connection)
208
208
  complete_success = false unless success
209
209
  end
210
210
  else
@@ -222,14 +222,14 @@ module WebTranslateIt
222
222
  puts 'Usage: wti rm path/to/master_file_1 path/to/master_file_2 ...'
223
223
  exit
224
224
  end
225
- WebTranslateIt::Connection.new(configuration.api_key) do |http| # rubocop:todo Metrics/BlockLength
225
+ WebTranslateIt::Connection.new(configuration.api_key) do |conn| # rubocop:todo Metrics/BlockLength
226
226
  parameters.each do |param|
227
227
  next unless Util.ask_yes_no("Are you sure you want to delete the master file #{param}?\nThis will also delete its target files and translations.", false)
228
228
 
229
229
  files = configuration.files.find_all { |file| file.file_path == param }
230
230
  if files.any?
231
231
  files.each do |master_file|
232
- master_file.delete(http)
232
+ master_file.delete(conn.http_connection)
233
233
  # delete files
234
234
  if File.exist?(master_file.file_path)
235
235
  success = File.delete(master_file.file_path)
@@ -265,10 +265,10 @@ module WebTranslateIt
265
265
  end
266
266
  source = parameters[0]
267
267
  destination = parameters[1]
268
- WebTranslateIt::Connection.new(configuration.api_key) do |http|
268
+ WebTranslateIt::Connection.new(configuration.api_key) do |conn|
269
269
  if Util.ask_yes_no("Are you sure you want to move the master file #{source} and its target files?", true)
270
270
  configuration.files.find_all { |file| file.file_path == source }.each do |master_file|
271
- master_file.upload(http, false, false, nil, false, true, true, destination)
271
+ master_file.upload(conn.http_connection, false, false, nil, false, true, true, destination)
272
272
  # move master file
273
273
  if File.exist?(source)
274
274
  success = File.rename(source, destination) if File.exist?(source)
@@ -283,7 +283,7 @@ module WebTranslateIt
283
283
  end
284
284
  configuration.reload
285
285
  configuration.files.find_all { |file| file.master_id == master_file.id }.each do |target_file|
286
- success = target_file.fetch(http)
286
+ success = target_file.fetch(conn.http_connection)
287
287
  complete_success = false unless success
288
288
  end
289
289
  puts StringUtil.success('All done.') if complete_success
@@ -302,8 +302,8 @@ module WebTranslateIt
302
302
  end
303
303
  parameters.each do |param|
304
304
  print StringUtil.success("Adding locale #{param.upcase}... ")
305
- WebTranslateIt::Connection.new(configuration.api_key) do
306
- WebTranslateIt::Project.create_locale(param)
305
+ WebTranslateIt::Connection.new(configuration.api_key) do |conn|
306
+ WebTranslateIt::Project.create_locale(conn, param)
307
307
  end
308
308
  puts 'Done.'
309
309
  end
@@ -320,8 +320,8 @@ module WebTranslateIt
320
320
  next unless Util.ask_yes_no("Are you certain you want to delete the locale #{param.upcase}?\nThis will also delete its files and translations.", false)
321
321
 
322
322
  print StringUtil.success("Deleting locale #{param.upcase}... ")
323
- WebTranslateIt::Connection.new(configuration.api_key) do
324
- WebTranslateIt::Project.delete_locale(param)
323
+ WebTranslateIt::Connection.new(configuration.api_key) do |conn|
324
+ WebTranslateIt::Project.delete_locale(conn, param)
325
325
  end
326
326
  puts 'Done.'
327
327
  end
@@ -4,36 +4,36 @@ module WebTranslateIt
4
4
 
5
5
  class Connection
6
6
 
7
- @@api_key = nil
8
- @@http_connection = nil
9
- @@debug = false
7
+ @debug = false
8
+
9
+ class << self
10
+
11
+ attr_reader :debug
12
+
13
+ end
14
+
15
+ attr_reader :api_key, :http_connection
10
16
 
11
17
  #
12
18
  # Initialize and yield a HTTPS Keep-Alive connection to WebTranslateIt.com
13
19
  #
14
20
  # Usage:
15
21
  #
16
- # WebTranslateIt::Connection.new(api_key) do
17
- # # do something with Connection.api_key and Connection.http_connection
18
- # end
19
- #
20
- # Or:
21
- #
22
- # WebTranslateIt::Connection.new(api_key) do |http_connection|
23
- # http_connection.request(request)
22
+ # WebTranslateIt::Connection.new(api_key) do |connection|
23
+ # connection.http_connection.request(request)
24
24
  # end
25
25
  #
26
26
  def initialize(api_key) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
27
- @@api_key = api_key
27
+ @api_key = api_key
28
28
  proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : Struct.new(:host, :port, :user, :password).new
29
29
  http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
30
30
  http.use_ssl = true
31
31
  http.open_timeout = http.read_timeout = 60
32
- http.set_debug_output($stderr) if @@debug
32
+ http.set_debug_output($stderr) if self.class.debug
33
33
  begin
34
34
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
35
- @@http_connection = http.start
36
- yield @@http_connection if block_given?
35
+ @http_connection = http.start
36
+ yield self if block_given?
37
37
  rescue OpenSSL::SSL::SSLError
38
38
  puts 'Error: Unable to verify SSL certificate.'
39
39
  exit 1
@@ -43,15 +43,7 @@ module WebTranslateIt
43
43
  end
44
44
 
45
45
  def self.turn_debug_on
46
- @@debug = true
47
- end
48
-
49
- def self.api_key
50
- @@api_key
51
- end
52
-
53
- def self.http_connection
54
- @@http_connection
46
+ @debug = true
55
47
  end
56
48
 
57
49
  end
@@ -8,10 +8,10 @@ module WebTranslateIt
8
8
  success = true
9
9
  tries ||= 3
10
10
  begin
11
- WebTranslateIt::Connection.new(api_key) do |http|
11
+ WebTranslateIt::Connection.new(api_key) do |conn|
12
12
  request = Net::HTTP::Get.new("/api/projects/#{api_key}")
13
13
  WebTranslateIt::Util.add_fields(request)
14
- response = http.request(request)
14
+ response = conn.http_connection.request(request)
15
15
  return response.body if response.is_a?(Net::HTTPSuccess)
16
16
 
17
17
  puts 'An error occured while fetching the project information:'
@@ -37,10 +37,10 @@ module WebTranslateIt
37
37
  success = true
38
38
  tries ||= 3
39
39
  begin
40
- WebTranslateIt::Connection.new(api_key) do |http|
40
+ WebTranslateIt::Connection.new(api_key) do |conn|
41
41
  request = Net::HTTP::Get.new(url)
42
42
  WebTranslateIt::Util.add_fields(request)
43
- return Util.handle_response(http.request(request), true)
43
+ return Util.handle_response(conn.http_connection.request(request), true)
44
44
  end
45
45
  rescue Timeout::Error
46
46
  puts 'Request timeout. Will retry in 5 seconds.'
@@ -54,14 +54,14 @@ module WebTranslateIt
54
54
  success
55
55
  end
56
56
 
57
- def self.create_locale(locale_code) # rubocop:todo Metrics/MethodLength
57
+ def self.create_locale(connection, locale_code) # rubocop:todo Metrics/MethodLength
58
58
  success = true
59
59
  tries ||= 3
60
60
  begin
61
- request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/locales")
61
+ request = Net::HTTP::Post.new("/api/projects/#{connection.api_key}/locales")
62
62
  WebTranslateIt::Util.add_fields(request)
63
63
  request.set_form_data({'id' => locale_code}, ';')
64
- Util.handle_response(Connection.http_connection.request(request), true)
64
+ Util.handle_response(connection.http_connection.request(request), true)
65
65
  rescue Timeout::Error
66
66
  puts 'Request timeout. Will retry in 5 seconds.'
67
67
  if (tries -= 1).positive?
@@ -74,13 +74,13 @@ module WebTranslateIt
74
74
  success
75
75
  end
76
76
 
77
- def self.delete_locale(locale_code) # rubocop:todo Metrics/MethodLength
77
+ def self.delete_locale(connection, locale_code) # rubocop:todo Metrics/MethodLength
78
78
  success = true
79
79
  tries ||= 3
80
80
  begin
81
- request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/locales/#{locale_code}")
81
+ request = Net::HTTP::Delete.new("/api/projects/#{connection.api_key}/locales/#{locale_code}")
82
82
  WebTranslateIt::Util.add_fields(request)
83
- Util.handle_response(Connection.http_connection.request(request), true)
83
+ Util.handle_response(connection.http_connection.request(request), true)
84
84
  rescue Timeout::Error
85
85
  puts 'Request timeout. Will retry in 5 seconds.'
86
86
  if (tries -= 1).positive?
@@ -5,7 +5,7 @@ module WebTranslateIt
5
5
  class String # rubocop:todo Metrics/ClassLength
6
6
 
7
7
  attr_accessor :id, :key, :plural, :type, :dev_comment, :word_count, :status, :category, :labels, :file,
8
- :created_at, :updated_at, :translations, :new_record
8
+ :created_at, :updated_at, :translations, :new_record, :connection
9
9
 
10
10
  # Initialize a new WebTranslateIt::String
11
11
  #
@@ -21,8 +21,9 @@ module WebTranslateIt
21
21
  #
22
22
  # to instantiate a new String with a source and target translation.
23
23
 
24
- def initialize(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
24
+ def initialize(params = {}, connection: nil) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
25
25
  params.stringify_keys!
26
+ self.connection = connection
26
27
  self.id = params['id'] || nil
27
28
  self.key = params['key'] || nil
28
29
  self.plural = params['plural'] || nil
@@ -51,11 +52,11 @@ module WebTranslateIt
51
52
  #
52
53
  # to find and instantiate an array of String which key is like `product_name_123`.
53
54
 
54
- def self.find_all(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
55
+ def self.find_all(connection, params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
55
56
  success = true
56
57
  tries ||= 3
57
58
  params.stringify_keys!
58
- url = "/api/projects/#{Connection.api_key}/strings"
59
+ url = "/api/projects/#{connection.api_key}/strings"
59
60
  url += "?#{HashUtil.to_params('filters' => params)}" unless params.empty?
60
61
 
61
62
  request = Net::HTTP::Get.new(url)
@@ -63,9 +64,11 @@ module WebTranslateIt
63
64
  begin
64
65
  strings = []
65
66
  while request
66
- response = Connection.http_connection.request(request)
67
+ response = connection.http_connection.request(request)
68
+ return [] unless response.code.to_i < 400
69
+
67
70
  JSON.parse(response.body).each do |string_response|
68
- string = WebTranslateIt::String.new(string_response)
71
+ string = WebTranslateIt::String.new(string_response, connection: connection)
69
72
  string.new_record = false
70
73
  strings.push(string)
71
74
  end
@@ -104,16 +107,16 @@ module WebTranslateIt
104
107
  # to find and instantiate the String which ID is `1234`.
105
108
  #
106
109
 
107
- def self.find(id) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
110
+ def self.find(connection, id) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
108
111
  success = true
109
112
  tries ||= 3
110
- request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}")
113
+ request = Net::HTTP::Get.new("/api/projects/#{connection.api_key}/strings/#{id}")
111
114
  WebTranslateIt::Util.add_fields(request)
112
115
  begin
113
- response = Connection.http_connection.request(request)
116
+ response = connection.http_connection.request(request)
114
117
  return nil if response.code.to_i == 404
115
118
 
116
- string = WebTranslateIt::String.new(JSON.parse(response.body))
119
+ string = WebTranslateIt::String.new(JSON.parse(response.body), connection: connection)
117
120
  string.new_record = false
118
121
  return string
119
122
  rescue Timeout::Error
@@ -156,10 +159,10 @@ module WebTranslateIt
156
159
  def delete # rubocop:todo Metrics/MethodLength
157
160
  success = true
158
161
  tries ||= 3
159
- request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/strings/#{id}")
162
+ request = Net::HTTP::Delete.new("/api/projects/#{connection.api_key}/strings/#{id}")
160
163
  WebTranslateIt::Util.add_fields(request)
161
164
  begin
162
- Util.handle_response(Connection.http_connection.request(request), true, true)
165
+ Util.handle_response(connection.http_connection.request(request), true, true)
163
166
  rescue Timeout::Error
164
167
  puts 'Request timeout. Will retry in 5 seconds.'
165
168
  if (tries -= 1).positive?
@@ -189,14 +192,15 @@ module WebTranslateIt
189
192
  return translation if translation
190
193
  return nil if new_record
191
194
 
192
- request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}/locales/#{locale}/translations")
195
+ request = Net::HTTP::Get.new("/api/projects/#{connection.api_key}/strings/#{id}/locales/#{locale}/translations")
193
196
  WebTranslateIt::Util.add_fields(request)
194
197
  begin
195
- response = Util.handle_response(Connection.http_connection.request(request), true, true)
198
+ response = Util.handle_response(connection.http_connection.request(request), true, true)
196
199
  hash = JSON.parse(response)
197
200
  return nil if hash.empty?
198
201
 
199
202
  translation = WebTranslateIt::Translation.new(hash)
203
+ translation.connection = connection
200
204
  return translation
201
205
  rescue Timeout::Error
202
206
  puts 'Request timeout. Will retry in 5 seconds.'
@@ -218,17 +222,18 @@ module WebTranslateIt
218
222
  def update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
219
223
  success = true
220
224
  tries ||= 3
221
- request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/strings/#{id}")
225
+ request = Net::HTTP::Put.new("/api/projects/#{connection.api_key}/strings/#{id}")
222
226
  WebTranslateIt::Util.add_fields(request)
223
227
  request.body = to_json
224
228
 
225
229
  translations.each do |translation|
226
230
  translation.string_id = id
231
+ translation.connection = connection
227
232
  translation.save
228
233
  end
229
234
 
230
235
  begin
231
- Util.handle_response(Connection.http_connection.request(request), true, true)
236
+ Util.handle_response(connection.http_connection.request(request), true, true)
232
237
  rescue Timeout::Error
233
238
  puts 'Request timeout. Will retry in 5 seconds.'
234
239
  if (tries -= 1).positive?
@@ -247,11 +252,11 @@ module WebTranslateIt
247
252
  def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
248
253
  success = true
249
254
  tries ||= 3
250
- request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/strings")
255
+ request = Net::HTTP::Post.new("/api/projects/#{connection.api_key}/strings")
251
256
  WebTranslateIt::Util.add_fields(request)
252
257
  request.body = to_json(true)
253
258
  begin
254
- response = JSON.parse(Util.handle_response(Connection.http_connection.request(request), true, true))
259
+ response = JSON.parse(Util.handle_response(connection.http_connection.request(request), true, true))
255
260
  self.id = response['id']
256
261
  self.new_record = false
257
262
  return true
@@ -4,7 +4,7 @@ module WebTranslateIt
4
4
 
5
5
  class Term # rubocop:todo Metrics/ClassLength
6
6
 
7
- attr_accessor :id, :text, :description, :created_at, :updated_at, :translations, :new_record
7
+ attr_accessor :id, :text, :description, :created_at, :updated_at, :translations, :new_record, :connection
8
8
 
9
9
  # Initialize a new WebTranslateIt::Term
10
10
  #
@@ -20,8 +20,9 @@ module WebTranslateIt
20
20
  #
21
21
  # to instantiate a new Term with a Term Translations in Spanish and French.
22
22
 
23
- def initialize(params = {})
23
+ def initialize(params = {}, connection: nil) # rubocop:todo Metrics/AbcSize
24
24
  params.stringify_keys!
25
+ self.connection = connection
25
26
  self.id = params['id'] || nil
26
27
  self.text = params['text'] || nil
27
28
  self.description = params['description'] || nil
@@ -41,11 +42,11 @@ module WebTranslateIt
41
42
  #
42
43
  # puts terms.inspect #=> An array of WebTranslateIt::Term objects
43
44
 
44
- def self.find_all(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
45
+ def self.find_all(connection, params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
45
46
  success = true
46
47
  tries ||= 3
47
48
  params.stringify_keys!
48
- url = "/api/projects/#{Connection.api_key}/terms"
49
+ url = "/api/projects/#{connection.api_key}/terms"
49
50
  url += "?#{HashUtil.to_params(params)}" unless params.empty?
50
51
 
51
52
  request = Net::HTTP::Get.new(url)
@@ -53,9 +54,11 @@ module WebTranslateIt
53
54
  begin
54
55
  terms = []
55
56
  while request
56
- response = Connection.http_connection.request(request)
57
+ response = connection.http_connection.request(request)
58
+ return [] unless response.code.to_i < 400
59
+
57
60
  JSON.parse(response.body).each do |term_response|
58
- term = WebTranslateIt::Term.new(term_response)
61
+ term = WebTranslateIt::Term.new(term_response, connection: connection)
59
62
  term.new_record = false
60
63
  terms.push(term)
61
64
  end
@@ -94,16 +97,16 @@ module WebTranslateIt
94
97
  # to find and instantiate the Term which ID is `1234`.
95
98
  #
96
99
 
97
- def self.find(term_id) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
100
+ def self.find(connection, term_id) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
98
101
  success = true
99
102
  tries ||= 3
100
- request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/terms/#{term_id}")
103
+ request = Net::HTTP::Get.new("/api/projects/#{connection.api_key}/terms/#{term_id}")
101
104
  WebTranslateIt::Util.add_fields(request)
102
105
  begin
103
- response = Connection.http_connection.request(request)
106
+ response = connection.http_connection.request(request)
104
107
  return nil if response.code.to_i == 404
105
108
 
106
- term = WebTranslateIt::Term.new(JSON.parse(response.body))
109
+ term = WebTranslateIt::Term.new(JSON.parse(response.body), connection: connection)
107
110
  term.new_record = false
108
111
  return term
109
112
  rescue Timeout::Error
@@ -146,10 +149,10 @@ module WebTranslateIt
146
149
  def delete # rubocop:todo Metrics/MethodLength
147
150
  success = true
148
151
  tries ||= 3
149
- request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/terms/#{id}")
152
+ request = Net::HTTP::Delete.new("/api/projects/#{connection.api_key}/terms/#{id}")
150
153
  WebTranslateIt::Util.add_fields(request)
151
154
  begin
152
- Util.handle_response(Connection.http_connection.request(request), true, true)
155
+ Util.handle_response(connection.http_connection.request(request), true, true)
153
156
  rescue Timeout::Error
154
157
  puts 'Request timeout. Will retry in 5 seconds.'
155
158
  if (tries -= 1).positive?
@@ -179,10 +182,10 @@ module WebTranslateIt
179
182
  return translation if translation
180
183
  return nil if new_record
181
184
 
182
- request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/terms/#{id}/locales/#{locale}/translations")
185
+ request = Net::HTTP::Get.new("/api/projects/#{connection.api_key}/terms/#{id}/locales/#{locale}/translations")
183
186
  WebTranslateIt::Util.add_fields(request)
184
187
  begin
185
- response = Util.handle_response(Connection.http_connection.request(request), true, true)
188
+ response = Util.handle_response(connection.http_connection.request(request), true, true)
186
189
  array = JSON.parse(response)
187
190
  return nil if array.empty?
188
191
 
@@ -209,17 +212,18 @@ module WebTranslateIt
209
212
  def update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
210
213
  success = true
211
214
  tries ||= 3
212
- request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{id}")
215
+ request = Net::HTTP::Put.new("/api/projects/#{connection.api_key}/terms/#{id}")
213
216
  WebTranslateIt::Util.add_fields(request)
214
217
  request.body = to_json
215
218
 
216
219
  translations.each do |translation|
217
220
  translation.term_id = id
221
+ translation.connection = connection
218
222
  translation.save
219
223
  end
220
224
 
221
225
  begin
222
- Util.handle_response(Connection.http_connection.request(request), true, true)
226
+ Util.handle_response(connection.http_connection.request(request), true, true)
223
227
  rescue Timeout::Error
224
228
  puts 'Request timeout. Will retry in 5 seconds.'
225
229
  if (tries -= 1).positive?
@@ -235,12 +239,12 @@ module WebTranslateIt
235
239
  def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
236
240
  success = true
237
241
  tries ||= 3
238
- request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/terms")
242
+ request = Net::HTTP::Post.new("/api/projects/#{connection.api_key}/terms")
239
243
  WebTranslateIt::Util.add_fields(request)
240
244
  request.body = to_json(true)
241
245
 
242
246
  begin
243
- response = JSON.parse(Util.handle_response(Connection.http_connection.request(request), true, true))
247
+ response = JSON.parse(Util.handle_response(connection.http_connection.request(request), true, true))
244
248
  self.id = response['id']
245
249
  self.new_record = false
246
250
  return true
@@ -4,7 +4,7 @@ module WebTranslateIt
4
4
 
5
5
  class TermTranslation
6
6
 
7
- attr_accessor :id, :locale, :text, :description, :status, :new_record, :term_id
7
+ attr_accessor :id, :locale, :text, :description, :status, :new_record, :term_id, :connection
8
8
 
9
9
  # Initialize a new WebTranslateIt::TermTranslation
10
10
  #
@@ -59,12 +59,12 @@ module WebTranslateIt
59
59
  def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
60
60
  success = true
61
61
  tries ||= 3
62
- request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/terms/#{term_id}/locales/#{locale}/translations")
62
+ request = Net::HTTP::Post.new("/api/projects/#{connection.api_key}/terms/#{term_id}/locales/#{locale}/translations")
63
63
  WebTranslateIt::Util.add_fields(request)
64
64
  request.body = to_json
65
65
 
66
66
  begin
67
- response = JSON.parse(Util.handle_response(Connection.http_connection.request(request), true, true))
67
+ response = JSON.parse(Util.handle_response(connection.http_connection.request(request), true, true))
68
68
  self.id = response['id']
69
69
  self.new_record = false
70
70
  return true
@@ -80,14 +80,14 @@ module WebTranslateIt
80
80
  success
81
81
  end
82
82
 
83
- def update # rubocop:todo Metrics/MethodLength
83
+ def update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
84
84
  success = true
85
85
  tries ||= 3
86
- request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{id}/locales/#{locale}/translations/#{id}")
86
+ request = Net::HTTP::Put.new("/api/projects/#{connection.api_key}/terms/#{id}/locales/#{locale}/translations/#{id}")
87
87
  WebTranslateIt::Util.add_fields(request)
88
88
  request.body = to_json
89
89
  begin
90
- Util.handle_response(Connection.http_connection.request(request), true, true)
90
+ Util.handle_response(connection.http_connection.request(request), true, true)
91
91
  rescue Timeout::Error
92
92
  puts 'Request timeout. Will retry in 5 seconds.'
93
93
  if (tries -= 1).positive?
@@ -4,7 +4,7 @@ module WebTranslateIt
4
4
 
5
5
  class Translation
6
6
 
7
- attr_accessor :id, :locale, :text, :status, :created_at, :updated_at, :version, :string_id
7
+ attr_accessor :id, :locale, :text, :status, :created_at, :updated_at, :version, :string_id, :connection
8
8
 
9
9
  # Initialize a new WebTranslateIt::Translation
10
10
  #
@@ -39,11 +39,11 @@ module WebTranslateIt
39
39
 
40
40
  def save # rubocop:todo Metrics/MethodLength
41
41
  tries ||= 3
42
- request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/strings/#{string_id}/locales/#{locale}/translations")
42
+ request = Net::HTTP::Post.new("/api/projects/#{connection.api_key}/strings/#{string_id}/locales/#{locale}/translations")
43
43
  WebTranslateIt::Util.add_fields(request)
44
44
  request.body = to_json
45
45
  begin
46
- Util.handle_response(Connection.http_connection.request(request), true, true)
46
+ Util.handle_response(connection.http_connection.request(request), true, true)
47
47
  rescue Timeout::Error
48
48
  puts 'Request timeout. Will retry in 5 seconds.'
49
49
  if (tries -= 1).positive?
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+
5
+ # Destructively convert all keys to strings.
6
+ def stringify_keys!
7
+ keys.each do |key|
8
+ self[key.to_s] = delete(key)
9
+ end
10
+ self
11
+ end
12
+
13
+ end
@@ -3,7 +3,7 @@
3
3
  class HashUtil
4
4
 
5
5
  def self.to_params(hash) # rubocop:todo Metrics/MethodLength
6
- params = ''
6
+ params = +''
7
7
  stack = []
8
8
 
9
9
  hash.each do |k, v|
@@ -30,14 +30,4 @@ class HashUtil
30
30
 
31
31
  end
32
32
 
33
- class Hash
34
-
35
- # Destructively convert all keys to strings.
36
- def stringify_keys!
37
- keys.each do |key|
38
- self[key.to_s] = delete(key)
39
- end
40
- self
41
- end
42
-
43
- end
33
+ require_relative 'hash_extensions'
@@ -34,9 +34,9 @@ module WebTranslateIt
34
34
  return if config.ignore_locales.include?(locale)
35
35
 
36
36
  config.logger&.debug { " Fetching #{locale} language file(s) from WebTranslateIt" }
37
- WebTranslateIt::Connection.new(config.api_key) do |http|
37
+ WebTranslateIt::Connection.new(config.api_key) do |conn|
38
38
  config.files.find_all { |file| file.locale.in?([locale, I18n.locale]) }.each do |file|
39
- file.fetch(http)
39
+ file.fetch(conn.http_connection)
40
40
  end
41
41
  end
42
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_translate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edouard Briere
@@ -61,6 +61,7 @@ files:
61
61
  - lib/web_translate_it/translation.rb
62
62
  - lib/web_translate_it/translation_file.rb
63
63
  - lib/web_translate_it/util.rb
64
+ - lib/web_translate_it/util/hash_extensions.rb
64
65
  - lib/web_translate_it/util/hash_util.rb
65
66
  - lib/web_translate_it/util/string_util.rb
66
67
  - license