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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4147ca27a676c98d74fa654c4244212aa7e296855d4bda492fd061a3a3a02347
4
- data.tar.gz: 59c042db99a7f2899baef674319ec18ab9ee193f3581f35b40fd92b0c43118bb
3
+ metadata.gz: f15e541afdd5e50a0e9317640b872bb04144dcaa717b2390887546b694e308fd
4
+ data.tar.gz: f34bb8a44f1e8552a0bc83b3f4c15d604500d56ecea503260d6423b047b36dcc
5
5
  SHA512:
6
- metadata.gz: 38e44afa51cb74149fb4b7aff68d958232f39d111bef8927835fc9bd012c8738ac0a4a4c175ab1bc9e7b2a846135a5f4ff5473845d562d45d408200e439abedf
7
- data.tar.gz: 67cc5c58134b64955f65595f5b5797c72adbaa8dbe9e239fff0d434044e3a599c0eea4dd7418bbbb2834b9cd8f9e7572258f07a514f80c048508cfba408cd2d0
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 new [lokalise_rails gem](https://github.com/bodrovis/lokalise_rails).
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 = response['content']['project_id']
24
- @team_id = response['content']['team_id']
25
- @user_id = response['content']['user_id']
26
- @branch = response['content']['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']
@@ -18,7 +18,7 @@ module Lokalise
18
18
  headers: {
19
19
  accept: 'application/json',
20
20
  user_agent: "ruby-lokalise-api gem/#{Lokalise::VERSION}",
21
- 'x-api-token': client.token
21
+ client.token_header => client.token
22
22
  },
23
23
  url: BASE_URL
24
24
  }
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lokalise
4
+ class OAuthClient < Client
5
+ def initialize(token, params = {})
6
+ super(token, params)
7
+ @token_header = 'Authorization'
8
+ @token = "Bearer #{@token}"
9
+ end
10
+ end
11
+ end
@@ -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
- respond_with_error response.status, body if response.status.between?(400, 599) || (body.respond_to?(:has_key?) && body.key?('error'))
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 response['content'].key? data_key_singular
112
- data = response['content'].delete data_key_singular
113
- response['content'].merge! data
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
- return response['content'][id_key] if response['content'].key?(id_key)
160
+ content = response['content']
161
+ return content[id_key] if content.key?(id_key)
160
162
 
161
- response['content'][data_key][id_key]
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
- self.class.merge @client, self.class.endpoint(project_id, branch_id, :merge), params
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lokalise
4
- VERSION = '4.3.1'
4
+ VERSION = '4.4.0'
5
5
  end
@@ -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
 
@@ -4,4 +4,8 @@ module TestClient
4
4
  def test_client(token = nil, params = {})
5
5
  Lokalise.client(token || ENV['LOKALISE_API_TOKEN'], params)
6
6
  end
7
+
8
+ def test_oauth_client(token = nil, params = {})
9
+ Lokalise.oauth_client(token || ENV['LOKALISE_API_TOKEN'], params)
10
+ end
7
11
  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.3.1
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-09-21 00:00:00.000000000 Z
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