crowdin-api 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #
3
4
  # The Crowdin::Client library is used for interactions with a crowdin.com website.
4
5
  #
5
6
  # == Example
6
7
  #
7
- # require 'crowdin-api'
8
+ # require 'crowdin-api'
9
+ #
10
+ # crowdin = Crowdin::Client.new do |config|
11
+ # config.api_token = 'YOUR_API_TOKEN'
12
+ # end
8
13
  #
9
- # crowdin = Crowdin::Client.new do |config|
10
- # config.api_token = 'YOUR_API_TOKEN'
11
- # end
14
+ # crowdin.list_projects
12
15
  #
13
16
  module Crowdin
14
17
  class Client
@@ -24,59 +27,56 @@ module Crowdin
24
27
  include ApiResources::Storages
25
28
  include ApiResources::TranslationStatus
26
29
  include ApiResources::Translations
30
+ include ApiResources::Workflows
31
+ include ApiResources::SourceStrings
32
+
33
+ include Errors::ApiErrorsRaiser
34
+
35
+ attr_accessor :logger
27
36
 
28
37
  attr_reader :config
29
- attr_reader :options
30
38
  attr_reader :connection
39
+ attr_reader :options
31
40
 
32
- def initialize
33
- raise ArgumentError, 'block with configurations not given' unless block_given?
34
-
35
- @config = Crowdin::Configuration.new
36
- yield config
41
+ def initialize(&block)
42
+ build_configuration(&block)
37
43
 
38
44
  check_logger
45
+ check_rest_client_proxy
39
46
 
40
- set_rest_client_proxy!
41
-
42
- build_options
43
47
  build_connection
44
48
  end
45
49
 
46
50
  def log!(message)
47
- return true unless config.logger_enabled?
48
-
49
- logger.debug(message)
51
+ !config.logger_enabled? || logger.debug(message)
50
52
  end
51
53
 
52
- def logger=(logger)
53
- @logger = logger
54
- config.enable_logger = true
54
+ private
55
+
56
+ def build_configuration
57
+ @config = Crowdin::Configuration.new
58
+ yield config if block_given?
55
59
  end
56
60
 
57
- protected
61
+ def build_connection
62
+ @connection ||= ::RestClient::Resource.new(config.base_url, build_options)
63
+ end
58
64
 
59
65
  def build_options
60
- @options = config.options
61
- options[:headers] = config.headers
66
+ @options ||= config.options.merge(headers: config.headers)
62
67
  end
63
68
 
64
- def build_connection
65
- @connection = ::RestClient::Resource.new(config.base_url, options)
69
+ def set_default_logger
70
+ require 'logger'
71
+ @logger ||= Logger.new($stderr)
66
72
  end
67
73
 
68
- private
69
-
70
- def set_rest_client_proxy!
74
+ def check_rest_client_proxy
71
75
  ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false
72
76
  end
73
77
 
74
78
  def check_logger
75
- config.enable_logger ||= false
76
- end
77
-
78
- def logger
79
- @logger ||= Logger.new($stderr)
79
+ config.logger_enabled? ? set_default_logger : config.enable_logger = false
80
80
  end
81
81
  end
82
82
  end
@@ -9,6 +9,9 @@ module Crowdin
9
9
 
10
10
  attr_reader :target_api_url
11
11
 
12
+ alias logger_enabled? enable_logger
13
+ alias enterprise_mode? organization_domain
14
+
12
15
  def initialize
13
16
  @target_api_url = '/api/v2'
14
17
  end
@@ -31,11 +34,11 @@ module Crowdin
31
34
  end
32
35
 
33
36
  def base_url
34
- organization_domain ? "https://#{organization_domain}.api.crowdin.com" : 'https://api.crowdin.com'
35
- end
36
-
37
- def logger_enabled?
38
- enable_logger
37
+ if enterprise_mode?
38
+ organization_domain.include?('.com') ? organization_domain : "https://#{organization_domain}.api.crowdin.com"
39
+ else
40
+ 'https://api.crowdin.com'
41
+ end
39
42
  end
40
43
  end
41
44
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Crowdin
4
4
  class Client
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.0'
6
6
  end
