ruby-lokalise-api 4.3.1 → 4.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/ruby-lokalise-api/client.rb +2 -1
- data/lib/ruby-lokalise-api/collections/base.rb +5 -4
- data/lib/ruby-lokalise-api/connection.rb +1 -1
- data/lib/ruby-lokalise-api/oauth_client.rb +11 -0
- data/lib/ruby-lokalise-api/request.rb +2 -1
- data/lib/ruby-lokalise-api/resources/base.rb +7 -5
- data/lib/ruby-lokalise-api/resources/branch.rb +2 -1
- data/lib/ruby-lokalise-api/version.rb +1 -1
- data/lib/ruby-lokalise-api.rb +15 -0
- data/spec/lib/ruby-lokalise-api/connection_spec.rb +6 -0
- data/spec/lib/ruby-lokalise-api_spec.rb +15 -0
- data/spec/support/test_client.rb +4 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f15e541afdd5e50a0e9317640b872bb04144dcaa717b2390887546b694e308fd
|
4
|
+
data.tar.gz: f34bb8a44f1e8552a0bc83b3f4c15d604500d56ecea503260d6423b047b36dcc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbbc75876ec0e0c9f5a611d54520585759d77cb4f13a81126d7f9a45c89852331801054ec973f94a6c0b4e58c0a8ea8c6cb88167e5b4a4b3ac9521531b911a87
|
7
|
+
data.tar.gz: 2687c66c1fb1ef0012cac50329b87a7c48118cff98a890e06b7764ccce828a850750b6896d5ace0a6810e8cebb86658bbd333794e6bc94cde725c957b1c968f6
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
|
8
8
|
Official opinionated Ruby interface for the [Lokalise API](https://app.lokalise.com/api2docs/curl/) that represents returned data as Ruby objects.
|
9
9
|
|
10
|
-
Looking for a Rails integration? Try the
|
10
|
+
Looking for a Rails integration? Try the [lokalise_rails gem](https://github.com/bodrovis/lokalise_rails). Also you can use a [lokalise_manager gem](https://github.com/bodrovis/lokalise_manager) which allows to exchange translation files between Lokalise and *any* Ruby script.
|
11
11
|
|
12
12
|
## Quickstart
|
13
13
|
|
@@ -39,6 +39,12 @@ process = @client.upload_file project_id,
|
|
39
39
|
process.status
|
40
40
|
```
|
41
41
|
|
42
|
+
Alternatively instantiate your client with an [OAuth2 token](http://docs.lokalise.com/en/articles/5574713-oauth-2):
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
@client = Lokalise.oauth_client 'YOUR_OAUTH_TOKEN_HERE'
|
46
|
+
```
|
47
|
+
|
42
48
|
## Usage
|
43
49
|
|
44
50
|
Detailed documentation can be found at [lokalise.github.io/ruby-lokalise-api](https://lokalise.github.io/ruby-lokalise-api/).
|
@@ -23,7 +23,7 @@ require 'ruby-lokalise-api/rest/webhooks'
|
|
23
23
|
|
24
24
|
module Lokalise
|
25
25
|
class Client
|
26
|
-
attr_reader :token
|
26
|
+
attr_reader :token, :token_header
|
27
27
|
attr_accessor :timeout, :open_timeout, :enable_compression
|
28
28
|
|
29
29
|
def initialize(token, params = {})
|
@@ -31,6 +31,7 @@ module Lokalise
|
|
31
31
|
@timeout = params.fetch(:timeout, nil)
|
32
32
|
@open_timeout = params.fetch(:open_timeout, nil)
|
33
33
|
@enable_compression = params.fetch(:enable_compression, false)
|
34
|
+
@token_header = 'x-api-token'
|
34
35
|
end
|
35
36
|
|
36
37
|
# rubocop:disable Metrics/ParameterLists
|
@@ -19,11 +19,12 @@ module Lokalise
|
|
19
19
|
def initialize(response, params = {})
|
20
20
|
produce_collection_for response
|
21
21
|
populate_pagination_data_for response
|
22
|
+
content = response['content']
|
22
23
|
# Project, team id, user id, and branch may not be present in some cases
|
23
|
-
@project_id =
|
24
|
-
@team_id =
|
25
|
-
@user_id =
|
26
|
-
@branch =
|
24
|
+
@project_id = content['project_id']
|
25
|
+
@team_id = content['team_id']
|
26
|
+
@user_id = content['user_id']
|
27
|
+
@branch = content['branch']
|
27
28
|
@request_params = params
|
28
29
|
@client = response['client']
|
29
30
|
@path = response['path']
|
@@ -58,7 +58,8 @@ 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
|
-
|
61
|
+
status = response.status
|
62
|
+
respond_with_error status, body if status.between?(400, 599) || (body.respond_to?(:has_key?) && body.key?('error'))
|
62
63
|
extract_headers_from(response).
|
63
64
|
merge('content' => body,
|
64
65
|
'client' => client,
|
@@ -107,10 +107,11 @@ module Lokalise
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def produce_resource(model_class, response)
|
110
|
+
content = response['content']
|
110
111
|
data_key_singular = data_key_for model_class: model_class
|
111
|
-
if
|
112
|
-
data =
|
113
|
-
|
112
|
+
if content.key? data_key_singular
|
113
|
+
data = content.delete data_key_singular
|
114
|
+
content.merge! data
|
114
115
|
end
|
115
116
|
|
116
117
|
new response
|
@@ -156,9 +157,10 @@ module Lokalise
|
|
156
157
|
# Sometimes there is an `id_key` but it has a value of `null`
|
157
158
|
# (for example when we do not place the actual order but only check its price).
|
158
159
|
# Therefore we must explicitly check if the key is present
|
159
|
-
|
160
|
+
content = response['content']
|
161
|
+
return content[id_key] if content.key?(id_key)
|
160
162
|
|
161
|
-
|
163
|
+
content[data_key][id_key]
|
162
164
|
end
|
163
165
|
|
164
166
|
# Store all resources attributes under the corresponding instance variables.
|
@@ -6,7 +6,8 @@ module Lokalise
|
|
6
6
|
supports :update, :destroy, [:reload_data, '', :find]
|
7
7
|
|
8
8
|
def merge(params = {})
|
9
|
-
|
9
|
+
klass = self.class
|
10
|
+
klass.merge @client, klass.endpoint(project_id, branch_id, :merge), params
|
10
11
|
end
|
11
12
|
|
12
13
|
class << self
|
data/lib/ruby-lokalise-api.rb
CHANGED
@@ -63,6 +63,7 @@ require 'ruby-lokalise-api/collections/custom_translation_status'
|
|
63
63
|
require 'ruby-lokalise-api/collections/webhook'
|
64
64
|
|
65
65
|
require 'ruby-lokalise-api/client'
|
66
|
+
require 'ruby-lokalise-api/oauth_client'
|
66
67
|
|
67
68
|
module Lokalise
|
68
69
|
class << self
|
@@ -79,5 +80,19 @@ module Lokalise
|
|
79
80
|
def reset_client!
|
80
81
|
@client = nil
|
81
82
|
end
|
83
|
+
|
84
|
+
# Initializes a new OAuthClient object
|
85
|
+
#
|
86
|
+
# @return [Lokalise::OAuthClient]
|
87
|
+
# @param token [String]
|
88
|
+
# @param params [Hash]
|
89
|
+
def oauth_client(token, params = {})
|
90
|
+
@oauth_client ||= Lokalise::OAuthClient.new token, params
|
91
|
+
end
|
92
|
+
|
93
|
+
# Reset the currently set OAuth client
|
94
|
+
def reset_oauth_client!
|
95
|
+
@oauth_client = nil
|
96
|
+
end
|
82
97
|
end
|
83
98
|
end
|
@@ -13,6 +13,12 @@ RSpec.describe Lokalise::Connection do
|
|
13
13
|
Faraday.default_adapter = :net_http
|
14
14
|
end
|
15
15
|
|
16
|
+
it 'Authorization header must be present for OAuth client' do
|
17
|
+
conn = connection test_oauth_client
|
18
|
+
expect(conn.headers['Authorization']).to eq("Bearer #{test_client.token}")
|
19
|
+
expect(conn.headers['X-api-token']).to be_nil
|
20
|
+
end
|
21
|
+
|
16
22
|
it 'timeouts and compression should not be set by default but the token must be present' do
|
17
23
|
conn = connection test_client
|
18
24
|
expect(conn.options.timeout).to be_nil
|
@@ -16,6 +16,21 @@ RSpec.describe Lokalise do
|
|
16
16
|
expect(current_client).to be_nil
|
17
17
|
end
|
18
18
|
|
19
|
+
specify '.oauth_client' do
|
20
|
+
expect(test_oauth_client).to be_an_instance_of(Lokalise::OAuthClient)
|
21
|
+
expect(test_oauth_client.token).to eq("Bearer #{ENV['LOKALISE_API_TOKEN']}")
|
22
|
+
expect(test_oauth_client.timeout).to be_nil
|
23
|
+
expect(test_oauth_client.open_timeout).to be_nil
|
24
|
+
expect(test_oauth_client.enable_compression).to be false
|
25
|
+
end
|
26
|
+
|
27
|
+
specify '.reset_oauth_client!' do
|
28
|
+
expect(test_oauth_client).to be_an_instance_of(Lokalise::OAuthClient)
|
29
|
+
described_class.reset_oauth_client!
|
30
|
+
current_oauth_client = described_class.instance_variable_get '@oauth_client'
|
31
|
+
expect(current_oauth_client).to be_nil
|
32
|
+
end
|
33
|
+
|
19
34
|
context 'with client params' do
|
20
35
|
before { described_class.reset_client! }
|
21
36
|
|
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: 4.
|
4
|
+
version: 4.4.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: 2021-
|
11
|
+
date: 2021-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -252,6 +252,7 @@ files:
|
|
252
252
|
- lib/ruby-lokalise-api/data/attributes.json
|
253
253
|
- lib/ruby-lokalise-api/error.rb
|
254
254
|
- lib/ruby-lokalise-api/json_handler.rb
|
255
|
+
- lib/ruby-lokalise-api/oauth_client.rb
|
255
256
|
- lib/ruby-lokalise-api/request.rb
|
256
257
|
- lib/ruby-lokalise-api/resources/base.rb
|
257
258
|
- lib/ruby-lokalise-api/resources/branch.rb
|