ruby-lokalise-api 4.0.0 → 4.3.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: b871674214b37d9a6b0621c0e237d393755ecb14dbdf190fb6689decb0798b6f
4
- data.tar.gz: 2422640be2390f293d46b642097c4f9e5b0848f7720504acb6e0618910fd5188
3
+ metadata.gz: 4147ca27a676c98d74fa654c4244212aa7e296855d4bda492fd061a3a3a02347
4
+ data.tar.gz: 59c042db99a7f2899baef674319ec18ab9ee193f3581f35b40fd92b0c43118bb
5
5
  SHA512:
6
- metadata.gz: 2850e23fb85b2512095416d5decdb7fb6462ca6837d7b3827ae2e4d0c91aef2632f720c23ca7b11af4d66676597e7290af3cf4a2a65055ac2ac2cf61ca453827
7
- data.tar.gz: 03c7adf6eddd058429c3ebffadc455d8748610d7a5afa7d3c5c54b6d249696b2ad2c040b14f464bfeb89823d75b64a375e26f821ac0451649c17bd47cf4ccd4c
6
+ metadata.gz: 38e44afa51cb74149fb4b7aff68d958232f39d111bef8927835fc9bd012c8738ac0a4a4c175ab1bc9e7b2a846135a5f4ff5473845d562d45d408200e439abedf
7
+ data.tar.gz: 67cc5c58134b64955f65595f5b5797c72adbaa8dbe9e239fff0d434044e3a599c0eea4dd7418bbbb2834b9cd8f9e7572258f07a514f80c048508cfba408cd2d0
@@ -4,7 +4,7 @@
4
4
  2. [Create a topic branch.][branch]
5
5
  3. Implement your feature or bug fix.
6
6
  4. Don't forget to add specs and make sure they pass by running `rspec .`.
7
- 5. Make sure your code complies with the style guide by running `rubocop`. `rubocop -a` can automatically fix most issues for you.
7
+ 5. Make sure your code complies with the style guide by running `rubocop`. `rubocop -a` can automatically fix most issues for you. Run `rubocop -A` to make it more aggressive.
8
8
  6. If necessary, add documentation for your feature or bug fix.
9
9
  7. Commit and push your changes.
10
10
  8. [Submit a pull request.][pr]
