ruby-lokalise-api 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/README.md +37 -1
- data/lib/ruby-lokalise-api.rb +4 -2
- data/lib/ruby-lokalise-api/client.rb +4 -1
- data/lib/ruby-lokalise-api/connection.rb +9 -3
- data/lib/ruby-lokalise-api/request.rb +4 -4
- data/lib/ruby-lokalise-api/version.rb +1 -1
- data/spec/lib/ruby-lokalise-api/connection_spec.rb +41 -0
- data/spec/lib/ruby-lokalise-api/error_spec.rb +1 -2
- data/spec/lib/ruby-lokalise-api_spec.rb +17 -0
- data/spec/support/test_client.rb +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebd0d7d723970806d7d8078803ada29e359b90f6248a34fb17176b55f977f39b
|
4
|
+
data.tar.gz: 5cbc279f312e4021ed6e6028e843013507aad310cc76f3376a206a65ea391726
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99ce24c8248ed892d0a37688a7df26ba661a017be9630f22cac528be128172684e8e3ecc9022d4a94897ef5b2b2fe3cb21182be7b3778eb8f8d8a773984e4b32
|
7
|
+
data.tar.gz: 4688f964ff6d8f9dccede52a9bf9f8e40a31e3674242d49f1329373ca271bb25cd7b1cd204805e9799982028afebf8e97a0ea4a92a7b8f99acbd4a02105c5d17
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 2.5.0 (01-Aug-19)
|
4
|
+
|
5
|
+
* Added `:timeout` and `:open_timeout` options for the client [to customize request timeouts](https://github.com/lokalise/ruby-lokalise-api#setting-timeouts)
|
6
|
+
* [Added explanations](https://github.com/lokalise/ruby-lokalise-api#choosing-adapter) on how to change a default adapter
|
7
|
+
|
3
8
|
## 2.4.0 (31-Jul-19)
|
4
9
|
|
5
|
-
* Remove MultiJson dependency and allow to use a custom JSON parser
|
10
|
+
* Remove MultiJson dependency and allow to use a [custom JSON parser](https://github.com/lokalise/ruby-lokalise-api#customizing-json-parser)
|
6
11
|
|
7
12
|
## 2.3.0 (17-Jul-19)
|
8
13
|
|
data/README.md
CHANGED
@@ -31,6 +31,7 @@ Official opinionated Ruby interface for the [Lokalise API](https://lokalise.co/a
|
|
31
31
|
+ [Translation Providers](#translation-providers)
|
32
32
|
+ [Translation Statuses](#translation-statuses)
|
33
33
|
* [Additional Info](#additional-info)
|
34
|
+
+ [Customizing request](#customizing-request)
|
34
35
|
+ [Customizing JSON parser](#customizing-json-parser)
|
35
36
|
+ [Error handling](#error-handling)
|
36
37
|
+ [API Rate Limits](#api-rate-limits)
|
@@ -60,7 +61,7 @@ require 'ruby-lokalise-api'
|
|
60
61
|
@client = Lokalise.client 'YOUR_TOKEN_HERE'
|
61
62
|
```
|
62
63
|
|
63
|
-
Now the `@client` can be used to perform API requests!
|
64
|
+
Now the `@client` can be used to perform API requests! Learn more about additional options in the [Customizing request section](#customizing-request).
|
64
65
|
|
65
66
|
### Objects and models
|
66
67
|
|
@@ -1470,6 +1471,41 @@ As long as Lokalise supports only very limited array of color hexadecimal codes
|
|
1470
1471
|
|
1471
1472
|
## Additional Info
|
1472
1473
|
|
1474
|
+
### Customizing Request
|
1475
|
+
|
1476
|
+
#### Choosing Adapter
|
1477
|
+
|
1478
|
+
This library utilizes [Faraday](https://lostisland.github.io/faraday) to perform requests. The default adapter is built-in [Net::HTTP](https://ruby-doc.org/stdlib-2.6.3/libdoc/net/http/rdoc/Net/HTTP.html) but you may customize it as needed.
|
1479
|
+
|
1480
|
+
For example, to use [Excon](https://github.com/excon/excon):
|
1481
|
+
|
1482
|
+
```ruby
|
1483
|
+
require 'excon' # somewhere in your code
|
1484
|
+
|
1485
|
+
Faraday.default_adapter = :excon
|
1486
|
+
```
|
1487
|
+
|
1488
|
+
All supported adapters are listed [on Faraday official website](https://lostisland.github.io/faraday/adapters/).
|
1489
|
+
|
1490
|
+
#### Setting Timeouts
|
1491
|
+
|
1492
|
+
Request timeouts may be adjusted during client initialization:
|
1493
|
+
|
1494
|
+
```ruby
|
1495
|
+
@client = Lokalise.client('YOUR_TOKEN', open_timeout: 100, timeout: 500)
|
1496
|
+
@client.open_timeout # => 100
|
1497
|
+
@client.timeout # => 500
|
1498
|
+
```
|
1499
|
+
|
1500
|
+
Both values are in *seconds*. They can be adjusted later with simple accessors:
|
1501
|
+
|
1502
|
+
```ruby
|
1503
|
+
@client.open_timeout = 200
|
1504
|
+
@client.timeout = 600
|
1505
|
+
@client.open_timeout # => 200
|
1506
|
+
@client.timeout # => 600
|
1507
|
+
```
|
1508
|
+
|
1473
1509
|
### Customizing JSON parser
|
1474
1510
|
|
1475
1511
|
This gem used to rely on [MultiJson](https://github.com/intridea/multi_json) but it is not maintained anymore. By default we are using a [built-in JSON module](https://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html) but you may utilize any other parser by overriding `#custom_dump` and `#custom_load` methods inside `Lokalise::JsonHandler` module.
|
data/lib/ruby-lokalise-api.rb
CHANGED
@@ -60,8 +60,10 @@ module Lokalise
|
|
60
60
|
# Initializes a new Client object
|
61
61
|
#
|
62
62
|
# @return [Lokalise::Client]
|
63
|
-
|
64
|
-
|
63
|
+
# @param token [String]
|
64
|
+
# @param params [Hash]
|
65
|
+
def client(token, params = {})
|
66
|
+
@client ||= Lokalise::Client.new token, params
|
65
67
|
end
|
66
68
|
|
67
69
|
# Reset the currently set client
|
@@ -19,9 +19,12 @@ require 'ruby-lokalise-api/rest/custom_translation_statuses'
|
|
19
19
|
module Lokalise
|
20
20
|
class Client
|
21
21
|
attr_reader :token
|
22
|
+
attr_accessor :timeout, :open_timeout
|
22
23
|
|
23
|
-
def initialize(token)
|
24
|
+
def initialize(token, params = {})
|
24
25
|
@token = token
|
26
|
+
@timeout = params.fetch(:timeout) { nil }
|
27
|
+
@open_timeout = params.fetch(:open_timeout) { nil }
|
25
28
|
end
|
26
29
|
|
27
30
|
def construct_request(klass, method, endpoint_ids, params = {}, object_key = nil)
|
@@ -2,16 +2,22 @@ module Lokalise
|
|
2
2
|
module Connection
|
3
3
|
BASE_URL = 'https://api.lokalise.co/api2/'.freeze
|
4
4
|
|
5
|
-
def connection(
|
5
|
+
def connection(client)
|
6
6
|
options = {
|
7
7
|
headers: {
|
8
8
|
accept: 'application/json',
|
9
9
|
user_agent: "ruby-lokalise-api gem/#{Lokalise::VERSION}",
|
10
|
-
'x-api-token': token
|
10
|
+
'x-api-token': client.token
|
11
11
|
},
|
12
12
|
url: BASE_URL
|
13
13
|
}
|
14
|
-
Faraday.new(options) { |faraday| faraday.adapter Faraday.default_adapter }
|
14
|
+
Faraday.new(options, request_params_for(client)) { |faraday| faraday.adapter Faraday.default_adapter }
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def request_params_for(client)
|
20
|
+
{request: {timeout: client.timeout, open_timeout: client.open_timeout}}
|
15
21
|
end
|
16
22
|
end
|
17
23
|
end
|
@@ -7,21 +7,21 @@ module Lokalise
|
|
7
7
|
|
8
8
|
def get(path, client, params = {})
|
9
9
|
respond_with(
|
10
|
-
connection(client
|
10
|
+
connection(client).get(prepare(path), params),
|
11
11
|
client
|
12
12
|
)
|
13
13
|
end
|
14
14
|
|
15
15
|
def post(path, client, params = {})
|
16
16
|
respond_with(
|
17
|
-
connection(client
|
17
|
+
connection(client).post(prepare(path), custom_dump(params)),
|
18
18
|
client
|
19
19
|
)
|
20
20
|
end
|
21
21
|
|
22
22
|
def put(path, client, params = {})
|
23
23
|
respond_with(
|
24
|
-
connection(client
|
24
|
+
connection(client).put(prepare(path), custom_dump(params)),
|
25
25
|
client
|
26
26
|
)
|
27
27
|
end
|
@@ -30,7 +30,7 @@ module Lokalise
|
|
30
30
|
respond_with(
|
31
31
|
# Rubocop tries to replace `delete` with `gsub` but that's a different method here!
|
32
32
|
# rubocop:disable Style/CollectionMethods
|
33
|
-
connection(client
|
33
|
+
connection(client).delete(prepare(path)) do |req|
|
34
34
|
# rubocop:enable Style/CollectionMethods
|
35
35
|
req.body = custom_dump params
|
36
36
|
end,
|
@@ -0,0 +1,41 @@
|
|
1
|
+
RSpec.describe Lokalise::Connection do
|
2
|
+
include described_class
|
3
|
+
|
4
|
+
before(:each) { Lokalise.reset_client! }
|
5
|
+
after(:all) do
|
6
|
+
Lokalise.reset_client!
|
7
|
+
Faraday.default_adapter = :net_http
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'timeouts should not be set by default but the token must be present' do
|
11
|
+
conn = connection test_client
|
12
|
+
expect(conn.options.timeout).to be_nil
|
13
|
+
expect(conn.options.open_timeout).to be_nil
|
14
|
+
expect(conn.headers['X-api-token']).to eq(test_client.token)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should allow to customize timeouts' do
|
18
|
+
custom_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'],
|
19
|
+
open_timeout: 100, timeout: 500)
|
20
|
+
conn = connection custom_client
|
21
|
+
expect(conn.options.timeout).to eq(500)
|
22
|
+
expect(conn.options.open_timeout).to eq(100)
|
23
|
+
expect(conn.headers['X-api-token']).to eq(custom_client.token)
|
24
|
+
|
25
|
+
custom_client.timeout = 300
|
26
|
+
custom_client.open_timeout = 200
|
27
|
+
another_conn = connection custom_client
|
28
|
+
expect(another_conn.options.timeout).to eq(300)
|
29
|
+
expect(another_conn.options.open_timeout).to eq(200)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should be possible to customize adapter' do
|
33
|
+
conn = connection test_client
|
34
|
+
expect(conn.builder.handlers).to eq([Faraday::Adapter::NetHttp])
|
35
|
+
|
36
|
+
Faraday.default_adapter = :excon
|
37
|
+
|
38
|
+
another_conn = connection test_client
|
39
|
+
expect(another_conn.builder.handlers).to eq([Faraday::Adapter::Excon])
|
40
|
+
end
|
41
|
+
end
|
@@ -1,9 +1,8 @@
|
|
1
|
-
# TODO: rewrite with raw `get` method
|
2
1
|
RSpec.describe Lokalise::Error do
|
3
2
|
include Lokalise::Request
|
4
3
|
|
5
4
|
before(:each) { Lokalise.reset_client! }
|
6
|
-
after(:
|
5
|
+
after(:all) { Lokalise.reset_client! }
|
7
6
|
|
8
7
|
it 'should raise BadRequest when API token is invalid' do
|
9
8
|
expect do
|
@@ -2,6 +2,8 @@ RSpec.describe Lokalise do
|
|
2
2
|
specify '.client' do
|
3
3
|
expect(test_client).to be_an_instance_of(Lokalise::Client)
|
4
4
|
expect(test_client.token).to eq(ENV['LOKALISE_API_TOKEN'])
|
5
|
+
expect(test_client.timeout).to be_nil
|
6
|
+
expect(test_client.open_timeout).to be_nil
|
5
7
|
end
|
6
8
|
|
7
9
|
specify '.reset_client!' do
|
@@ -10,4 +12,19 @@ RSpec.describe Lokalise do
|
|
10
12
|
current_client = Lokalise.instance_variable_get '@client'
|
11
13
|
expect(current_client).to be_nil
|
12
14
|
end
|
15
|
+
|
16
|
+
context 'client params' do
|
17
|
+
before(:each) { Lokalise.reset_client! }
|
18
|
+
after(:all) { Lokalise.reset_client! }
|
19
|
+
|
20
|
+
it 'should be possible to customize timeout' do
|
21
|
+
custom_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'], timeout: 600)
|
22
|
+
expect(custom_client.timeout).to eq(600)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should be possible to customize open timeout' do
|
26
|
+
custom_client = Lokalise.client(ENV['LOKALISE_API_TOKEN'], open_timeout: 100)
|
27
|
+
expect(custom_client.open_timeout).to eq(100)
|
28
|
+
end
|
29
|
+
end
|
13
30
|
end
|
data/spec/support/test_client.rb
CHANGED
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: 2.
|
4
|
+
version: 2.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Bodrov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -236,6 +236,7 @@ files:
|
|
236
236
|
- lib/ruby-lokalise-api/utils/string_utils.rb
|
237
237
|
- lib/ruby-lokalise-api/version.rb
|
238
238
|
- ruby-lokalise-api.gemspec
|
239
|
+
- spec/lib/ruby-lokalise-api/connection_spec.rb
|
239
240
|
- spec/lib/ruby-lokalise-api/custom_json_parser_spec.rb
|
240
241
|
- spec/lib/ruby-lokalise-api/error_spec.rb
|
241
242
|
- spec/lib/ruby-lokalise-api/rest/comments_spec.rb
|
@@ -284,6 +285,7 @@ signing_key:
|
|
284
285
|
specification_version: 4
|
285
286
|
summary: Ruby interface to the Lokalise API
|
286
287
|
test_files:
|
288
|
+
- spec/lib/ruby-lokalise-api/connection_spec.rb
|
287
289
|
- spec/lib/ruby-lokalise-api/custom_json_parser_spec.rb
|
288
290
|
- spec/lib/ruby-lokalise-api/error_spec.rb
|
289
291
|
- spec/lib/ruby-lokalise-api/rest/comments_spec.rb
|