Skip to content

Commit

Permalink
Version 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis committed Mar 11, 2022
1 parent 1706990 commit e5d6edc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 29 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## 3.0.0 (unreleased)
## 3.0.0 (11-Mar-22)

* Use ruby-lokalise-api v6
* **Breaking change**: Require Ruby 2.7 or above
* **Breaking change (potentially)**: Use ruby-lokalise-api v6. In general, this transition should not affect you if you employ `export!` and `import!` methods only. However, please be aware that ruby-lokalise-api has a few breaking changes [listed in its own changelog](https://lokalise.github.io/ruby-lokalise-api/additional_info/changelog)
* Use Zeitwerk loader
* Prettify and update source code

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ The uploading process is multi-threaded.
`processes` will contain an array of objects responding to the following methods:

* `#success` — usually returns `true` (to learn more, check documentation for the `:raise_on_export_fail` option below)
* `#process` — returns an object (an instance of the `Lokalise::Resources::QueuedProcess`) representing a [queued background process](https://lokalise.github.io/ruby-lokalise-api/api/queued-processes) as uploading is done in the background on Lokalise.
* `#process` — returns an object (an instance of the `RubyLokaliseApi::Resources::QueuedProcess`) representing a [queued background process](https://lokalise.github.io/ruby-lokalise-api/api/queued-processes) as uploading is done in the background on Lokalise.
* `#path` — returns an instance of the `Pathname` class which represent the file being uploaded.

You can perform periodic checks to read the status of the process. Here's a very simple example:
Expand Down Expand Up @@ -139,7 +139,7 @@ importer = LokaliseManager.importer api_token: '1234abc',
```
* `import_safe_mode` (`boolean`) — default to `false`. When this option is enabled, the import task will check whether the directory set with `locales_path` is empty or not. If it is not empty, you will be prompted to continue.
* `max_retries_import` (`integer`) — this option is introduced to properly handle Lokalise API rate limiting. If the HTTP status code 429 (too many requests) has been received, this gem will apply an exponential backoff mechanism with a very simple formula: `2 ** retries`. If the maximum number of retries has been reached, a `Lokalise::Error::TooManyRequests` exception will be raised and the operation will be halted.
* `max_retries_import` (`integer`) — this option is introduced to properly handle Lokalise API rate limiting. If the HTTP status code 429 (too many requests) has been received, this gem will apply an exponential backoff mechanism with a very simple formula: `2 ** retries`. If the maximum number of retries has been reached, a `RubyLokaliseApi::Error::TooManyRequests` exception will be raised and the operation will be halted.
### Export config
Expand Down Expand Up @@ -171,7 +171,7 @@ In this case the `export_opts` will have `detect_icu_plurals` set to `true` and
c.skip_file_export = ->(file) { f.split[1].to_s.include?('fr') }
```
* `max_retries_export` (`integer`) — this option is introduced to properly handle Lokalise API rate limiting. If the HTTP status code 429 (too many requests) has been received, LokaliseManager will apply an exponential backoff mechanism with a very simple formula: `2 ** retries` (initially `retries` is `0`). If the maximum number of retries has been reached, a `Lokalise::Error::TooManyRequests` exception will be raised and the export operation will be halted. By default, LokaliseManager will make up to `5` retries which potentially means `1 + 2 + 4 + 8 + 16 + 32 = 63` seconds of waiting time. If the `max_retries_export` is less than `1`, LokaliseManager will not perform any retries and give up immediately after receiving error 429.
* `max_retries_export` (`integer`) — this option is introduced to properly handle Lokalise API rate limiting. If the HTTP status code 429 (too many requests) has been received, LokaliseManager will apply an exponential backoff mechanism with a very simple formula: `2 ** retries` (initially `retries` is `0`). If the maximum number of retries has been reached, a `RubyLokaliseApi::Error::TooManyRequests` exception will be raised and the export operation will be halted. By default, LokaliseManager will make up to `5` retries which potentially means `1 + 2 + 4 + 8 + 16 + 32 = 63` seconds of waiting time. If the `max_retries_export` is less than `1`, LokaliseManager will not perform any retries and give up immediately after receiving error 429.
* `raise_on_export_fail` (`boolean`) — default is `true`. When this option is enabled, LokaliseManager will re-raise any exceptions that happened during the file uploading. In other words, if any uploading thread raised an exception, your exporting process will exit with an exception. Suppose, you are uploading 12 translation files; these files will be split in 2 groups with 6 files each, and each group will be uploaded in parallel (using threads). However, suppose some exception happens when uploading the first group. By default this exception will be re-raised for the whole process and the script will never try to upload the second group. If you would like to continue uploading even if an exception happened, set the `raise_on_export_fail` to `false`. In this case the `export!` method will return an array with scheduled processes and with information about processes that were not successfully scheduled. This information is represented as an object with three methods: `path` (contains an instance of the `Pathname` class which says which file could not be uploaded), `error` (the actual exception), and `success` (returns `false`). So, you can use the following snippet to check your processes:
```ruby
Expand Down
12 changes: 6 additions & 6 deletions lib/lokalise_manager/task_definitions/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'ruby-lokalise-api'
require 'ruby_lokalise_api'
require 'pathname'

module LokaliseManager
Expand Down Expand Up @@ -34,18 +34,18 @@ def initialize(custom_opts = {}, global_config = LokaliseManager::GlobalConfig)

# Creates a Lokalise API client
#
# @return [Lokalise::Client]
# @return [RubyLokaliseApi::Client]
def api_client
client_opts = [config.api_token, config.timeouts]
client_method = config.use_oauth2_token ? :oauth2_client : :client

@api_client = ::Lokalise.send(client_method, *client_opts)
@api_client = ::RubyLokaliseApi.send(client_method, *client_opts)
end

# Resets API client
def reset_api_client!
::Lokalise.reset_client!
::Lokalise.reset_oauth2_client!
::RubyLokaliseApi.reset_client!
::RubyLokaliseApi.reset_oauth2_client!
@api_client = nil
end

Expand Down Expand Up @@ -95,7 +95,7 @@ def with_exp_backoff(max_retries)
retries = 0
begin
yield
rescue Lokalise::Error::TooManyRequests => e
rescue RubyLokaliseApi::Error::TooManyRequests => e
raise(e.class, "Gave up after #{retries} retries") if retries >= max_retries

sleep 2**retries
Expand Down
2 changes: 1 addition & 1 deletion lokalise_manager.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
spec.extra_rdoc_files = ['README.md']
spec.require_paths = ['lib']

spec.add_dependency 'ruby-lokalise-api', '~> 5.0'
spec.add_dependency 'ruby-lokalise-api', '~> 6.0'
spec.add_dependency 'rubyzip', '~> 2.3'
spec.add_dependency 'zeitwerk', '~> 2.4'

Expand Down
10 changes: 5 additions & 5 deletions spec/lib/lokalise_manager/task_definitions/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
end

specify '.reset_client!' do
expect(described_object.api_client).to be_an_instance_of(Lokalise::Client)
expect(described_object.api_client).to be_an_instance_of(RubyLokaliseApi::Client)
described_object.reset_api_client!
current_client = described_object.instance_variable_get :@api_client
expect(current_client).to be_nil
Expand Down Expand Up @@ -99,8 +99,8 @@
})

client = described_object.api_client
expect(client).to be_an_instance_of(Lokalise::Client)
expect(client).not_to be_an_instance_of(Lokalise::OAuth2Client)
expect(client).to be_an_instance_of(RubyLokaliseApi::Client)
expect(client).not_to be_an_instance_of(RubyLokaliseApi::OAuth2Client)
expect(client.open_timeout).to eq(100)
expect(client.timeout).to eq(500)
end
Expand All @@ -110,8 +110,8 @@

client = described_object.api_client

expect(client).to be_an_instance_of(Lokalise::OAuth2Client)
expect(client).not_to be_an_instance_of(Lokalise::Client)
expect(client).to be_an_instance_of(RubyLokaliseApi::OAuth2Client)
expect(client).not_to be_an_instance_of(RubyLokaliseApi::Client)
end
end
end
16 changes: 8 additions & 8 deletions spec/lib/lokalise_manager/task_definitions/exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
allow(described_object.config).to receive(:raise_on_export_fail).and_return(false)
allow(described_object).to receive(:sleep).and_return(0)

fake_client = instance_double('Lokalise::Client')
fake_client = instance_double('RubyLokaliseApi::Client')
allow(fake_client).to receive(:token).with(any_args).and_return('fake_token')
allow(fake_client).to receive(:upload_file).with(any_args).and_raise(Lokalise::Error::TooManyRequests)
allow(fake_client).to receive(:upload_file).with(any_args).and_raise(RubyLokaliseApi::Error::TooManyRequests)
allow(described_object).to receive(:api_client).and_return(fake_client)
processes = []
expect { processes = described_object.export! }.not_to raise_error

expect(processes[0].success).to be false
expect(processes[1].error.class).to eq(Lokalise::Error::TooManyRequests)
expect(processes[1].error.class).to eq(RubyLokaliseApi::Error::TooManyRequests)
expect(processes.count).to eq(7)

expect(described_object).to have_received(:sleep).exactly(7).times
Expand All @@ -70,14 +70,14 @@
allow(described_object.config).to receive(:max_retries_export).and_return(1)
allow(described_object).to receive(:sleep).and_return(0)

fake_client = instance_double('Lokalise::Client')
fake_client = instance_double('RubyLokaliseApi::Client')
allow(fake_client).to receive(:token).with(any_args).and_return('fake_token')
allow(fake_client).to receive(:upload_file).with(any_args).and_raise(Lokalise::Error::TooManyRequests)
allow(fake_client).to receive(:upload_file).with(any_args).and_raise(RubyLokaliseApi::Error::TooManyRequests)
allow(described_object).to receive(:api_client).and_return(fake_client)

expect do
described_object.export!
end.to raise_error(Lokalise::Error::TooManyRequests, /Gave up after 1 retries/i)
end.to raise_error(RubyLokaliseApi::Error::TooManyRequests, /Gave up after 1 retries/i)

expect(described_object).to have_received(:sleep).exactly(2).times
expect(described_object).to have_received(:api_client).at_least(4).times
Expand Down Expand Up @@ -147,7 +147,7 @@
expect(process_data.path.to_s).to include('en.yml')

process = process_data.process
expect(process).to be_an_instance_of(Lokalise::Resources::QueuedProcess)
expect(process).to be_an_instance_of(RubyLokaliseApi::Resources::QueuedProcess)
expect(process.project_id).to eq(project_id)
expect(process.status).to eq('queued')
end
Expand Down Expand Up @@ -225,7 +225,7 @@
allow_project_id described_object, '542886116159f798720dc4.94769464'

VCR.use_cassette('upload_files_error') do
expect { described_object.export! }.to raise_error(Lokalise::Error::BadRequest, /Unknown `lang_iso`/)
expect { described_object.export! }.to raise_error(RubyLokaliseApi::Error::BadRequest, /Unknown `lang_iso`/)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/lokalise_manager/task_definitions/importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

VCR.use_cassette('download_files_error') do
expect { described_object.send :download_files }.
to raise_error(Lokalise::Error::BadRequest, /Invalid `project_id` parameter/)
to raise_error(RubyLokaliseApi::Error::BadRequest, /Invalid `project_id` parameter/)
end
end
end
Expand All @@ -52,11 +52,11 @@
it 'handles too many requests' do
allow(described_object).to receive(:sleep).and_return(0)

fake_client = instance_double('Lokalise::Client')
allow(fake_client).to receive(:download_files).and_raise(Lokalise::Error::TooManyRequests)
fake_client = instance_double('RubyLokaliseApi::Client')
allow(fake_client).to receive(:download_files).and_raise(RubyLokaliseApi::Error::TooManyRequests)
allow(described_object).to receive(:api_client).and_return(fake_client)

expect { described_object.import! }.to raise_error(Lokalise::Error::TooManyRequests, /Gave up after 2 retries/i)
expect { described_object.import! }.to raise_error(RubyLokaliseApi::Error::TooManyRequests, /Gave up after 2 retries/i)

expect(described_object).to have_received(:sleep).exactly(2).times
expect(described_object).to have_received(:api_client).exactly(3).times
Expand Down

0 comments on commit e5d6edc

Please sign in to comment.