7
7
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crowdin
4
+ module Errors
5
+ module ApiErrorsRaiser
6
+ def raise_only_for_enterprise_mode_error
7
+ raise(OnlyForEnterpriseMode, 'This method can be called only for Enterprise mode')
8
+ end
9
+
10
+ def raise_project_id_is_required_error
11
+ raise(ArgumentError, ':project_id is required in parameters or while Client initialization')
12
+ end
13
+
14
+ def raise_parameter_is_required_error(parameter)
15
+ raise(ArgumentError, ":#{parameter} is required")
16
+ end
17
+
18
+ # crowdin-console errors
19
+
20
+ def raise_api_token_is_required_error
21
+ raise(ArgumentError, '--api-token option is required')
22
+ end
23
+
24
+ def raise_organization_domain_is_required_error
25
+ raise(ArgumentError, '--organization-domain option is required for Enterprise mode')
26
+ end
27
+ end
28
+ end
29
+ end
@@ -2,19 +2,6 @@
2
2
 
3
3
  module Crowdin
4
4
  module Errors
5
- class Error < StandardError
6
- attr_reader :key, :error_code, :error_message
7
-
8
- def initialize(key, error_code, error_message)
9
- @key = key
10
- @error_code = error_code.to_i
11
- @error_message = error_message
12
- @message = "#{key} => #{error_code}: #{error_message}"
13
- end
14
-
15
- def to_s
16
- @message
17
- end
18
- end
5
+ class OnlyForEnterpriseMode < StandardError; end
19
6
  end
20
7
  end
@@ -9,7 +9,7 @@ module Crowdin
9
9
  @client = client
10
10
  @method = method
11
11
  @full_path = client.config.target_api_url + path
12
- @payload = Payload.new(method, query).perform
12
+ @payload = perform_payload(query)
13
13
  @headers = headers
14
14
  @destination = destination
15
15
  @errors = []
@@ -20,13 +20,15 @@ module Crowdin
20
20
  process_response!
21
21
  end
22
22
 
23
+ private
24
+
23
25
  def process_request!
24
26
  return @response = client.connection[@full_path].delete if delete_request?
25
27
  return @response = client.connection[@full_path].get(@payload) if get_request?
26
28
 
27
29
  client.connection[@full_path].send(@method, @payload, @headers) { |response, _, _| @response = response }
28
30
  rescue StandardError => error
29
- client.log! error.class
31
+ client.log! error
30
32
 
31
33
  @errors << "Something went wrong while proccessing request. Details - #{error.class}"
32
34
  end
@@ -36,14 +38,19 @@ module Crowdin
36
38
 
37
39
  begin
38
40
  if @response
39
- doc = JSON.parse(@response.body)
40
-
41
41
  client.log! "args: #{@response.request.args}"
42
- client.log! "body: #{doc}"
43
42
 
44
- data = fetch_response_data(doc)
43
+ if @response.body.empty?
44
+ @response.code
45
+ else
46
+ doc = JSON.parse(@response.body)
47
+
48
+ client.log! "body: #{doc}"
45
49
 
46
- @errors.any? ? fetch_errors : data
50
+ data = fetch_response_data(doc)
51
+
52
+ @errors.any? ? fetch_errors : data
53
+ end
47
54
  end
48
55
  rescue StandardError => error
49
56
  client.log! error
@@ -54,10 +61,10 @@ module Crowdin
54
61
  end
55
62
  end
56
63
 
57
- private
64
+ def perform_payload(query)
65
+ return query if query.is_a?(File)
58
66
 
59
- def fetch_errors
60
- @errors.join(';')
67
+ get_request? ? { params: fetch_cleared_query(query) } : fetch_cleared_query(query).to_json
61
68
  end
62
69
 
63
70
  def download_file(url)
@@ -71,14 +78,31 @@ module Crowdin
71
78
  @errors << "Something went wrong while downloading file. Details - #{error.class}"
72
79
  end
73
80
 
81
+ def fetch_errors
82
+ @errors.join(';')
83
+ end
84
+
74
85
  def fetch_response_data(doc)
75
- if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].scan(/response-content-disposition/)
86
+ if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].include?('response-content-disposition')
76
87
  download_file(doc['data']['url'])
77
88
  else
78
89
  doc
79
90
  end
80
91
  end
81
92
 
