ruby-lokalise-api 6.2.0 → 7.0.0

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: 90b862c7994f5c977a2a3a71dac7877163d83c7051ea16b808a770f2b1c24f9e
4
- data.tar.gz: ca1cb74e869faae115c85d59011bd254bf0f06e6738d81297de4239d73c4c8d9
3
+ metadata.gz: a0df97ee1958c45138da225b66c6cc99797a9cf77c3f84435b3fff41e86317c9
4
+ data.tar.gz: a98e58642231426d959a49299d47154c1883d1fa89214693bf062642a89c4581
5
5
  SHA512:
6
- metadata.gz: 98cba873be8c354d5de708493cbdf078bca3c1269b8587417044b3a7e5b2c8d235b83e09de84cb2d03279d3459074b1b150dc7b8a5e830be12332254d2f57cbf
7
- data.tar.gz: 5ef019ca07ac5e515f31805ebc63dd962832e42df1c10de46bc6a053de8639d92831763687d8cd80289ec8fb755da9c6b15f8272c0ec50c00c0ce722be4ed30c
6
+ metadata.gz: 3b1760ad8acee33810dc905ceb9fe8ff3791b8ac42cda5affb031112c53b0981dc5a2ad714d5ee04352fd5c50856ec695c45abf1c3765e939ab8230f1ed78ff1
7
+ data.tar.gz: 947b22b36b74ebde8abeeb8edde48b701e9b4cfd2f4f7bbf5f1c2e35aa94408c1b4deb60b56ab03c65ae1f0f5405c0aef0a9a5cdbf15d0e8e094060d75aeea34
data/Rakefile CHANGED
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rake'
4
+ require 'rake/clean'
5
+ require 'rspec/core/rake_task'
6
+ require 'rubocop/rake_task'
4
7
 
5
8
  begin
6
9
  require 'bundler/setup'
@@ -9,13 +12,33 @@ rescue LoadError
9
12
  puts 'although not required, bundler is recommened for running the tests'
10
13
  end
11
14
 
12
- task default: :spec
13
-
14
- require 'rspec/core/rake_task'
15
15
  RSpec::Core::RakeTask.new(:spec)
16
16
 
17
- require 'rubocop/rake_task'
18
17
  RuboCop::RakeTask.new do |task|
19
18
  task.requires << 'rubocop-performance'
20
19
  task.requires << 'rubocop-rspec'
20
+ task.requires << 'rubocop-rake'
21
+ end
22
+
23
+ CLOBBER.include(FileList['./*.gem'])
24
+
25
+ namespace :lokalise do
26
+ desc 'Updates RubyGems, installs dependencies'
27
+ task :install do
28
+ puts 'Running bundle install'
29
+ sh 'gem update --system'
30
+ sh 'bundle'
31
+ end
32
+
33
+ desc 'Builds the gem'
34
+ task :build do
35
+ puts 'Building'
36
+ sh 'gem build ruby-lokalise-api.gemspec'
37
+ end
21
38
  end
39
+
40
+ task rubospec: %w[rubocop spec]
41
+
42
+ task full_build: %w[clobber lokalise:install lokalise:build]
43
+
44
+ task default: :full_build
@@ -3,6 +3,47 @@
3
3
  module RubyLokaliseApi
4
4
  module BaseRequest
5
5
  include RubyLokaliseApi::JsonHandler