data/README.md CHANGED
@@ -47,4 +47,4 @@ Detailed documentation can be found at [lokalise.github.io/ruby-lokalise-api](ht
47
47
 
48
48
  This gem is licensed under the [BSD 3 Clause license](https://github.com/lokalise/ruby-lokalise-api/blob/master/LICENSE). Prior to version 4 the license type was MIT.
49
49
 
50
- Copyright (c) [Lokalise team](http://lokalise.co), [Ilya Bodrov](http://bodrovis.tech)
50
+ Copyright (c) [Lokalise team](http://lokalise.co) and [Ilya Bodrov](http://bodrovis.tech)
@@ -24,12 +24,13 @@ require 'ruby-lokalise-api/rest/webhooks'
24
24
  module Lokalise
25
25
  class Client
26
26
  attr_reader :token
27
- attr_accessor :timeout, :open_timeout
27
+ attr_accessor :timeout, :open_timeout, :enable_compression
28
28
 
29
29
  def initialize(token, params = {})
30
30
  @token = token
31
31
  @timeout = params.fetch(:timeout, nil)
32
32
  @open_timeout = params.fetch(:open_timeout, nil)
33
+ @enable_compression = params.fetch(:enable_compression, false)
33
34
  end
34
35
 
35
36
  # rubocop:disable Metrics/ParameterLists
@@ -56,7 +57,7 @@ module Lokalise
56
57
  return params unless object_key
57
58
 
58
59
  params = [params] unless params.is_a?(Array)
59
- Hash[object_key, params]
60
+ {object_key => params}
60
61
  end
61
62
 
62
63
  alias c_r construct_request
@@ -5,7 +5,16 @@ module Lokalise
5
5
  BASE_URL = 'https://api.lokalise.com/api2/'
6
6
 
7
7
  def connection(client)
8
- options = {
8
+ Faraday.new(options(client), request_params_for(client)) do |faraday|
9
+ faraday.use(:gzip) if client.enable_compression
10
+ faraday.adapter Faraday.default_adapter
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def options(client)
17
+ {
9
18
  headers: {
10
19
  accept: 'application/json',
11
20
  user_agent: "ruby-lokalise-api gem/#{Lokalise::VERSION}",
@@ -13,11 +22,8 @@ module Lokalise
13
22
  },
14
23
  url: BASE_URL
15
24
  }
16
- Faraday.new(options, request_params_for(client)) { |faraday| faraday.adapter Faraday.default_adapter }
17
25
  end
18
26
 
19
- private
20
-
21
27
  # Allows to customize request params per-client
22
28
  def request_params_for(client)
23
29
  {request: {timeout: client.timeout, open_timeout: client.open_timeout}}
@@ -86,7 +86,9 @@
86
86
  "translation_tier",
87
87
  "translation_tier_name",
88
88
  "briefing",
89
- "total"
89
+ "total",
90
+ "payment_method",
91
+ "dry_run"
90
92
  ],
91
93
  "payment_card": [
92
94
  "card_id",
@@ -208,7 +210,8 @@
208
210
  "is_fuzzy",
209
211
  "is_reviewed",
210
212
  "words",
211
- "custom_translation_statuses"
213
+ "custom_translation_statuses",
214
+ "task_id"
212
215
  ],
213
216
  "translation_provider": [
214
217
  "provider_id",
@@ -39,7 +39,8 @@ module Lokalise
39
39
  class << self
40
40
  # Create a new error from an HTTP response
41
41
  def from_response(body)
42
- new body['error']['message'].to_s
42
+ msg = body.key?('error') ? body['error']['message'] : body['message']
43
+ new msg.to_s
43
44
  end
44
45
  end
45
46
 
@@ -58,7 +58,7 @@ module Lokalise
58
58
  def respond_with(response, client)
59
59
  body = custom_load response.body
60
60
  uri = Addressable::URI.parse response.env.url
61
- respond_with_error(response.status, body) if body.respond_to?(:has_key?) && body.key?('error')
61
+ respond_with_error response.status, body if response.status.between?(400, 599) || (body.respond_to?(:has_key?) && body.key?('error'))
62
62
  extract_headers_from(response).
63
63
  merge('content' => body,
64
64
  'client' => client,
@@ -74,7 +74,7 @@ module Lokalise
74
74
  end
75
75
 
76
76
  def respond_with_error(code, body)
77
- raise(Lokalise::Error, body['error']) unless Lokalise::Error::ERRORS.key? code
77
+ raise(Lokalise::Error, body['error'] || body) unless Lokalise::Error::ERRORS.key? code
78
78
 
79
79
  raise Lokalise::Error::ERRORS[code].from_response(body)
80
80
  end
@@ -22,7 +22,8 @@ module Lokalise
22
22
  c_r Lokalise::Resources::File, :download, [project_id, 'download'], params
23
23
  end
24
24
 
25
- # Imports translation file to the given project. File data must base64-encoded
25
+ # Imports translation file to the given project. File data must base64-encoded.
26
+ # To encode your data in Base64, use `Base64.strict_encode64()` method.
26
27
  #
27
28
  # @see https://app.lokalise.com/api2docs/curl/#transition-upload-a-file-post
28
29
  # @return [Hash]
@@ -47,9 +47,7 @@ module Lokalise
47
47
  #
48
48
  # @return [Array<String>]
49
49
  def attributes_for(klass)
50
- @attributes ||= begin
51
- YAML.load_file(File.expand_path('../data/attributes.json', __dir__)).freeze
52
- end
50
+ @attributes ||= YAML.load_file(File.expand_path('../data/attributes.json', __dir__)).freeze
53
51
 
54
52
  name = unify klass.name.snakecase
55
53
  @attributes[name]
@@ -22,6 +22,6 @@ class String
22
22
  end
23
23
 
24
24
  def remove_trailing_slash
25
- gsub %r{/\z}, ''
25
+ delete_suffix '/'
26
26
  end
27
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lokalise
4
- VERSION = '4.0.0'
4
+ VERSION = '4.3.1'
5
5
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'faraday'
4
+ require 'faraday_middleware'
4
5
  require 'yaml'
5
6
  require 'addressable'
6
7
 
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_dependency 'addressable', '~> 2.5'
26
26
  spec.add_dependency 'faraday', '~> 1.0'
27
+ spec.add_dependency 'faraday_middleware', '~> 1.0'
27
28
  spec.add_dependency 'json', '>= 1.8.0'
28
29
 
29
30
  spec.add_development_dependency 'codecov', '~> 0.1'
@@ -3,6 +3,9 @@
3
3
  RSpec.describe Lokalise::Connection do
4
4
  include described_class
5
5
 
6
+ let(:project_id) { '803826145ba90b42d5d860.46800099' }
7
+ let(:key_id) { 44_596_059 }
8
+
6
9
  before { Lokalise.reset_client! }
7
10
 
8
11
  after do
@@ -10,11 +13,12 @@ RSpec.describe Lokalise::Connection do
10
13
  Faraday.default_adapter = :net_http
11
14
  end
12
15
 
13
- it 'timeouts should not be set by default but the token must be present' do
16
+ it 'timeouts and compression should not be set by default but the token must be present' do
14
17
  conn = connection test_client
15
18
  expect(conn.options.timeout).to be_nil
16
19
  expect(conn.options.open_timeout).to be_nil
17
20
  expect(conn.headers['X-api-token']).to eq(test_client.token)
21
+ expect(conn.builder.handlers).not_to include(FaradayMiddleware::Gzip)
18
22
  end
19
23
 
20
24
  it 'allows to customize timeouts' do
@@ -42,4 +46,38 @@ RSpec.describe Lokalise::Connection do
42
46
  expect(another_conn.builder.adapter).to eq(Faraday::Adapter::Excon)
43
47
  expect(conn.builder.adapter).to eq(Faraday::Adapter::NetHttp)
44
48
  end
49
+
50
+ it 'allows to customize compression' do
51
+ custom_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'], enable_compression: true)
52
+ conn = connection custom_client
53
+ expect(conn.headers['X-api-token']).to eq(custom_client.token)
54
+ expect(conn.builder.handlers).to include(FaradayMiddleware::Gzip)
55
+ end
56
+
57
+ it 'is possible to enable gzip compression' do
58
+ gzip_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'], enable_compression: true)
59
+ keys = VCR.use_cassette('all_keys_gzip') do
60
+ gzip_client.keys project_id
61
+ end.collection
62
+
63
+ expect(keys.first.key_id).to eq(key_id)
64
+ end
65
+
66
+ it 'is possible to disable gzip compression' do
67
+ no_gzip_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'], enable_compression: false)
68
+ keys = VCR.use_cassette('all_keys_no_gzip') do
69
+ no_gzip_client.keys project_id
70
+ end.collection
71
+
72
+ expect(keys.first.key_id).to eq(key_id)
73
+ end
74
+
75
+ it 'gzip compression is off by default' do
76
+ default_gzip_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'])
77
+ keys = VCR.use_cassette('all_keys_default_gzip') do
78
+ default_gzip_client.keys project_id
79
+ end.collection
80
+
81
+ expect(keys.first.key_id).to eq(key_id)
82
+ end
45
83
  end
@@ -15,6 +15,14 @@ RSpec.describe Lokalise::Error do
15
15
  end.to raise_error(described_class)
16
16
  end
17
17
 
18
+ it 'handles an exception when the response does not contain an error key' do
19
+ expect do
20
+ VCR.use_cassette('error_no_error_key') do
21
+ get 'projects', Lokalise.client('invalid')
22
+ end
23
+ end.to raise_error(Lokalise::Error::BadRequest)
24
+ end
25
+
18
26
  it 'raises BadRequest when API token is invalid' do
19
27
  expect do
20
28
  VCR.use_cassette('error_invalid_token') do
@@ -37,8 +37,8 @@ RSpec.describe Lokalise::Client do
37
37
 
38
38
  expect(order.order_id).to eq(order_id)
39
39
  expect(order.project_id).to eq(project_id)
40
- expect(order.card_id).to eq(card_id.to_s)
41
- expect(order.status).to eq('in progress')
40
+ expect(order.card_id).to eq(card_id)
41
+ expect(order.status).to eq('completed')
42
42
  expect(order.created_at).to eq('2019-03-19 18:18:21 (Etc/UTC)')
43
43
  expect(order.created_by).to eq(20_181)
44
44
  expect(order.created_by_email).to eq('bodrovis@protonmail.com')
@@ -52,6 +52,7 @@ RSpec.describe Lokalise::Client do
52
52
  expect(order.translation_tier_name).to eq('Professional translator')
53
53
  expect(order.briefing).to eq('Some briefing')
54
54
  expect(order.total).to eq(0.07)
55
+ expect(order.payment_method).to be_nil
55
56
  end
56
57
 
57
58
  specify '#reload_data' do
@@ -86,4 +87,27 @@ RSpec.describe Lokalise::Client do
86
87
  expect(order.order_id).to eq(order_id)
87
88
  expect(order.status).to eq('in progress')
88
89
  end
90
+
91
+ it 'creates an order with dry run' do
92
+ order = VCR.use_cassette('create_order_dry_run') do
93
+ test_client.create_order team_id,
94
+ project_id: project_id,
95
+ card_id: card_id,
96
+ briefing: 'Some briefing',
97
+ source_language_iso: 'en',
98
+ target_language_isos: [
99
+ 'ru'
100
+ ],
101
+ keys: [
102
+ 74_189_435
103
+ ],
104
+ provider_slug: 'gengo',
105
+ translation_tier: '1',
106
+ dry_run: true
107
+ end
108
+
109
+ expect(order.order_id).to be_nil
110
+ expect(order.status).to eq('draft')
111
+ expect(order.dry_run).to be true
112
+ end
89
113
  end
@@ -59,26 +59,27 @@ RSpec.describe Lokalise::Client do
59
59
 
60
60
  specify '#translation' do
61
61
  translation = VCR.use_cassette('translation') do
62
- test_client.translation project_id, translation_id
62
+ test_client.translation project_id, 304_581_218
63
63
  end
64
64
 
65
- expect(translation.translation_id).to eq(translation_id)
66
- expect(translation.key_id).to eq(15_571_975)
67
- expect(translation.language_iso).to eq('en')
68
- expect(translation.modified_at).to eq('2019-03-26 16:41:31 (Etc/UTC)')
69
- expect(translation.modified_at_timestamp).to eq(1_553_618_491)
65
+ expect(translation.translation_id).to eq(304_581_218)
66
+ expect(translation.key_id).to eq(44_596_059)
67
+ expect(translation.language_iso).to eq('ru')
68
+ expect(translation.modified_at).to eq('2020-05-15 10:44:42 (Etc/UTC)')
69
+ expect(translation.modified_at_timestamp).to eq(1_589_539_482)
70
70
  expect(translation.modified_by).to eq(20_181)
71
71
  expect(translation.modified_by_email).to eq('bodrovis@protonmail.com')
72
- expect(translation.translation).to eq('RSpec is a testing suite')
72
+ expect(translation.translation).to eq('Сообщение')
73
73
  expect(translation.is_fuzzy).to eq(false)
74
74
  expect(translation.is_reviewed).to eq(false)
75
- expect(translation.words).to eq(5)
75
+ expect(translation.words).to eq(1)
76
76
  expect(translation.custom_translation_statuses).to eq([])
77
+ expect(translation.task_id).to be_nil
77
78
  end
78
79
 
79
80
  specify '#reload_data' do
80
81
  translation = VCR.use_cassette('translation') do
81
- test_client.translation project_id, translation_id
82
+ test_client.translation project_id, 304_581_218
82
83
  end
83
84
 
84
85
  reloaded_translation = VCR.use_cassette('translation') do
@@ -6,6 +6,7 @@ RSpec.describe Lokalise do
6
6
  expect(test_client.token).to eq(ENV['LOKALISE_API_TOKEN'])
7
7
  expect(test_client.timeout).to be_nil
8
8
  expect(test_client.open_timeout).to be_nil
9
+ expect(test_client.enable_compression).to be false
9
10
  end
10
11
 
11
12
  specify '.reset_client!' do
@@ -29,5 +30,10 @@ RSpec.describe Lokalise do
29
30
  custom_client = described_class.client(ENV['LOKALISE_API_TOKEN'], open_timeout: 100)
30
31
  expect(custom_client.open_timeout).to eq(100)
31
32
  end
33
+
34
+ it 'is possible to customize compression' do
35
+ custom_client = described_class.client(ENV['LOKALISE_API_TOKEN'], enable_compression: true)
36
+ expect(custom_client.enable_compression).to be true
37
+ end
32
38
  end
33
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lokalise-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Bodrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: json
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -334,7 +348,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
334
348
  - !ruby/object:Gem::Version
335
349
  version: '0'
336
350
  requirements: []
337
- rubygems_version: 3.2.7
351
+ rubygems_version: 3.2.26
338
352
  signing_key:
339
353
  specification_version: 4
340
354
  summary: Ruby interface to the Lokalise API
@@ -356,11 +370,11 @@ test_files:
356
370
  - spec/lib/ruby-lokalise-api/rest/screenshots_spec.rb
357
371
  - spec/lib/ruby-lokalise-api/rest/snapshots_spec.rb
358
372
  - spec/lib/ruby-lokalise-api/rest/tasks_spec.rb
359
- - spec/lib/ruby-lokalise-api/rest/teams_spec.rb
360
- - spec/lib/ruby-lokalise-api/rest/team_users_spec.rb
361
373
  - spec/lib/ruby-lokalise-api/rest/team_user_groups_spec.rb
362
- - spec/lib/ruby-lokalise-api/rest/translations_spec.rb
374
+ - spec/lib/ruby-lokalise-api/rest/team_users_spec.rb
375
+ - spec/lib/ruby-lokalise-api/rest/teams_spec.rb
363
376
  - spec/lib/ruby-lokalise-api/rest/translation_providers_spec.rb
377
+ - spec/lib/ruby-lokalise-api/rest/translations_spec.rb
364
378
  - spec/lib/ruby-lokalise-api/rest/webhooks_spec.rb
365
379
  - spec/lib/ruby-lokalise-api/utils/snakecase_spec.rb
366
380
  - spec/lib/ruby-lokalise-api_spec.rb