93
+ def fetch_cleared_query(query)
94
+ case query
95
+ when Array
96
+ query.map do |el|
97
+ el.reject { |_, value| value.nil? }
98
+ end.reject(&:empty?)
99
+ when Hash
100
+ query.reject { |_, value| value.nil? }
101
+ else
102
+ query
103
+ end
104
+ end
105
+
82
106
  def get_request?
83
107
  @method.eql?(:get)
84
108
  end
data/lib/crowdin-api.rb CHANGED
@@ -2,14 +2,13 @@
2
2
 
3
3
  # Libs
4
4
  require 'json'
5
- require 'logger'
6
5
  require 'open-uri'
7
6
  require 'rest-client'
8
7
 
9
8
  # Core modules
10
- require 'crowdin-api/core/payload'
11
- require 'crowdin-api/core/request'
12
9
  require 'crowdin-api/core/errors'
10
+ require 'crowdin-api/core/api_errors_raiser'
11
+ require 'crowdin-api/core/request'
13
12
 
14
13
  # Api modules
15
14
  require 'crowdin-api/api-resources/languages'
@@ -18,6 +17,8 @@ require 'crowdin-api/api-resources/source_files'
18
17
  require 'crowdin-api/api-resources/storages'
19
18
  require 'crowdin-api/api-resources/translation_status'
20
19
  require 'crowdin-api/api-resources/translations'
20
+ require 'crowdin-api/api-resources/workflows'
21
+ require 'crowdin-api/api-resources/source_strings'
21
22
 
22
23
  # Client
23
24
  require 'crowdin-api/client/version'
@@ -1,35 +1,72 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe 'Config instance' do
4
- it 'should have a project_id' do
4
+ before do
5
5
  @crowdin = Crowdin::Client.new do |config|
6
+ config.api_token = 'api_token'
6
7
  config.project_id = 1
7
8
  end
9
+ end
8
10
 
11
+ it 'show have a #project_id' do
9
12
  expect(@crowdin.config.project_id).to_not be_nil
10
13
  end
11
14
 
12
- it 'should have a api_token' do
13
- @crowdin = Crowdin::Client.new do |config|
14
- config.api_token = 'api_token'
15
+ it 'should have a #api_token' do
16
+ expect(@crowdin.config.api_token).to_not be_nil
17
+ end
18
+
19
+ it '#target_api_url should equal /api/v2 by default' do
20
+ expect(@crowdin.config.target_api_url).to eq('/api/v2')
21
+ end
22
+
23
+ describe '#logger_enabled?' do
24
+ it 'should be false by default' do
25
+ expect(@crowdin.config.logger_enabled?).to be_falsey
15
26
  end
16
27
 
17
- expect(@crowdin.config.api_token).to_not be_nil
28
+ it 'should equal specified argument' do
29
+ @crowdin = Crowdin::Client.new do |config|
30
+ config.enable_logger = true
31
+ end
32
+
33
+ expect(@crowdin.config.logger_enabled?).to be_truthy
34
+ end
18
35
  end
19
36
 
20
- it 'should have a base_url' do
21
- @crowdin = Crowdin::Client.new do |config|
22
- config.api_token = 'api_token'
37
+ describe '#enterprise_mode?' do
38
+ it 'should be false by default' do
39
+ expect(@crowdin.config.enterprise_mode?).to be_falsey
23
40
  end
24
41
 
25
- expect(@crowdin.config.base_url).to_not be_nil
42
+ it 'should equal specified arguments' do
43
+ @crowdin = Crowdin::Client.new do |config|
44
+ config.organization_domain = 'organization_domain'
45
+ end
46
+
47
+ expect(@crowdin.config.enterprise_mode?).to be_truthy
48
+ end
26
49
  end
27
50
 
28
- it 'should have a enable_logger set to false by default' do
29
- @crowdin = Crowdin::Client.new do |config|
30
- config.api_token = 'api_token'
51
+ describe '#base_url' do
52
+ it 'should equal https://api.crowdin.com by default' do
53
+ expect(@crowdin.config.base_url).to eq('https://api.crowdin.com')
31
54
  end
32
55
 