6
+ include RubyLokaliseApi::Connection
7
+
8
+ def get(path, client, params = {})
9
+ respond_with(
10
+ connection(client).get(prepare(path), params),
11
+ client
12
+ )
13
+ end
14
+
15
+ def post(path, client, params = {})
16
+ respond_with(
17
+ connection(client).post(prepare(path), custom_dump(params)),
18
+ client
19
+ )
20
+ end
21
+
22
+ def put(path, client, params = {})
23
+ respond_with(
24
+ connection(client).put(prepare(path), custom_dump(params)),
25
+ client
26
+ )
27
+ end
28
+
29
+ def patch(path, client, params = {})
30
+ respond_with(
31
+ connection(client).patch(prepare(path), params.any? ? custom_dump(params) : nil),
32
+ client
33
+ )
34
+ end
35
+
36
+ def delete(path, client, params = {})
37
+ respond_with(
38
+ # Rubocop tries to replace `delete` with `gsub` but that's a different method here!
39
+ # rubocop:disable Style/CollectionMethods
40
+ connection(client).delete(prepare(path)) do |req|
41
+ # rubocop:enable Style/CollectionMethods
42
+ req.body = custom_dump params
43
+ end,
44
+ client
45
+ )
46
+ end
6
47
 
7
48
  private
8
49
 
@@ -11,8 +52,16 @@ module RubyLokaliseApi
11
52
  path.delete_prefix('/').gsub(%r{//}, '/').gsub(%r{/+\z}, '')
12
53
  end
13
54
 
14
- def raise_on_error!(status, body)
15
- respond_with_error(status, body) if status.between?(400, 599) || (body.respond_to?(:has_key?) && body.key?('error'))
55
+ def raise_on_error!(response, body)
56
+ return unless !response.success? || (body.respond_to?(:has_key?) && body.key?('error'))
57
+
58
+ respond_with_error(response.status, body)
59
+ end
60
+
61
+ def respond_with(response, _client)
62
+ body = custom_load response.body
63
+ raise_on_error! response, body
64
+ body
16
65
  end
17
66
 
18
67
  def respond_with_error(code, body)
@@ -42,7 +42,8 @@ module RubyLokaliseApi
42
42
  # @param endpoint_ids [Array, Hash] IDs that are used to generate the proper path to the endpoint
43
43
  # @param params [Array, Hash] Request parameters
44
44
  # @param object_key [String, Symbol] Key that should be used to wrap parameters into
45
- # @param initial_ids [Array] IDs that should be used to generate base endpoint path. The base path is used for method chaining
45
+ # @param initial_ids [Array] IDs that should be used to generate base endpoint path.
46
+ # The base path is used for method chaining
46
47
  def construct_request(klass, method, endpoint_ids, params = {}, object_key = nil, initial_ids = nil)
47
48
  path = klass.endpoint(*endpoint_ids)
48
49
  formatted_params = format_params(params, object_key)
@@ -62,6 +63,14 @@ module RubyLokaliseApi
62
63
  {object_key => params}
63
64
  end
64
65
 
66
+ def base_url
67
+ 'https://api.lokalise.com/api2/'
68
+ end
69
+
70
+ def compression?
71
+ true
72
+ end
73
+
65
74
  alias c_r construct_request
66
75
  end
67
76
  end
@@ -2,26 +2,33 @@
2
2
 
3
3
  module RubyLokaliseApi
4
4
  module Connection
5
- BASE_URL = 'https://api.lokalise.com/api2/'
6
-
7
5
  def connection(client)
8
6
  Faraday.new(options(client), request_params_for(client)) do |faraday|
9
7
  faraday.adapter Faraday.default_adapter
10
- faraday.request :gzip
8
+ faraday.request(:gzip) if client.compression?
11
9
  end
12
10
  end
13
11
 
14
12
  private
15
13
 
16
14
  def options(client)
15
+ params = __base_options(client)
16
+
17
+ if client.respond_to?(:token) && client.respond_to?(:token_header)
18
+ params[:headers][client.token_header] = client.token
19
+ end
20
+ params[:headers][:accept_encoding] = 'gzip,deflate,br' if client.compression?
21
+
22
+ params
23
+ end
24
+
25
+ def __base_options(client)
17
26
  {
18
27
  headers: {
19
28
  accept: 'application/json',
20
- user_agent: "ruby-lokalise-api gem/#{RubyLokaliseApi::VERSION}",
21
- accept_encoding: 'gzip,deflate,br',
22
- client.token_header => client.token
29
+ user_agent: "ruby-lokalise-api gem/#{RubyLokaliseApi::VERSION}"
23
30
  },
24
- url: BASE_URL
31
+ url: client.base_url
25
32
  }
26
33
  end
27
34
 
@@ -3,13 +3,19 @@
3
3
  module RubyLokaliseApi
4
4
  module OAuth2
5
5
  class Auth
6
- include RubyLokaliseApi::OAuth2::Request
6
+ include RubyLokaliseApi::BaseRequest
7
7
 
8
- attr_reader :client_id, :client_secret
8
+ attr_reader :client_id, :client_secret, :timeout, :open_timeout
9
9
 
10
- def initialize(client_id, client_secret)
10
+ def initialize(client_id, client_secret, params = {})
11
11
  @client_id = client_id
12
12
  @client_secret = client_secret
13
+ @timeout = params[:timeout]
14
+ @open_timeout = params[:open_timeout]
15
+ end
16
+
17
+ def base_url
18
+ URI('https://app.lokalise.com/oauth2/')
13
19
  end
14
20
 
15
21
  def auth(scope:, redirect_uri: nil, state: nil)
@@ -30,7 +36,8 @@ module RubyLokaliseApi
30
36
  code: code,
31
37
  grant_type: 'authorization_code'
32
38
  })
33
- post 'token', params
39
+
40
+ RubyLokaliseApi::OAuth2::Token.new post('token', self, params)
34
41
  end
35
42
 
36
43
  def refresh(token)
@@ -38,7 +45,12 @@ module RubyLokaliseApi
38
45
  refresh_token: token,
39
46
  grant_type: 'refresh_token'
40
47
  })
41
- post 'token', params
48
+
49
+ RubyLokaliseApi::OAuth2::Refresh.new post('token', self, params)
50
+ end
51
+
52
+ def compression?
53
+ false
42
54
  end
43
55
 
44
56
  private
@@ -52,8 +64,8 @@ module RubyLokaliseApi
52
64
 
53
65
  def _build_url_from(params)
54
66
  URI::HTTPS.build(
55
- host: BASE_URL.host,
56
- path: "#{BASE_URL.path}auth",
67
+ host: base_url.host,
68
+ path: "#{base_url.path}auth",
57
69
  query: URI.encode_www_form(params)
58
70
  ).to_s
59
71
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module OAuth2
5
+ class Refresh
6
+ attr_reader :access_token, :expires_in, :scope, :token_type
7
+
8
+ def initialize(raw_params)
9
+ @access_token = raw_params['access_token']
10
+ @expires_in = raw_params['expires_in']
11
+ @scope = raw_params['scope']
12
+ @token_type = raw_params['token_type']
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLokaliseApi
4
+ module OAuth2
5
+ class Token
6
+ attr_reader :access_token, :refresh_token, :expires_in, :token_type
7
+
8
+ def initialize(raw_params)
9
+ @access_token = raw_params['access_token']
10
+ @refresh_token = raw_params['refresh_token']
11
+ @expires_in = raw_params['expires_in']
12
+ @token_type = raw_params['token_type']
13
+ end
14
+ end
15
+ end
16
+ end
@@ -3,58 +3,17 @@
3
3
  module RubyLokaliseApi
4
4
  module Request
5
5
  include RubyLokaliseApi::BaseRequest
6
- include RubyLokaliseApi::Connection
7
6
 
8
7
  # Lokalise returns pagination info in special headers