33
- expect(@crowdin.config.enable_logger).eql? false
56
+ it 'should equal specified organization domain' do
57
+ @crowdin = Crowdin::Client.new do |config|
58
+ config.organization_domain = 'organization_domain'
59
+ end
60
+
61
+ expect(@crowdin.config.base_url).to eq('https://organization_domain.api.crowdin.com')
62
+ end
63
+
64
+ it 'should equal full specified organization domain when user specify full url (with .com)' do
65
+ @crowdin = Crowdin::Client.new do |config|
66
+ config.organization_domain = 'organization_domain.com'
67
+ end
68
+
69
+ expect(@crowdin.config.base_url).to eq('organization_domain.com')
70
+ end
34
71
  end
35
72
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+
6
+ require 'codecov'
7
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
8
+
3
9
  require 'crowdin-api'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crowdin-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Crowdin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-29 00:00:00.000000000 Z
11
+ date: 2021-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: open-uri
@@ -70,6 +70,20 @@ dependencies:
70
70
  - - ">="
71
71
  - !ruby/object:Gem::Version
72
72
  version: 2.2.32
73
+ - !ruby/object:Gem::Dependency
74
+ name: codecov
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.6.0
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: 0.6.0
73
87
  - !ruby/object:Gem::Dependency
74
88
  name: rake
75
89
  requirement: !ruby/object:Gem::Requirement
@@ -146,9 +160,10 @@ dependencies:
146
160
  - - "~>"
147
161
  - !ruby/object:Gem::Version
148
162
  version: '3.14'
149
- description: Ruby Client for the Crowdin API. Documentation - https://support.crowdin.com/api/v2/.
163
+ description: The Crowdin Ruby Client is used to interact with the Crowdin API from
164
+ Ruby
150
165
  email:
151
- - support@crowdin.net
166
+ - support@crowdin.com
152
167
  executables:
153
168
  - crowdin-console
154
169
  extensions: []
@@ -173,19 +188,21 @@ files:
173
188
  - lib/crowdin-api/api-resources/languages.rb
174
189
  - lib/crowdin-api/api-resources/projects.rb
175
190
  - lib/crowdin-api/api-resources/source_files.rb
191
+ - lib/crowdin-api/api-resources/source_strings.rb
176
192
  - lib/crowdin-api/api-resources/storages.rb
177
193
  - lib/crowdin-api/api-resources/translation_status.rb
178
194
  - lib/crowdin-api/api-resources/translations.rb
195
+ - lib/crowdin-api/api-resources/workflows.rb
179
196
  - lib/crowdin-api/client/client.rb
180
197
  - lib/crowdin-api/client/configuration.rb
181
198
  - lib/crowdin-api/client/version.rb
199
+ - lib/crowdin-api/core/api_errors_raiser.rb
182
200
  - lib/crowdin-api/core/errors.rb
183
- - lib/crowdin-api/core/payload.rb
184
201
  - lib/crowdin-api/core/request.rb
185
202
  - spec/core/config-instance_spec.rb
186
203
  - spec/crowdin-api_spec.rb
187
204
  - spec/spec_helper.rb
188
- homepage: https://github.com/crowdin/crowdin-api/
205
+ homepage: https://github.com/crowdin/crowdin-api-client-ruby
189
206
  licenses:
190
207
  - MIT
191
208
  metadata: {}
@@ -197,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
214
  requirements:
198
215
  - - ">="
199
216
  - !ruby/object:Gem::Version
200
- version: '0'
217
+ version: '2.4'
201
218
  required_rubygems_version: !ruby/object:Gem::Requirement
202
219
  requirements:
203
220
  - - ">="
@@ -207,5 +224,5 @@ requirements: []
207
224
  rubygems_version: 3.0.3.1
208
225
  signing_key:
209
226
  specification_version: 4
210
- summary: Client library to manage translations on Crowdin
227
+ summary: Ruby Client for the Crowdin API
211
228
  test_files: []
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Crowdin
4
- module Web
5
- class Payload
6
- attr_reader :method, :query
7
-
8
- def initialize(method, query)
9
- @method = method
10
- @query = query
11
- end
12
-
13
- def perform
14
- return @query if @query.is_a?(File)
15
-
16
- @method.eql?(:get) ? { params: fetch_cleared_query } : fetch_cleared_query.to_json
17
- end
18
-
19
- private
20
-
21
- def fetch_cleared_query
22
- @query.reject { |_, value| value.nil? }
23
- end
24
- end
25
- end
26
- end