9
- PAGINATION_HEADERS = %w[x-pagination-total-count x-pagination-page-count x-pagination-limit x-pagination-page].freeze
10
-
11
- def get(path, client, params = {})
12
- respond_with(
13
- connection(client).get(prepare(path), params),
14
- client
15
- )
16
- end
17
-
18
- def post(path, client, params = {})
19
- respond_with(
20
- connection(client).post(prepare(path), custom_dump(params)),
21
- client
22
- )
23
- end
24
-
25
- def put(path, client, params = {})
26
- respond_with(
27
- connection(client).put(prepare(path), custom_dump(params)),
28
- client
29
- )
30
- end
31
-
32
- def patch(path, client, params = {})
33
- respond_with(
34
- connection(client).patch(prepare(path), params.any? ? custom_dump(params) : nil),
35
- client
36
- )
37
- end
38
-
39
- def delete(path, client, params = {})
40
- respond_with(
41
- # Rubocop tries to replace `delete` with `gsub` but that's a different method here!
42
- # rubocop:disable Style/CollectionMethods
43
- connection(client).delete(prepare(path)) do |req|
44
- # rubocop:enable Style/CollectionMethods
45
- req.body = custom_dump params
46
- end,
47
- client
48
- )
49
- end
8
+ PAGINATION_HEADERS = %w[x-pagination-total-count x-pagination-page-count x-pagination-limit
9
+ x-pagination-page].freeze
50
10
 
51
11
  private
52
12
 
53
13
  def respond_with(response, client)
54
14
  body = custom_load response.body
55
15
  uri = Addressable::URI.parse response.env.url
56
- status = response.status
57
- raise_on_error! status, body
16
+ raise_on_error! response, body
58
17
  extract_headers_from(response).
59
18
  merge('content' => body,
60
19
  'client' => client,
@@ -27,12 +27,17 @@ module RubyLokaliseApi
27
27
  @path = infer_path_from response, endpoint_generator
28
28
  end
29
29
 
30
+ # Returns object attribute with [] notation by calling
31
+ # the corresponding method on the object if the
32
+ # instance variable named after the requested key exists
33
+ #
34
+ # @param raw_key_attr [String or Hash]
30
35
  def [](raw_key_attr)
31
36
  key_attr = raw_key_attr.to_s.to_sym
32
37
 
33
- return nil unless self.instance_variables.include?(:"@#{key_attr}")
38
+ return nil unless instance_variables.include?(:"@#{key_attr}")
34
39
 
35
- self.send key_attr
40
+ send key_attr
36
41
  end
37
42
 
38
43
  class << self
@@ -6,7 +6,8 @@ module RubyLokaliseApi
6
6
  # Returns all translation statuses for the given project
7
7
  #
8
8
  # @see https://developers.lokalise.com/reference/list-all-custom-translation-statuses
9
- # @return [RubyLokaliseApi::Collection::CustomTranslationStatus<RubyLokaliseApi::Resources::CustomTranslationStatus>]
9
+ # @return [RubyLokaliseApi::Collection::CustomTranslationStatus
10
+ # <RubyLokaliseApi::Resources::CustomTranslationStatus>]
10
11
  # @param project_id [String]
11
12
  # @param params [Hash]
12
13
  def translation_statuses(project_id, params = {})
@@ -13,7 +13,8 @@ module RubyLokaliseApi
13
13
  c_r RubyLokaliseApi::Collections::File, :all, project_id, params
14
14
  end
15
15
 
16
- # Exports translation files as .zip bundle, uploads them to Amazon S3 and returns a URL to the generated bundle. The URL is valid for a year
16
+ # Exports translation files as .zip bundle, uploads them to Amazon S3 and
17
+ # returns a URL to the generated bundle. The URL is valid for a year
17
18
  #
18
19
  # @see https://developers.lokalise.com/reference/download-files
19
20
  # @return [Hash]
@@ -70,7 +70,8 @@ module RubyLokaliseApi
70
70
  key ? key.snakecase : key
71
71
  end
72
72
 
73
- # Unify some resources' names (eg, `ProjectComment` and `KeyComment` have the same attributes which are stored under `comment`)
73
+ # Unify some resources' names (eg, `ProjectComment` and `KeyComment` have the same
74
+ # attributes which are stored under `comment`)
74
75
  #
75
76
  # @return [String]
76
77
  def unify(name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLokaliseApi
4
- VERSION = '6.2.0'
4
+ VERSION = '7.0.0'
5
5
  end
@@ -43,8 +43,14 @@ module RubyLokaliseApi
43
43
  @oauth2_client = nil
44
44
  end
45
45
 
46
- def auth_client(client_id, client_secret)
47
- RubyLokaliseApi::OAuth2::Auth.new client_id, client_secret
46
+ # Initializes a new Auth client to request OAuth 2 tokens
47
+ #
48
+ # @return [RubyLokaliseApi::OAuth2::Auth]
49
+ # @param client_id [String]
50
+ # @param client_secret [String]
51
+ # @param params [Hash]
52
+ def auth_client(client_id, client_secret, params = {})
53
+ RubyLokaliseApi::OAuth2::Auth.new client_id, client_secret, params
48
54
  end
49
55
  end
50
56
  end
@@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency 'rspec', '~> 3.6'
35
35
  spec.add_development_dependency 'rubocop', '~> 1.6'
36
36
  spec.add_development_dependency 'rubocop-performance', '~> 1.5'
37
+ spec.add_development_dependency 'rubocop-rake', '~> 0.6'
37
38
  spec.add_development_dependency 'rubocop-rspec', '~> 2.0'
38
39
  spec.add_development_dependency 'simplecov', '~> 0.16'
39
40
  spec.add_development_dependency 'vcr', '~> 6.0'
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: 6.2.0
4
+ version: 7.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Krukowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-02 00:00:00.000000000 Z
11
+ date: 2022-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -178,6 +178,20 @@ dependencies:
178
178
  - - "~>"
179
179
  - !ruby/object:Gem::Version
180
180
  version: '1.5'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rubocop-rake
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '0.6'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '0.6'
181
195
  - !ruby/object:Gem::Dependency
182
196
  name: rubocop-rspec
183
197
  requirement: !ruby/object:Gem::Requirement
@@ -283,8 +297,8 @@ files:
283
297
  - lib/ruby_lokalise_api/error.rb
284
298
  - lib/ruby_lokalise_api/json_handler.rb
285
299
  - lib/ruby_lokalise_api/oauth2/auth.rb
286
- - lib/ruby_lokalise_api/oauth2/connection.rb
287
- - lib/ruby_lokalise_api/oauth2/request.rb
300
+ - lib/ruby_lokalise_api/oauth2/refresh.rb
301
+ - lib/ruby_lokalise_api/oauth2/token.rb
288
302
  - lib/ruby_lokalise_api/oauth2_client.rb
289
303
  - lib/ruby_lokalise_api/request.rb
290
304
  - lib/ruby_lokalise_api/resources/base.rb
@@ -359,7 +373,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
359
373
  - !ruby/object:Gem::Version
360
374
  version: '0'
361
375
  requirements: []
362
- rubygems_version: 3.3.19
376
+ rubygems_version: 3.3.26
363
377
  signing_key:
364
378
  specification_version: 4
365
379
  summary: Ruby interface to the Lokalise API
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLokaliseApi
4
- module OAuth2
5
- module Connection
6
- BASE_URL = URI('https://app.lokalise.com/oauth2/')
7
-
8
- def connection
9
- Faraday.new(options) { |f| f.adapter Faraday.default_adapter }
10
- end
11
-
12
- private
13
-
14
- def options
15
- {
16
- headers: {
17
- accept: 'application/json',
18
- user_agent: "ruby-lokalise-api gem/#{RubyLokaliseApi::VERSION}"
19
- },
20
- url: BASE_URL.to_s
21
- }
22
- end
23
- end
24
- end
25
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLokaliseApi
4
- module OAuth2
5
- module Request
6
- include RubyLokaliseApi::BaseRequest
7
- include RubyLokaliseApi::OAuth2::Connection
8
-
9
- def post(path, params = {})
10
- respond_with connection.post(prepare(path), custom_dump(params))
11
- end
12
-
13
- private
14
-
15
- def respond_with(response)
16
- body = custom_load response.body
17
- status = response.status
18
- raise_on_error! status, body
19
- body
20
- end
21
- end
22
- end
23
- end