From 218b3a2cc9935e08eb599a0ecff9eb454189554f Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Fri, 3 Dec 2021 19:39:25 +0200 Subject: [PATCH 01/40] Add required_ruby_version to gemspec --- crowdin-api.gemspec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crowdin-api.gemspec b/crowdin-api.gemspec index 20a6bc9..59cee96 100644 --- a/crowdin-api.gemspec +++ b/crowdin-api.gemspec @@ -18,6 +18,8 @@ Gem::Specification.new do |gem| gem.bindir = 'bin' gem.executables << 'crowdin-console' + gem.required_ruby_version = '>= 2.4' + gem.add_runtime_dependency 'open-uri', '>= 0.1.0', '< 0.2.0' gem.add_runtime_dependency 'rest-client', '>= 2.0.0', '< 2.1.0' From b8e287b31bc876d6537facdbae85fa8b9fd9c4ab Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Sat, 4 Dec 2021 23:38:59 +0200 Subject: [PATCH 02/40] Support full organization domain --- lib/crowdin-api/client/configuration.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/crowdin-api/client/configuration.rb b/lib/crowdin-api/client/configuration.rb index 44b46b6..fd63549 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -31,7 +31,11 @@ def headers end def base_url - organization_domain ? "https://#{organization_domain}.api.crowdin.com" : 'https://api.crowdin.com' + if organization_domain + !!organization_domain.scan(/.com/) ? organization_domain : "https://#{organization_domain}.api.crowdin.com" + else + 'https://api.crowdin.com' + end end def logger_enabled? From a7414bba40dc4bbb20c9af28c04f46103cd1f171 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 00:54:41 +0200 Subject: [PATCH 03/40] Minor core(architecture) changes --- .rubocop_todo.yml | 16 +++++++++------ lib/crowdin-api.rb | 1 - lib/crowdin-api/client/client.rb | 24 +++++++++-------------- lib/crowdin-api/client/configuration.rb | 4 ++-- lib/crowdin-api/core/payload.rb | 26 ------------------------- lib/crowdin-api/core/request.rb | 20 ++++++++++++++----- 6 files changed, 36 insertions(+), 55 deletions(-) delete mode 100644 lib/crowdin-api/core/payload.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2ea13fd..2dc73c1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-11-29 03:21:36 UTC using RuboCop version 1.23.0. +# on 2021-12-05 22:52:45 UTC using RuboCop version 1.23.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -76,22 +76,21 @@ Naming/RescuedExceptionsVariableName: Exclude: - 'lib/crowdin-api/core/request.rb' -# Offense count: 7 +# Offense count: 4 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: separated, grouped Style/AccessorGrouping: Exclude: - - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' # Offense count: 1 # Cop supports --auto-correct. Style/CollectionCompact: Exclude: - - 'lib/crowdin-api/core/payload.rb' + - 'lib/crowdin-api/core/request.rb' -# Offense count: 11 +# Offense count: 10 # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: @@ -106,7 +105,6 @@ Style/Documentation: - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' - 'lib/crowdin-api/core/errors.rb' - - 'lib/crowdin-api/core/payload.rb' - 'lib/crowdin-api/core/request.rb' # Offense count: 1 @@ -116,3 +114,9 @@ Style/Documentation: Style/FrozenStringLiteralComment: Exclude: - 'bin/crowdin-console' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/RedundantSelf: + Exclude: + - 'lib/crowdin-api/client/configuration.rb' diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index 7130b5b..e9cf63e 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -7,7 +7,6 @@ require 'rest-client' # Core modules -require 'crowdin-api/core/payload' require 'crowdin-api/core/request' require 'crowdin-api/core/errors' diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 17ca3a6..f55f148 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -26,8 +26,7 @@ class Client include ApiResources::Translations attr_reader :config - attr_reader :options - attr_reader :connection + attr_writer :logger def initialize raise ArgumentError, 'block with configurations not given' unless block_given? @@ -39,7 +38,6 @@ def initialize set_rest_client_proxy! - build_options build_connection end @@ -49,26 +47,22 @@ def log!(message) logger.debug(message) end - def logger=(logger) - @logger = logger - config.enable_logger = true + def connection + @connection ||= build_connection end - protected + private - def build_options - @options = config.options - options[:headers] = config.headers + def set_rest_client_proxy! + ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false end def build_connection - @connection = ::RestClient::Resource.new(config.base_url, options) + ::RestClient::Resource.new(config.base_url, build_options) end - private - - def set_rest_client_proxy! - ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false + def build_options + config.options.merge(headers: config.headers) end def check_logger diff --git a/lib/crowdin-api/client/configuration.rb b/lib/crowdin-api/client/configuration.rb index fd63549..102d624 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -32,14 +32,14 @@ def headers def base_url if organization_domain - !!organization_domain.scan(/.com/) ? organization_domain : "https://#{organization_domain}.api.crowdin.com" + organization_domain.include?('.com') ? organization_domain : "https://#{organization_domain}.api.crowdin.com" else 'https://api.crowdin.com' end end def logger_enabled? - enable_logger + self.enable_logger end end end diff --git a/lib/crowdin-api/core/payload.rb b/lib/crowdin-api/core/payload.rb deleted file mode 100644 index e32acac..0000000 --- a/lib/crowdin-api/core/payload.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module Web - class Payload - attr_reader :method, :query - - def initialize(method, query) - @method = method - @query = query - end - - def perform - return @query if @query.is_a?(File) - - @method.eql?(:get) ? { params: fetch_cleared_query } : fetch_cleared_query.to_json - end - - private - - def fetch_cleared_query - @query.reject { |_, value| value.nil? } - end - end - end -end diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index ff9123e..b0385e1 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -9,7 +9,7 @@ def initialize(client, method, path, query = {}, headers = {}, destination = nil @client = client @method = method @full_path = client.config.target_api_url + path - @payload = Payload.new(method, query).perform + @payload = perform_payload(query) @headers = headers @destination = destination @errors = [] @@ -20,6 +20,8 @@ def perform process_response! end + private + def process_request! return @response = client.connection[@full_path].delete if delete_request? return @response = client.connection[@full_path].get(@payload) if get_request? @@ -54,10 +56,10 @@ def process_response! end end - private + def perform_payload(query) + return query if query.is_a?(File) - def fetch_errors - @errors.join(';') + get_request? ? { params: fetch_cleared_query(query) } : fetch_cleared_query(query).to_json end def download_file(url) @@ -71,14 +73,22 @@ def download_file(url) @errors << "Something went wrong while downloading file. Details - #{error.class}" end + def fetch_errors + @errors.join(';') + end + def fetch_response_data(doc) - if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].scan(/response-content-disposition/) + if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].include?('response-content-disposition') download_file(doc['data']['url']) else doc end end + def fetch_cleared_query(query) + query.reject { |_, value| value.nil? } + end + def get_request? @method.eql?(:get) end From 11bf2815bbad7c9ff8d43e2595ae236cbc804654 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 01:07:37 +0200 Subject: [PATCH 04/40] Handle response status codes when response have empty body --- lib/crowdin-api/core/request.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index b0385e1..4abdf4d 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -37,7 +37,7 @@ def process_response! return fetch_errors if @errors.any? begin - if @response + if @response && !@response.body.empty? doc = JSON.parse(@response.body) client.log! "args: #{@response.request.args}" @@ -46,6 +46,8 @@ def process_response! data = fetch_response_data(doc) @errors.any? ? fetch_errors : data + elsif @response + @response.code end rescue StandardError => error client.log! error From ea1acf1d8463297d803cd60f4911eb7c4406060f Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 01:19:52 +0200 Subject: [PATCH 05/40] Handle errors when response have empty body --- lib/crowdin-api/core/request.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index 4abdf4d..a5472fd 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -28,7 +28,7 @@ def process_request! client.connection[@full_path].send(@method, @payload, @headers) { |response, _, _| @response = response } rescue StandardError => error - client.log! error.class + client.log! error @errors << "Something went wrong while proccessing request. Details - #{error.class}" end @@ -37,17 +37,20 @@ def process_response! return fetch_errors if @errors.any? begin - if @response && !@response.body.empty? - doc = JSON.parse(@response.body) - + if @response client.log! "args: #{@response.request.args}" - client.log! "body: #{doc}" - data = fetch_response_data(doc) + if @response.body.empty? + @response.code + else + doc = JSON.parse(@response.body) + + client.log! "body: #{doc}" + + data = fetch_response_data(doc) - @errors.any? ? fetch_errors : data - elsif @response - @response.code + @errors.any? ? fetch_errors : data + end end rescue StandardError => error client.log! error From d072fffe442acfd534bbb544a6e642448a88a8b6 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 01:29:53 +0200 Subject: [PATCH 06/40] Open files with read option --- README.md | 4 ++-- lib/crowdin-api/api-resources/storages.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 395b8cd..35b2234 100644 --- a/README.md +++ b/README.md @@ -110,9 +110,9 @@ project = client.get_project(your_project_id) projects = client.list_projects(offset: 10, limit: 20) # Add Storage -adding_storage_response = crowdin.add_storage(File.open('YourFilename.extension')) +storage = crowdin.add_storage(File.open('YourFilename.extension', 'r')) # or you can specify only filename -adding_storage_response = crowdin.add_storage('YourFilename.extension') +storage = crowdin.add_storage('YourFilename.extension') # Download file filename = crowdin.download_file(your_destination, your_file_id, your_project_id) diff --git a/lib/crowdin-api/api-resources/storages.rb b/lib/crowdin-api/api-resources/storages.rb index d00b7ee..fe85281 100644 --- a/lib/crowdin-api/api-resources/storages.rb +++ b/lib/crowdin-api/api-resources/storages.rb @@ -34,14 +34,14 @@ def list_storages(query = {}) # # === Example # - # crowdin.add_storage(File.open('your_filename.extension')) + # crowdin.add_storage(File.open('your_filename.extension', 'r')) # or # crowdin.add_storage('your_filename.extension') # def add_storage(file = nil) file || raise(ArgumentError, ':file is required') - file = file.is_a?(File) ? file : File.open(file) + file = file.is_a?(File) ? file : File.open(file, 'r') request = Web::Request.new( self, From 2b34e7c76fcb471f346797b514394bf3ff18fa8b Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 01:49:37 +0200 Subject: [PATCH 07/40] Setup default logger when enabled --- lib/crowdin-api/client/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index f55f148..99dbbb5 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -66,7 +66,7 @@ def build_options end def check_logger - config.enable_logger ||= false + config.enable_logger ? logger : config.enable_logger = false end def logger From a4a20a53a62a26f1eeff5a46fe7b3a11af6d629b Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 01:53:26 +0200 Subject: [PATCH 08/40] Build connection method --- lib/crowdin-api/client/client.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 99dbbb5..4549160 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -26,6 +26,7 @@ class Client include ApiResources::Translations attr_reader :config + attr_reader :connection attr_writer :logger def initialize @@ -47,8 +48,8 @@ def log!(message) logger.debug(message) end - def connection - @connection ||= build_connection + def build_connection + @connection ||= ::RestClient::Resource.new(config.base_url, build_options) end private @@ -57,10 +58,6 @@ def set_rest_client_proxy! ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false end - def build_connection - ::RestClient::Resource.new(config.base_url, build_options) - end - def build_options config.options.merge(headers: config.headers) end From 66a821cacd6b9fec596c12a70483ac42e3d4ee4c Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 02:39:58 +0200 Subject: [PATCH 09/40] API Errors raiser. enterprise_mode? method --- lib/crowdin-api.rb | 2 +- lib/crowdin-api/api-resources/projects.rb | 12 +++++++++++- lib/crowdin-api/client/client.rb | 2 ++ lib/crowdin-api/client/configuration.rb | 6 +++++- lib/crowdin-api/core/api_errors_raiser.rb | 11 +++++++++++ lib/crowdin-api/core/errors.rb | 20 -------------------- 6 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 lib/crowdin-api/core/api_errors_raiser.rb delete mode 100644 lib/crowdin-api/core/errors.rb diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index e9cf63e..e6ba5f8 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -7,8 +7,8 @@ require 'rest-client' # Core modules +require 'crowdin-api/core/api_errors_raiser' require 'crowdin-api/core/request' -require 'crowdin-api/core/errors' # Api modules require 'crowdin-api/api-resources/languages' diff --git a/lib/crowdin-api/api-resources/projects.rb b/lib/crowdin-api/api-resources/projects.rb index 2a83812..93e92e6 100644 --- a/lib/crowdin-api/api-resources/projects.rb +++ b/lib/crowdin-api/api-resources/projects.rb @@ -62,9 +62,11 @@ def edit_project(project_id = nil, query = {}) request.perform end - # For Enterprise API only + # For Enterprise mode only def list_groups(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + request = Web::Request.new( self, :get, @@ -76,6 +78,8 @@ def list_groups(query = {}) end def add_group(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + request = Web::Request.new( self, :post, @@ -87,6 +91,8 @@ def add_group(query = {}) end def get_group(group_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise(ArgumentError, ':group_id is required') request = Web::Request.new( @@ -99,6 +105,8 @@ def get_group(group_id = nil) end def delete_group(group_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise(ArgumentError, ':group_id is required') request = Web::Request.new( @@ -111,6 +119,8 @@ def delete_group(group_id = nil) end def edit_group(group_id = nil, query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise(ArgumentError, ':group_id is required') request = Web::Request.new( diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 4549160..e66f3f0 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -25,6 +25,8 @@ class Client include ApiResources::TranslationStatus include ApiResources::Translations + include Errors::ApiErrorsRaiser + attr_reader :config attr_reader :connection attr_writer :logger diff --git a/lib/crowdin-api/client/configuration.rb b/lib/crowdin-api/client/configuration.rb index 102d624..a508ea9 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -31,13 +31,17 @@ def headers end def base_url - if organization_domain + if enterprise_mode? organization_domain.include?('.com') ? organization_domain : "https://#{organization_domain}.api.crowdin.com" else 'https://api.crowdin.com' end end + def enterprise_mode? + !!organization_domain + end + def logger_enabled? self.enable_logger end diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb new file mode 100644 index 0000000..749b239 --- /dev/null +++ b/lib/crowdin-api/core/api_errors_raiser.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Crowdin + module Errors + module ApiErrorsRaiser + def raise_only_for_enterprise_mode_error + raise(NoMethodError, 'This method can be called only for Enterprise mode') + end + end + end +end diff --git a/lib/crowdin-api/core/errors.rb b/lib/crowdin-api/core/errors.rb deleted file mode 100644 index d795f66..0000000 --- a/lib/crowdin-api/core/errors.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module Errors - class Error < StandardError - attr_reader :key, :error_code, :error_message - - def initialize(key, error_code, error_message) - @key = key - @error_code = error_code.to_i - @error_message = error_message - @message = "#{key} => #{error_code}: #{error_message}" - end - - def to_s - @message - end - end - end -end From f7018bf03829489d1371d0396c9e203deac280e3 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 02:56:43 +0200 Subject: [PATCH 10/40] Rubocop todo --- .rubocop_todo.yml | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2dc73c1..dc178b4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-12-05 22:52:45 UTC using RuboCop version 1.23.0. +# on 2021-12-06 00:55:09 UTC using RuboCop version 1.23.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -29,20 +29,15 @@ Layout/SpaceAroundOperators: Exclude: - 'crowdin-api.gemspec' -# Offense count: 1 -Lint/MissingSuper: - Exclude: - - 'lib/crowdin-api/core/errors.rb' - # Offense count: 2 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: - Max: 20 + Max: 23 # Offense count: 4 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. Metrics/MethodLength: - Max: 14 + Max: 18 # Offense count: 2 # Configuration parameters: CountComments, CountAsOne. @@ -69,6 +64,13 @@ Naming/FileName: - 'spec/core/config-instance_spec.rb' - 'spec/crowdin-api_spec.rb' +# Offense count: 1 +# Configuration parameters: EnforcedStyleForLeadingUnderscores. +# SupportedStylesForLeadingUnderscores: disallowed, required, optional +Naming/MemoizedInstanceVariableName: + Exclude: + - 'lib/crowdin-api/client/client.rb' + # Offense count: 3 # Cop supports --auto-correct. # Configuration parameters: PreferredName. @@ -76,12 +78,13 @@ Naming/RescuedExceptionsVariableName: Exclude: - 'lib/crowdin-api/core/request.rb' -# Offense count: 4 +# Offense count: 6 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: separated, grouped Style/AccessorGrouping: Exclude: + - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' # Offense count: 1 @@ -104,7 +107,7 @@ Style/Documentation: - 'lib/crowdin-api/api-resources/translations.rb' - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' - - 'lib/crowdin-api/core/errors.rb' + - 'lib/crowdin-api/core/api_errors_raiser.rb' - 'lib/crowdin-api/core/request.rb' # Offense count: 1 From 1a7bc9ca5b323b6fba9c4f9f61af8274087d2d9c Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 03:05:35 +0200 Subject: [PATCH 11/40] Codecov report. .gitignore changes --- .gitignore | 9 ++++++--- crowdin-api.gemspec | 1 + spec/spec_helper.rb | 6 ++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 639f196..98bc812 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,12 @@ -# Ruby logs +# Ignore Ruby logs .ruby-version .idea -# MacOS logs +# Ignore MacOS logs .DS_Store -# Versioning +# Ignore versioning file Gemfile.lock + +# Ignore tests coverage folder +coverage diff --git a/crowdin-api.gemspec b/crowdin-api.gemspec index 59cee96..f352fce 100644 --- a/crowdin-api.gemspec +++ b/crowdin-api.gemspec @@ -24,6 +24,7 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency 'rest-client', '>= 2.0.0', '< 2.1.0' gem.add_development_dependency 'bundler', '~> 2.2', '>= 2.2.32' + gem.add_development_dependency 'codecov', '~> 0.6.0' gem.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6' gem.add_development_dependency 'rspec', '~> 3.10' gem.add_development_dependency 'rubocop', '~> 1.23' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index aa77b54..294cbbc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,9 @@ # frozen_string_literal: true +require 'simplecov' +SimpleCov.start + +require 'codecov' +SimpleCov.formatter = SimpleCov::Formatter::Codecov + require 'crowdin-api' From 59688ff0b69808abd51c64c7d69f9a7e62970a36 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 12:24:09 +0200 Subject: [PATCH 12/40] Change target branch in codecov badge (Readme) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 35b2234..cb635f6 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ For more about Crowdin API v2 see the documentation: [![Gem](https://img.shields.io/gem/dtv/crowdin-api?cacheSeconds=1800)](https://rubygems.org/gems/crowdin-api) [![Test and Lint](https://github.com/crowdin/crowdin-api-client-ruby/actions/workflows/test-and-lint.yml/badge.svg)](https://github.com/crowdin/crowdin-api-client-ruby/actions/workflows/test-and-lint.yml) -[![codecov](https://codecov.io/gh/crowdin/crowdin-api-client-ruby/branch/master/graph/badge.svg?token=OJsyJwQbFM)](https://codecov.io/gh/crowdin/crowdin-api-client-ruby) +[![codecov](https://codecov.io/gh/crowdin/crowdin-api-client-ruby/branch/main/graph/badge.svg?token=OJsyJwQbFM)](https://codecov.io/gh/crowdin/crowdin-api-client-ruby) [![GitHub issues](https://img.shields.io/github/issues/crowdin/crowdin-api-client-ruby?cacheSeconds=1800)](https://github.com/crowdin/crowdin-api-client-ruby/issues) [![GitHub](https://img.shields.io/github/license/crowdin/crowdin-api-client-ruby?cacheSeconds=1800)](https://github.com/crowdin/crowdin-api-client-ruby/blob/main/LICENSE) From 37fd45a45990ef9f024e806b46598522a5892de8 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 6 Dec 2021 13:11:56 +0200 Subject: [PATCH 13/40] Readme changes --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cb635f6..3abaccd 100644 --- a/README.md +++ b/README.md @@ -101,13 +101,13 @@ To generate a new token in Crowdin Enterprise, follow these steps: project = crowdin.add_project(name: your_project_name, sourceLanguageId: your_language_id) # Get list of Projects -projects = client.list_projects +projects = crowdin.list_projects # Get specified project -project = client.get_project(your_project_id) +project = crowdin.get_project(your_project_id) # Get list of Projects with offset and limit -projects = client.list_projects(offset: 10, limit: 20) +projects = crowdin.list_projects(offset: 10, limit: 20) # Add Storage storage = crowdin.add_storage(File.open('YourFilename.extension', 'r')) @@ -135,7 +135,7 @@ bundle exec crowdin-console --enable-logger --api-token API_TOKEN --project-id P Or Crowdin Enterprise ```console -bundle exec crowdin-console --enable-logger --enterprise --api-token API_TOKEN --organization-domain YOUR_DOMAIN +bundle exec crowdin-console --enable-logger --enterprise --api-token API_TOKEN --organization-domain YOUR_DOMAIN --project-id PROJECT_ID ``` When execute you'll have IRB console with configured *@crowdin* instance From 014221d00c29d41b1b00a82a106818abdecff08f Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Tue, 7 Dec 2021 20:35:22 +0200 Subject: [PATCH 14/40] Config revisions | Config tests --- .rubocop_todo.yml | 18 ++++---- lib/crowdin-api/client/client.rb | 41 +++++++++--------- lib/crowdin-api/client/configuration.rb | 6 +-- spec/core/config-instance_spec.rb | 55 ++++++++++++++++++++----- 4 files changed, 77 insertions(+), 43 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index dc178b4..b16d87a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-12-06 00:55:09 UTC using RuboCop version 1.23.0. +# on 2021-12-07 18:29:55 UTC using RuboCop version 1.23.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -34,6 +34,12 @@ Layout/SpaceAroundOperators: Metrics/AbcSize: Max: 23 +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +# IgnoredMethods: refine +Metrics/BlockLength: + Max: 51 + # Offense count: 4 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. Metrics/MethodLength: @@ -64,7 +70,7 @@ Naming/FileName: - 'spec/core/config-instance_spec.rb' - 'spec/crowdin-api_spec.rb' -# Offense count: 1 +# Offense count: 3 # Configuration parameters: EnforcedStyleForLeadingUnderscores. # SupportedStylesForLeadingUnderscores: disallowed, required, optional Naming/MemoizedInstanceVariableName: @@ -78,7 +84,7 @@ Naming/RescuedExceptionsVariableName: Exclude: - 'lib/crowdin-api/core/request.rb' -# Offense count: 6 +# Offense count: 7 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: separated, grouped @@ -117,9 +123,3 @@ Style/Documentation: Style/FrozenStringLiteralComment: Exclude: - 'bin/crowdin-console' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/RedundantSelf: - Exclude: - - 'lib/crowdin-api/client/configuration.rb' diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index e66f3f0..2f190e3 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -27,49 +27,50 @@ class Client include Errors::ApiErrorsRaiser + attr_accessor :logger + attr_reader :config attr_reader :connection - attr_writer :logger - - def initialize - raise ArgumentError, 'block with configurations not given' unless block_given? + attr_reader :options - @config = Crowdin::Configuration.new - yield config + def initialize(&block) + build_configuration(&block) check_logger - - set_rest_client_proxy! + check_rest_client_proxy build_connection end def log!(message) - return true unless config.logger_enabled? + !config.logger_enabled? || logger.debug(message) + end - logger.debug(message) + private + + def build_configuration + @config = Crowdin::Configuration.new + yield config if block_given? end def build_connection @connection ||= ::RestClient::Resource.new(config.base_url, build_options) end - private - - def set_rest_client_proxy! - ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false + def build_options + @options ||= config.options.merge(headers: config.headers) end - def build_options - config.options.merge(headers: config.headers) + def set_default_logger + @logger ||= Logger.new($stderr) end - def check_logger - config.enable_logger ? logger : config.enable_logger = false + def check_rest_client_proxy + ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false end - def logger - @logger ||= Logger.new($stderr) + def check_logger + config.logger_enabled? ? set_default_logger : config.enable_logger = false end end end diff --git a/lib/crowdin-api/client/configuration.rb b/lib/crowdin-api/client/configuration.rb index a508ea9..896ee3e 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -9,6 +9,8 @@ class Configuration attr_reader :target_api_url + alias logger_enabled? enable_logger + def initialize @target_api_url = '/api/v2' end @@ -41,9 +43,5 @@ def base_url def enterprise_mode? !!organization_domain end - - def logger_enabled? - self.enable_logger - end end end diff --git a/spec/core/config-instance_spec.rb b/spec/core/config-instance_spec.rb index 98ed008..7cd0b50 100644 --- a/spec/core/config-instance_spec.rb +++ b/spec/core/config-instance_spec.rb @@ -1,35 +1,70 @@ # frozen_string_literal: true describe 'Config instance' do - it 'should have a project_id' do + before do @crowdin = Crowdin::Client.new do |config| + config.api_token = 'api_token' config.project_id = 1 end + end + it 'show have a #project_id' do expect(@crowdin.config.project_id).to_not be_nil end - it 'should have a api_token' do + it 'should have a #api_token' do + expect(@crowdin.config.api_token).to_not be_nil + end + + it 'should have a #base_url' do + expect(@crowdin.config.base_url).to_not be_nil + end + + it '#target_api_url should equal /api/v2 by default' do + expect(@crowdin.config.target_api_url).eql? '/api/v2' + end + + it '.logger_enabled? should be false by default' do + expect(@crowdin.config.logger_enabled?).eql? false + end + + it '.logger_enabled?' do @crowdin = Crowdin::Client.new do |config| - config.api_token = 'api_token' + config.enable_logger = true end - expect(@crowdin.config.api_token).to_not be_nil + expect(@crowdin.config.logger_enabled?).eql? true + end + + it '.enterprise_mode? should be false by default' do + expect(@crowdin.config.enterprise_mode?).eql? false end - it 'should have a base_url' do + it '.enterprise_mode?' do @crowdin = Crowdin::Client.new do |config| - config.api_token = 'api_token' + config.organization_domain = 'organization_domain' end - expect(@crowdin.config.base_url).to_not be_nil + expect(@crowdin.config.enterprise_mode?).eql? true + end + + it '.base_url should equal https://api.crowdin.com by default' do + expect(@crowdin.config.base_url).eql? 'https://api.crowdin.com' + end + + it '.base_url should equal specified organization domain' do + @crowdin = Crowdin::Client.new do |config| + config.organization_domain = 'organization_domain' + end + + expect(@crowdin.config.base_url).eql? 'https://organization_domain.api.crowdin.com' end - it 'should have a enable_logger set to false by default' do + it '.base_url should equal full specified organization domain when user specify full url (with .com)' do @crowdin = Crowdin::Client.new do |config| - config.api_token = 'api_token' + config.organization_domain = 'organization_domain.com' end - expect(@crowdin.config.enable_logger).eql? false + expect(@crowdin.config.base_url).eql? 'organization_domain.com' end end From 5f9d40807211d3b4271d8b1116056c76c3307100 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Tue, 7 Dec 2021 20:56:43 +0200 Subject: [PATCH 15/40] Fix when request have array as body --- lib/crowdin-api/core/request.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index a5472fd..72b0b54 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -91,7 +91,16 @@ def fetch_response_data(doc) end def fetch_cleared_query(query) - query.reject { |_, value| value.nil? } + case query + when Array + query.each do |el| + el.reject! { |_, value| value.nil? } + end + when Hash + query.reject { |_, value| value.nil? } + else + query + end end def get_request? From 1ea68b8b11c93f2b43b516f3c4ebefc8215af363 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Wed, 8 Dec 2021 17:48:54 +0200 Subject: [PATCH 16/40] Config instance tests --- spec/core/config-instance_spec.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/spec/core/config-instance_spec.rb b/spec/core/config-instance_spec.rb index 7cd0b50..b835ec4 100644 --- a/spec/core/config-instance_spec.rb +++ b/spec/core/config-instance_spec.rb @@ -21,50 +21,50 @@ end it '#target_api_url should equal /api/v2 by default' do - expect(@crowdin.config.target_api_url).eql? '/api/v2' + expect(@crowdin.config.target_api_url).to eq('/api/v2') end - it '.logger_enabled? should be false by default' do - expect(@crowdin.config.logger_enabled?).eql? false + it '#logger_enabled? should be false by default' do + expect(@crowdin.config.logger_enabled?).to be_falsey end - it '.logger_enabled?' do + it '#logger_enabled? should equal specified argument' do @crowdin = Crowdin::Client.new do |config| config.enable_logger = true end - expect(@crowdin.config.logger_enabled?).eql? true + expect(@crowdin.config.logger_enabled?).to be_truthy end - it '.enterprise_mode? should be false by default' do - expect(@crowdin.config.enterprise_mode?).eql? false + it '#enterprise_mode? should be false by default' do + expect(@crowdin.config.enterprise_mode?).to be_falsey end - it '.enterprise_mode?' do + it '#enterprise_mode? should equal specified arguments' do @crowdin = Crowdin::Client.new do |config| config.organization_domain = 'organization_domain' end - expect(@crowdin.config.enterprise_mode?).eql? true + expect(@crowdin.config.enterprise_mode?).to be_truthy end - it '.base_url should equal https://api.crowdin.com by default' do - expect(@crowdin.config.base_url).eql? 'https://api.crowdin.com' + it '#base_url should equal https://api.crowdin.com by default' do + expect(@crowdin.config.base_url).to eql('https://api.crowdin.com') end - it '.base_url should equal specified organization domain' do + it '#base_url should equal specified organization domain' do @crowdin = Crowdin::Client.new do |config| config.organization_domain = 'organization_domain' end - expect(@crowdin.config.base_url).eql? 'https://organization_domain.api.crowdin.com' + expect(@crowdin.config.base_url).to eql('https://organization_domain.api.crowdin.com') end - it '.base_url should equal full specified organization domain when user specify full url (with .com)' do + it '#base_url should equal full specified organization domain when user specify full url (with .com)' do @crowdin = Crowdin::Client.new do |config| config.organization_domain = 'organization_domain.com' end - expect(@crowdin.config.base_url).eql? 'organization_domain.com' + expect(@crowdin.config.base_url).to eql('organization_domain.com') end end From d09a1f484bbfca080254eafd774626e02327599e Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Thu, 9 Dec 2021 17:02:46 +0200 Subject: [PATCH 17/40] Tests revisions --- .rubocop_todo.yml | 6 +-- spec/core/config-instance_spec.rb | 64 ++++++++++++++++--------------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b16d87a..1785d84 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-12-07 18:29:55 UTC using RuboCop version 1.23.0. +# on 2021-12-09 14:59:50 UTC using RuboCop version 1.23.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -38,7 +38,7 @@ Metrics/AbcSize: # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. # IgnoredMethods: refine Metrics/BlockLength: - Max: 51 + Max: 54 # Offense count: 4 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. @@ -93,7 +93,7 @@ Style/AccessorGrouping: - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' -# Offense count: 1 +# Offense count: 2 # Cop supports --auto-correct. Style/CollectionCompact: Exclude: diff --git a/spec/core/config-instance_spec.rb b/spec/core/config-instance_spec.rb index b835ec4..7719f87 100644 --- a/spec/core/config-instance_spec.rb +++ b/spec/core/config-instance_spec.rb @@ -16,55 +16,57 @@ expect(@crowdin.config.api_token).to_not be_nil end - it 'should have a #base_url' do - expect(@crowdin.config.base_url).to_not be_nil - end - it '#target_api_url should equal /api/v2 by default' do expect(@crowdin.config.target_api_url).to eq('/api/v2') end - it '#logger_enabled? should be false by default' do - expect(@crowdin.config.logger_enabled?).to be_falsey - end - - it '#logger_enabled? should equal specified argument' do - @crowdin = Crowdin::Client.new do |config| - config.enable_logger = true + describe '#logger_enabled?' do + it 'should be false by default' do + expect(@crowdin.config.logger_enabled?).to be_falsey end - expect(@crowdin.config.logger_enabled?).to be_truthy - end + it 'should equal specified argument' do + @crowdin = Crowdin::Client.new do |config| + config.enable_logger = true + end - it '#enterprise_mode? should be false by default' do - expect(@crowdin.config.enterprise_mode?).to be_falsey + expect(@crowdin.config.logger_enabled?).to be_truthy + end end - it '#enterprise_mode? should equal specified arguments' do - @crowdin = Crowdin::Client.new do |config| - config.organization_domain = 'organization_domain' + describe '#enterprise_mode?' do + it 'should be false by default' do + expect(@crowdin.config.enterprise_mode?).to be_falsey end - expect(@crowdin.config.enterprise_mode?).to be_truthy - end + it 'should equal specified arguments' do + @crowdin = Crowdin::Client.new do |config| + config.organization_domain = 'organization_domain' + end - it '#base_url should equal https://api.crowdin.com by default' do - expect(@crowdin.config.base_url).to eql('https://api.crowdin.com') + expect(@crowdin.config.enterprise_mode?).to be_truthy + end end - it '#base_url should equal specified organization domain' do - @crowdin = Crowdin::Client.new do |config| - config.organization_domain = 'organization_domain' + describe '#base_url' do + it 'should equal https://api.crowdin.com by default' do + expect(@crowdin.config.base_url).to eq('https://api.crowdin.com') end - expect(@crowdin.config.base_url).to eql('https://organization_domain.api.crowdin.com') - end + it 'should equal specified organization domain' do + @crowdin = Crowdin::Client.new do |config| + config.organization_domain = 'organization_domain' + end - it '#base_url should equal full specified organization domain when user specify full url (with .com)' do - @crowdin = Crowdin::Client.new do |config| - config.organization_domain = 'organization_domain.com' + expect(@crowdin.config.base_url).to eq('https://organization_domain.api.crowdin.com') end - expect(@crowdin.config.base_url).to eql('organization_domain.com') + it 'should equal full specified organization domain when user specify full url (with .com)' do + @crowdin = Crowdin::Client.new do |config| + config.organization_domain = 'organization_domain.com' + end + + expect(@crowdin.config.base_url).to eq('organization_domain.com') + end end end From f22fe82d0c794b89762817c7129aa5dd0717fc81 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Sun, 12 Dec 2021 21:09:27 +0200 Subject: [PATCH 18/40] Bump Client version to 1.1.0 --- README.md | 2 +- lib/crowdin-api/client/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3abaccd..3e19beb 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ For more about Crowdin API v2 see the documentation: Add this line to your application's Gemfile: ```gemfile -gem 'crowdin-api', '~> 1.0.0' +gem 'crowdin-api', '~> 1.1.0' ``` And then execute: diff --git a/lib/crowdin-api/client/version.rb b/lib/crowdin-api/client/version.rb index bd5d830..331aab9 100644 --- a/lib/crowdin-api/client/version.rb +++ b/lib/crowdin-api/client/version.rb @@ -2,6 +2,6 @@ module Crowdin class Client - VERSION = '1.0.0' + VERSION = '1.1.0' end end From 258316fdd7eb05284b162c10e000a3523db0575f Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 00:54:24 +0200 Subject: [PATCH 19/40] Readme updates --- README.md | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3e19beb..986dc17 100644 --- a/README.md +++ b/README.md @@ -65,25 +65,31 @@ gem install crowdin-api ```ruby require 'crowdin-api' -# Create a new Crowdin Client object. +# Initialize a new Client instance with config options crowdin = Crowdin::Client.new do |config| config.api_token = 'YourApiToken' end -# or you can create Enterprise instance by specify your organization_domain + +# Or you can intialize Enterprise Client instance by specifying your organization_domain in config options crowdin = Crowdin::Client.new do |config| config.api_token = 'YourEnterpriseApiToken' config.organization_domain = 'YourOrganizationDomain' end +# Note: we use full specified organization domain if that includes .com +# config.organization_domain = your_domain -> https://your_domain.api.crowdin.com +# config.organization_domain = your_domain.com -> your_domain.com -# Also you can specify project_id to handle it in methods - -# All Crowdin Client config options: +# All supported Crowdin Client config options now: crowdin = Crowdin::Client.new do |config| config.api_token = 'YourApiToken' # [String] required config.organization_domain = 'YourOrganizationDomain' # [String] optional config.project_id = 'YourProjectId' # [Integer] nil by default config.enable_logger = true # [Boolean] false by default end +# Note: Client will initialize default Logger instance if you have specify enable_logger to true, +# you can change it by crowdin.logger = YourLogger + +# Also you can specify proxy by adding it to ENV['http_proxy'] before Client initialization ``` To generate a new token in Crowdin, follow these steps: @@ -94,7 +100,7 @@ To generate a new token in Crowdin Enterprise, follow these steps: - Go to *Account Settings* > *Access tokens* tab and click *New token*. - Specify *Token Name*, select *Scopes* and *Projects*, click *Create*. -### How to call methods +### Usage ```ruby # Create Project @@ -129,14 +135,15 @@ file_revisions = crowdin.list_file_revisions(your_file_id, { limit: 10, project_ The Crowdin Ruby client support crowdin-console, where you can test endpoints easier ```console -bundle exec crowdin-console --enable-logger --api-token API_TOKEN --project-id PROJECT_ID +$ bundle exec crowdin-console --enable-logger --api-token API_TOKEN --project-id PROJECT_ID ``` Or Crowdin Enterprise ```console -bundle exec crowdin-console --enable-logger --enterprise --api-token API_TOKEN --organization-domain YOUR_DOMAIN --project-id PROJECT_ID +$ bundle exec crowdin-console --enable-logger --enterprise --api-token API_TOKEN --organization-domain YOUR_DOMAIN --project-id PROJECT_ID ``` +Note: you can specify full organization domain by adding .com When execute you'll have IRB console with configured *@crowdin* instance From 0ea7187a671edbfb4ce9c909a33ef04498151599 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 01:44:58 +0200 Subject: [PATCH 20/40] Corrections --- README.md | 6 +++--- bin/crowdin-console | 13 ++++++++----- lib/crowdin-api.rb | 1 - lib/crowdin-api/api-resources/source_files.rb | 4 ++-- lib/crowdin-api/client/client.rb | 12 ++++++++---- lib/crowdin-api/client/configuration.rb | 7 ++----- lib/crowdin-api/core/api_errors_raiser.rb | 13 +++++++++++++ 7 files changed, 36 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 986dc17..14f45f0 100644 --- a/README.md +++ b/README.md @@ -109,12 +109,12 @@ project = crowdin.add_project(name: your_project_name, sourceLanguageId: your_la # Get list of Projects projects = crowdin.list_projects -# Get specified project -project = crowdin.get_project(your_project_id) - # Get list of Projects with offset and limit projects = crowdin.list_projects(offset: 10, limit: 20) +# Get specified project +project = crowdin.get_project(your_project_id) + # Add Storage storage = crowdin.add_storage(File.open('YourFilename.extension', 'r')) # or you can specify only filename diff --git a/bin/crowdin-console b/bin/crowdin-console index 0f713ba..2116b76 100755 --- a/bin/crowdin-console +++ b/bin/crowdin-console @@ -1,10 +1,10 @@ #!/usr/bin/env ruby -require 'bundler/setup' require 'crowdin-api' require 'optparse' -# You can specify your arguments when execute this script +# +# You can specify your arguments while executing this script # # Type bundle exec crowdin-console --help for more available arguments # @@ -12,9 +12,12 @@ require 'optparse' # # bundle exec crowdin-console --enable-logger --api-token YOUR_API_TOKEN --project-id YOUR_PROJECT_ID # -# or Enterpise API +# or Enterpise API: # # bundle exec crowdin-console --enterprise --api-token YOUR_ENTERPRISE_API_TOKEN --organization-domain YOUR_DOMAIN +# + +include Crowdin::Errors::ApiErrorsRaiser options = {} @@ -28,10 +31,10 @@ OptionParser.new do |opts| opts.on('--enable-logger', 'Enable logger') { |v| options[:enable_logger] = v } end.parse! -options[:api_token] || raise(ArgumentError, '--api-token argument is required') +options[:api_token] || raise_api_token_is_required_error if options[:enterprise] - options[:organization_domain] || raise(ArgumentError, '--organization-domain option is required for Enterprise mode') + options[:organization_domain] || raise_organization_domain_is_required_error @crowdin = Crowdin::Client.new do |config| config.api_token = options[:api_token] diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index e6ba5f8..f7d7e85 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -2,7 +2,6 @@ # Libs require 'json' -require 'logger' require 'open-uri' require 'rest-client' diff --git a/lib/crowdin-api/api-resources/source_files.rb b/lib/crowdin-api/api-resources/source_files.rb index 7d42421..6f568f9 100644 --- a/lib/crowdin-api/api-resources/source_files.rb +++ b/lib/crowdin-api/api-resources/source_files.rb @@ -166,7 +166,7 @@ def list_files(query = {}, project_id = config.project_id) # # crowdin.add_file(storage_id: your_storage_id, name: 'your_filename') # - # or you can use the specified project_id + # or you can specify project_id # # crowdin.add_file({}, your_project_id) # @@ -275,7 +275,7 @@ def download_file(destination = nil, file_id = nil, project_id = config.project_ # # crowdin.list_file_revisions(your_file_id, { limit: your_value }) # - # or you can use the specified project_id + # or you can cpecify project_id # # crowdin.list_file_revisions(your_file_id, { project_id: your_project_id ..}) # diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 2f190e3..6e3dda4 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -1,14 +1,17 @@ # frozen_string_literal: true +# # The Crowdin::Client library is used for interactions with a crowdin.com website. # # == Example # -# require 'crowdin-api' +# require 'crowdin-api' +# +# crowdin = Crowdin::Client.new do |config| +# config.api_token = 'YOUR_API_TOKEN' +# end # -# crowdin = Crowdin::Client.new do |config| -# config.api_token = 'YOUR_API_TOKEN' -# end +# crowdin.list_projects # module Crowdin class Client @@ -62,6 +65,7 @@ def build_options end def set_default_logger + require 'logger' @logger ||= Logger.new($stderr) end diff --git a/lib/crowdin-api/client/configuration.rb b/lib/crowdin-api/client/configuration.rb index 896ee3e..064b0b7 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -9,7 +9,8 @@ class Configuration attr_reader :target_api_url - alias logger_enabled? enable_logger + alias logger_enabled? enable_logger + alias enterprise_mode? organization_domain def initialize @target_api_url = '/api/v2' @@ -39,9 +40,5 @@ def base_url 'https://api.crowdin.com' end end - - def enterprise_mode? - !!organization_domain - end end end diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb index 749b239..c06bc7a 100644 --- a/lib/crowdin-api/core/api_errors_raiser.rb +++ b/lib/crowdin-api/core/api_errors_raiser.rb @@ -6,6 +6,19 @@ module ApiErrorsRaiser def raise_only_for_enterprise_mode_error raise(NoMethodError, 'This method can be called only for Enterprise mode') end + + def raise_project_id_is_required_error + raise(ArgumentError, 'For this request :project_id is required in parameters or while Client initialization') + end + + # crowdin-console errors + def raise_api_token_is_required_error + raise(ArgumentError, '--api-token argument is required') + end + + def raise_organization_domain_is_required_error + raise(ArgumentError, '--organization-domain option is required for Enterprise mode') + end end end end From 9960eee784e455bfc23c0468a75c51df94f1ce0d Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 01:59:21 +0200 Subject: [PATCH 21/40] Typo in project_id (crowdin-console options) --- bin/crowdin-console | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/crowdin-console b/bin/crowdin-console index 2116b76..5b48d21 100755 --- a/bin/crowdin-console +++ b/bin/crowdin-console @@ -27,7 +27,7 @@ OptionParser.new do |opts| opts.on('--enterprise', 'Enterprise API') { |v| options[:enterprise] = v } opts.on('--api-token TOKEN', 'API Token') { |v| options[:api_token] = v } opts.on('--organization-domain DOMAIN', 'Organization Domain') { |v| options[:organization_domain] = v } - opts.on('--prohject-id ID', 'Project ID') { |v| options[:project_id] = v } + opts.on('--project-id ID', 'Project ID') { |v| options[:project_id] = v } opts.on('--enable-logger', 'Enable logger') { |v| options[:enable_logger] = v } end.parse! From ca88d1ddfd04e112716bbda69218aafa296418be Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 02:28:27 +0200 Subject: [PATCH 22/40] raise_project_id_is_required_error --- lib/crowdin-api/api-resources/source_files.rb | 40 +++++++++---------- .../api-resources/translation_status.rb | 12 +++--- lib/crowdin-api/api-resources/translations.rb | 22 +++++----- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/crowdin-api/api-resources/source_files.rb b/lib/crowdin-api/api-resources/source_files.rb index 6f568f9..7b4c748 100644 --- a/lib/crowdin-api/api-resources/source_files.rb +++ b/lib/crowdin-api/api-resources/source_files.rb @@ -4,7 +4,7 @@ module Crowdin module ApiResources module SourceFiles def list_branches(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -17,7 +17,7 @@ def list_branches(query = {}, project_id = config.project_id) end def add_branch(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -31,7 +31,7 @@ def add_branch(query = {}, project_id = config.project_id) def get_branch(branch_id = nil, project_id = config.project_id) branch_id || raise(ArgumentError, ':branch_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -44,7 +44,7 @@ def get_branch(branch_id = nil, project_id = config.project_id) def delete_branch(branch_id = nil, project_id = config.project_id) branch_id || raise(ArgumentError, ':branch_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -59,7 +59,7 @@ def edit_branch(branch_id = nil, query = {}) project_id = query[:project_id] || config.project_id branch_id || raise(ArgumentError, ':branch_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -72,7 +72,7 @@ def edit_branch(branch_id = nil, query = {}) end def list_directories(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -85,7 +85,7 @@ def list_directories(query = {}, project_id = config.project_id) end def add_directory(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -99,7 +99,7 @@ def add_directory(query = {}, project_id = config.project_id) def get_directory(directory_id = nil, project_id = config.project_id) directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -112,7 +112,7 @@ def get_directory(directory_id = nil, project_id = config.project_id) def delete_directory(directory_id = nil, project_id = config.project_id) directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -127,7 +127,7 @@ def edit_directory(directory_id = nil, query = {}) project_id = query[:project_id] || config.project_id directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -140,7 +140,7 @@ def edit_directory(directory_id = nil, query = {}) end def list_files(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -171,7 +171,7 @@ def list_files(query = {}, project_id = config.project_id) # crowdin.add_file({}, your_project_id) # def add_file(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -185,7 +185,7 @@ def add_file(query = {}, project_id = config.project_id) def get_file(file_id = nil, project_id = config.project_id) file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -200,7 +200,7 @@ def update_or_restore_file(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -214,7 +214,7 @@ def update_or_restore_file(file_id = nil, query = {}) def delete_file(file_id = nil, project_id = config.project_id) file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -229,7 +229,7 @@ def edit_file(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -244,7 +244,7 @@ def edit_file(file_id = nil, query = {}) def download_file(destination = nil, file_id = nil, project_id = config.project_id) destination || raise(ArgumentError, ':destination is required for downlaods') file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -277,13 +277,13 @@ def download_file(destination = nil, file_id = nil, project_id = config.project_ # # or you can cpecify project_id # - # crowdin.list_file_revisions(your_file_id, { project_id: your_project_id ..}) + # crowdin.list_file_revisions(your_file_id, { project_id: your_project_id }) # def list_file_revisions(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -298,7 +298,7 @@ def list_file_revisions(file_id = nil, query = {}) def get_file_revision(file_id = nil, revision_id = nil, project_id = config.project_id) file_id || raise(ArgumentError, ':file_id is required') revision_id || raise(ArgumentError, ':revision_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, diff --git a/lib/crowdin-api/api-resources/translation_status.rb b/lib/crowdin-api/api-resources/translation_status.rb index 26b2fef..02665f9 100644 --- a/lib/crowdin-api/api-resources/translation_status.rb +++ b/lib/crowdin-api/api-resources/translation_status.rb @@ -7,7 +7,7 @@ def get_branch_progress(branch_id = nil, query = {}) project_id = query[:project_id] || config.project_id branch_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -23,7 +23,7 @@ def get_directory_progress(directory_id = nil, query = {}) project_id = query[:project_id] || config.project_id directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -39,7 +39,7 @@ def get_file_progress(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -55,7 +55,7 @@ def get_language_progress(language_id = nil, query = {}) project_id = query[:project_id] || config.project_id language_id || raise(ArgumentError, ':language_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -68,7 +68,7 @@ def get_language_progress(language_id = nil, query = {}) end def get_project_progress(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -81,7 +81,7 @@ def get_project_progress(query = {}, project_id = config.project_id) end def get_qa_progress(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, diff --git a/lib/crowdin-api/api-resources/translations.rb b/lib/crowdin-api/api-resources/translations.rb index a68f188..3221392 100644 --- a/lib/crowdin-api/api-resources/translations.rb +++ b/lib/crowdin-api/api-resources/translations.rb @@ -5,7 +5,7 @@ module ApiResources module Translations def pre_translation_status(pre_translation_id = nil, project_id = config.project_id) pre_translation_id || raise(ArgumentError, ':pre_translation_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -17,7 +17,7 @@ def pre_translation_status(pre_translation_id = nil, project_id = config.project end def apply_pre_translation(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -33,7 +33,7 @@ def build_project_directory_translation(directory_id = nil, query = {}) project_id = query[:project_id] || config.project_id directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -49,7 +49,7 @@ def build_project_file_translation(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error headers = query[:eTag] ? { 'If-None-Match' => query[:eTag] } : {} @@ -65,7 +65,7 @@ def build_project_file_translation(file_id = nil, query = {}) end def list_project_builds(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -78,7 +78,7 @@ def list_project_builds(query = {}, project_id = config.project_id) end def build_project_translation(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -94,7 +94,7 @@ def upload_translations(language_id = nil, query = {}) project_id = query[:project_id] || config.project_id language_id || raise(ArgumentError, ':language_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -109,7 +109,7 @@ def upload_translations(language_id = nil, query = {}) def download_project_translations(destinaton = nil, build_id = nil, project_id = config.project_id) destinaton || raise(ArgumentError, ':destination is required for downlaods') build_id || raise(ArgumentError, ':build_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -125,7 +125,7 @@ def download_project_translations(destinaton = nil, build_id = nil, project_id = def check_project_build_status(build_id = nil, project_id = config.project_id) build_id || raise(ArgumentError, ':build_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -138,7 +138,7 @@ def check_project_build_status(build_id = nil, project_id = config.project_id) def cancel_build(build_id = nil, project_id = config.project_id) build_id || raise(ArgumentError, ':build_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -150,7 +150,7 @@ def cancel_build(build_id = nil, project_id = config.project_id) end def export_project_translation(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, From db8f02efe03d91acdb68028ad9f2be57ff090d49 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 02:34:54 +0200 Subject: [PATCH 23/40] Languages module required parameters --- lib/crowdin-api/api-resources/languages.rb | 6 +++--- lib/crowdin-api/core/api_errors_raiser.rb | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/crowdin-api/api-resources/languages.rb b/lib/crowdin-api/api-resources/languages.rb index 4349e76..f910371 100644 --- a/lib/crowdin-api/api-resources/languages.rb +++ b/lib/crowdin-api/api-resources/languages.rb @@ -41,7 +41,7 @@ def add_custom_language(query = {}) end def get_language(language_id = nil) - language_id || raise(ArgumentError, ':language_id is required') + language_id || raise_parameter_is_required_error(:language_id) request = Web::Request.new( self, @@ -53,7 +53,7 @@ def get_language(language_id = nil) end def delete_custom_language(language_id = nil) - language_id || raise(ArgumentError, ':language_id is required') + language_id || raise_parameter_is_required_error(:language_id) request = Web::Request.new( self, @@ -65,7 +65,7 @@ def delete_custom_language(language_id = nil) end def edit_custom_language(language_id = nil) - language_id || raise(ArgumentError, ':language_id is required') + language_id || raise_parameter_is_required_error(:language_id) request = Web::Request.new( self, diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb index c06bc7a..f9a41a2 100644 --- a/lib/crowdin-api/core/api_errors_raiser.rb +++ b/lib/crowdin-api/core/api_errors_raiser.rb @@ -8,7 +8,11 @@ def raise_only_for_enterprise_mode_error end def raise_project_id_is_required_error - raise(ArgumentError, 'For this request :project_id is required in parameters or while Client initialization') + raise(ArgumentError, ':project_id is required in parameters or while Client initialization') + end + + def raise_parameter_is_required_error(parameter) + raise(ArgumentError, ":#{parameter} is required") end # crowdin-console errors From 956d113d69a44b0e20ef49810c0e1fcb10434c42 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 02:38:53 +0200 Subject: [PATCH 24/40] Projects module required parameters --- lib/crowdin-api/api-resources/projects.rb | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/crowdin-api/api-resources/projects.rb b/lib/crowdin-api/api-resources/projects.rb index 93e92e6..e004192 100644 --- a/lib/crowdin-api/api-resources/projects.rb +++ b/lib/crowdin-api/api-resources/projects.rb @@ -26,7 +26,7 @@ def add_project(query = {}) end def get_project(project_id = nil) - project_id || raise(ArgumentError, ':project_id is required') + project_id || raise_parameter_is_required_error(:project_id) request = Web::Request.new( self, @@ -38,7 +38,7 @@ def get_project(project_id = nil) end def delete_project(project_id = nil) - project_id || raise(ArgumentError, ':project_id is required') + project_id || raise_parameter_is_required_error(:project_id) request = Web::Request.new( self, @@ -50,7 +50,7 @@ def delete_project(project_id = nil) end def edit_project(project_id = nil, query = {}) - project_id || raise(ArgumentError, ':project_id is required') + project_id || raise_parameter_is_required_error(:project_id) request = Web::Request.new( self, @@ -92,8 +92,7 @@ def add_group(query = {}) def get_group(group_id = nil) config.enterprise_mode? || raise_only_for_enterprise_mode_error - - group_id || raise(ArgumentError, ':group_id is required') + group_id || raise_parameter_is_required_error(:group_id) request = Web::Request.new( self, @@ -106,8 +105,7 @@ def get_group(group_id = nil) def delete_group(group_id = nil) config.enterprise_mode? || raise_only_for_enterprise_mode_error - - group_id || raise(ArgumentError, ':group_id is required') + group_id || raise_parameter_is_required_error(:group_id) request = Web::Request.new( self, @@ -120,8 +118,7 @@ def delete_group(group_id = nil) def edit_group(group_id = nil, query = {}) config.enterprise_mode? || raise_only_for_enterprise_mode_error - - group_id || raise(ArgumentError, ':group_id is required') + group_id || raise_parameter_is_required_error(:group_id) request = Web::Request.new( self, From d5cce7facbf3bd1a537db28170cb537d26887500 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 02:45:25 +0200 Subject: [PATCH 25/40] Source Files module required parameters --- lib/crowdin-api/api-resources/source_files.rb | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/crowdin-api/api-resources/source_files.rb b/lib/crowdin-api/api-resources/source_files.rb index 7b4c748..7e28713 100644 --- a/lib/crowdin-api/api-resources/source_files.rb +++ b/lib/crowdin-api/api-resources/source_files.rb @@ -30,7 +30,7 @@ def add_branch(query = {}, project_id = config.project_id) end def get_branch(branch_id = nil, project_id = config.project_id) - branch_id || raise(ArgumentError, ':branch_id is required') + branch_id || raise_parameter_is_required_error(:branch_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -43,7 +43,7 @@ def get_branch(branch_id = nil, project_id = config.project_id) end def delete_branch(branch_id = nil, project_id = config.project_id) - branch_id || raise(ArgumentError, ':branch_id is required') + branch_id || raise_parameter_is_required_error(:branch_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -58,7 +58,7 @@ def delete_branch(branch_id = nil, project_id = config.project_id) def edit_branch(branch_id = nil, query = {}) project_id = query[:project_id] || config.project_id - branch_id || raise(ArgumentError, ':branch_id is required') + branch_id || raise_parameter_is_required_error(:branch_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -98,7 +98,7 @@ def add_directory(query = {}, project_id = config.project_id) end def get_directory(directory_id = nil, project_id = config.project_id) - directory_id || raise(ArgumentError, ':directory_id is required') + directory_id || raise_parameter_is_required_error(:directory_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -111,7 +111,7 @@ def get_directory(directory_id = nil, project_id = config.project_id) end def delete_directory(directory_id = nil, project_id = config.project_id) - directory_id || raise(ArgumentError, ':directory_id is required') + directory_id || raise_parameter_is_required_error(:directory_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -126,7 +126,7 @@ def delete_directory(directory_id = nil, project_id = config.project_id) def edit_directory(directory_id = nil, query = {}) project_id = query[:project_id] || config.project_id - directory_id || raise(ArgumentError, ':directory_id is required') + directory_id || raise_parameter_is_required_error(:directory_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -184,7 +184,7 @@ def add_file(query = {}, project_id = config.project_id) end def get_file(file_id = nil, project_id = config.project_id) - file_id || raise(ArgumentError, ':file_id is required') + file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -199,7 +199,7 @@ def get_file(file_id = nil, project_id = config.project_id) def update_or_restore_file(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id - file_id || raise(ArgumentError, ':file_id is required') + file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -213,7 +213,7 @@ def update_or_restore_file(file_id = nil, query = {}) end def delete_file(file_id = nil, project_id = config.project_id) - file_id || raise(ArgumentError, ':file_id is required') + file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -228,7 +228,7 @@ def delete_file(file_id = nil, project_id = config.project_id) def edit_file(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id - file_id || raise(ArgumentError, ':file_id is required') + file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -242,8 +242,8 @@ def edit_file(file_id = nil, query = {}) end def download_file(destination = nil, file_id = nil, project_id = config.project_id) - destination || raise(ArgumentError, ':destination is required for downlaods') - file_id || raise(ArgumentError, ':file_id is required') + destination || raise_parameter_is_required_error(:destination) + file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -275,14 +275,14 @@ def download_file(destination = nil, file_id = nil, project_id = config.project_ # # crowdin.list_file_revisions(your_file_id, { limit: your_value }) # - # or you can cpecify project_id + # or you can specify project_id # # crowdin.list_file_revisions(your_file_id, { project_id: your_project_id }) # def list_file_revisions(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id - file_id || raise(ArgumentError, ':file_id is required') + file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -296,8 +296,8 @@ def list_file_revisions(file_id = nil, query = {}) end def get_file_revision(file_id = nil, revision_id = nil, project_id = config.project_id) - file_id || raise(ArgumentError, ':file_id is required') - revision_id || raise(ArgumentError, ':revision_id is required') + file_id || raise_parameter_is_required_error(:file_id) + revision_id || raise_parameter_is_required_error(:revision_id) project_id || raise_project_id_is_required_error request = Web::Request.new( From d55d59f535bcf6285c43817d94ae39b0f95e5856 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 02:46:49 +0200 Subject: [PATCH 26/40] Storages module required parameters --- lib/crowdin-api/api-resources/storages.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/crowdin-api/api-resources/storages.rb b/lib/crowdin-api/api-resources/storages.rb index fe85281..e9840c4 100644 --- a/lib/crowdin-api/api-resources/storages.rb +++ b/lib/crowdin-api/api-resources/storages.rb @@ -39,7 +39,7 @@ def list_storages(query = {}) # crowdin.add_storage('your_filename.extension') # def add_storage(file = nil) - file || raise(ArgumentError, ':file is required') + file || raise_parameter_is_required_error(:file) file = file.is_a?(File) ? file : File.open(file, 'r') @@ -65,7 +65,7 @@ def add_storage(file = nil) # crowdin.get_storage(your_storage_id) # def get_storage(storage_id = nil) - storage_id || raise(ArgumentError, ':storage_id is required') + storage_id || raise_parameter_is_required_error(:storage_id) request = Web::Request.new( self, @@ -87,7 +87,7 @@ def get_storage(storage_id = nil) # crowdin.delete_storage(your_storage_id) # def delete_storage(storage_id = nil) - storage_id || raise(ArgumentError, ':storage_id is required') + storage_id || raise_parameter_is_required_error(:storage_id) request = Web::Request.new( self, From dfd81f6f7014f397dcea7e3edd79791d22eb44f9 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 02:48:26 +0200 Subject: [PATCH 27/40] Translation Status module required parameters --- lib/crowdin-api/api-resources/translation_status.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/crowdin-api/api-resources/translation_status.rb b/lib/crowdin-api/api-resources/translation_status.rb index 02665f9..c6ad6fb 100644 --- a/lib/crowdin-api/api-resources/translation_status.rb +++ b/lib/crowdin-api/api-resources/translation_status.rb @@ -6,7 +6,7 @@ module TranslationStatus def get_branch_progress(branch_id = nil, query = {}) project_id = query[:project_id] || config.project_id - branch_id || raise(ArgumentError, ':file_id is required') + branch_id || raise_parameter_is_required_error(:branch_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -22,7 +22,7 @@ def get_branch_progress(branch_id = nil, query = {}) def get_directory_progress(directory_id = nil, query = {}) project_id = query[:project_id] || config.project_id - directory_id || raise(ArgumentError, ':directory_id is required') + directory_id || raise_parameter_is_required_error(:directory_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -38,7 +38,7 @@ def get_directory_progress(directory_id = nil, query = {}) def get_file_progress(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id - file_id || raise(ArgumentError, ':file_id is required') + file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -54,7 +54,7 @@ def get_file_progress(file_id = nil, query = {}) def get_language_progress(language_id = nil, query = {}) project_id = query[:project_id] || config.project_id - language_id || raise(ArgumentError, ':language_id is required') + language_id || raise_parameter_is_required_error(:language_id) project_id || raise_project_id_is_required_error request = Web::Request.new( From 9d9427cb2f62052d001a437465d23c134d310bbc Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 02:51:29 +0200 Subject: [PATCH 28/40] Translations module required parameters --- lib/crowdin-api/api-resources/translations.rb | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/crowdin-api/api-resources/translations.rb b/lib/crowdin-api/api-resources/translations.rb index 3221392..78fb877 100644 --- a/lib/crowdin-api/api-resources/translations.rb +++ b/lib/crowdin-api/api-resources/translations.rb @@ -4,7 +4,7 @@ module Crowdin module ApiResources module Translations def pre_translation_status(pre_translation_id = nil, project_id = config.project_id) - pre_translation_id || raise(ArgumentError, ':pre_translation_id is required') + pre_translation_id || raise_parameter_is_required_error(:pre_translation_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -32,7 +32,7 @@ def apply_pre_translation(query = {}, project_id = config.project_id) def build_project_directory_translation(directory_id = nil, query = {}) project_id = query[:project_id] || config.project_id - directory_id || raise(ArgumentError, ':directory_id is required') + directory_id || raise_parameter_is_required_error(:directory_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -48,7 +48,7 @@ def build_project_directory_translation(directory_id = nil, query = {}) def build_project_file_translation(file_id = nil, query = {}) project_id = query[:project_id] || config.project_id - file_id || raise(ArgumentError, ':file_id is required') + file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error headers = query[:eTag] ? { 'If-None-Match' => query[:eTag] } : {} @@ -93,7 +93,7 @@ def build_project_translation(query = {}, project_id = config.project_id) def upload_translations(language_id = nil, query = {}) project_id = query[:project_id] || config.project_id - language_id || raise(ArgumentError, ':language_id is required') + language_id || raise_parameter_is_required_error(:language_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -106,10 +106,10 @@ def upload_translations(language_id = nil, query = {}) request.perform end - def download_project_translations(destinaton = nil, build_id = nil, project_id = config.project_id) - destinaton || raise(ArgumentError, ':destination is required for downlaods') - build_id || raise(ArgumentError, ':build_id is required') - project_id || raise_project_id_is_required_error + def download_project_translations(destination = nil, build_id = nil, project_id = config.project_id) + destination || raise_parameter_is_required_error(:destination) + build_id || raise_parameter_is_required_error(:build_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -117,14 +117,14 @@ def download_project_translations(destinaton = nil, build_id = nil, project_id = "/projects/#{project_id}/translations/builds/#{build_id}/download", {}, {}, - destinaton + destination ) request.perform end def check_project_build_status(build_id = nil, project_id = config.project_id) - build_id || raise(ArgumentError, ':build_id is required') + build_id || raise_parameter_is_required_error(:build_id) project_id || raise_project_id_is_required_error request = Web::Request.new( @@ -137,7 +137,7 @@ def check_project_build_status(build_id = nil, project_id = config.project_id) end def cancel_build(build_id = nil, project_id = config.project_id) - build_id || raise(ArgumentError, ':build_id is required') + build_id || raise_parameter_is_required_error(:build_id) project_id || raise_project_id_is_required_error request = Web::Request.new( From bf5dc4cf7a04a183c05f1aadea53a8359b514ade Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 02:54:37 +0200 Subject: [PATCH 29/40] Crowdin Errors --- lib/crowdin-api.rb | 1 + lib/crowdin-api/core/api_errors_raiser.rb | 2 +- lib/crowdin-api/core/errors.rb | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 lib/crowdin-api/core/errors.rb diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index f7d7e85..a2c412f 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -6,6 +6,7 @@ require 'rest-client' # Core modules +require 'crowdin-api/core/errors' require 'crowdin-api/core/api_errors_raiser' require 'crowdin-api/core/request' diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb index f9a41a2..df220e7 100644 --- a/lib/crowdin-api/core/api_errors_raiser.rb +++ b/lib/crowdin-api/core/api_errors_raiser.rb @@ -4,7 +4,7 @@ module Crowdin module Errors module ApiErrorsRaiser def raise_only_for_enterprise_mode_error - raise(NoMethodError, 'This method can be called only for Enterprise mode') + raise(OnlyForEnterpriseMode, 'This method can be called only for Enterprise mode') end def raise_project_id_is_required_error diff --git a/lib/crowdin-api/core/errors.rb b/lib/crowdin-api/core/errors.rb new file mode 100644 index 0000000..3aa75c5 --- /dev/null +++ b/lib/crowdin-api/core/errors.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Crowdin + module Errors + class OnlyForEnterpriseMode < StandardError; end + end +end From e0b5a2fd700a160211ad7fd31741b3554d024349 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 03:24:24 +0200 Subject: [PATCH 30/40] fetch_cleared_query --- lib/crowdin-api/core/request.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index 72b0b54..4d2c1ef 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -95,9 +95,9 @@ def fetch_cleared_query(query) when Array query.each do |el| el.reject! { |_, value| value.nil? } - end + end.reject!(&:empty?) when Hash - query.reject { |_, value| value.nil? } + query.reject! { |_, value| value.nil? } else query end From cded7aa0bad2d54ba01677cd7eb6865b4c577269 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 03:32:11 +0200 Subject: [PATCH 31/40] fetch_cleared_query --- lib/crowdin-api/core/request.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index 4d2c1ef..4da79e1 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -93,11 +93,11 @@ def fetch_response_data(doc) def fetch_cleared_query(query) case query when Array - query.each do |el| - el.reject! { |_, value| value.nil? } - end.reject!(&:empty?) + query.map do |el| + el.reject { |_, value| value.nil? } + end.reject(&:empty?) when Hash - query.reject! { |_, value| value.nil? } + query.reject { |_, value| value.nil? } else query end From bdf8ab2620485c384ca86985f09b83bc7b6827fb Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 15:42:22 +0200 Subject: [PATCH 32/40] Use option instead argument --- bin/crowdin-console | 4 ++-- lib/crowdin-api/core/api_errors_raiser.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/crowdin-console b/bin/crowdin-console index 5b48d21..0020aff 100755 --- a/bin/crowdin-console +++ b/bin/crowdin-console @@ -4,9 +4,9 @@ require 'crowdin-api' require 'optparse' # -# You can specify your arguments while executing this script +# You can specify your options while executing this script # -# Type bundle exec crowdin-console --help for more available arguments +# Type bundle exec crowdin-console --help for more available options # # Example: # diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb index df220e7..01cf85a 100644 --- a/lib/crowdin-api/core/api_errors_raiser.rb +++ b/lib/crowdin-api/core/api_errors_raiser.rb @@ -17,7 +17,7 @@ def raise_parameter_is_required_error(parameter) # crowdin-console errors def raise_api_token_is_required_error - raise(ArgumentError, '--api-token argument is required') + raise(ArgumentError, '--api-token option is required') end def raise_organization_domain_is_required_error From a798101eb34d4e318306c5d6076dc924ad02b5b5 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 21:34:39 +0200 Subject: [PATCH 33/40] Workflows module --- .rubocop_todo.yml | 17 ++++++- lib/crowdin-api.rb | 1 + lib/crowdin-api/api-resources/workflows.rb | 59 ++++++++++++++++++++++ lib/crowdin-api/client/client.rb | 1 + 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 lib/crowdin-api/api-resources/workflows.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1785d84..dbd58e2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-12-09 14:59:50 UTC using RuboCop version 1.23.0. +# on 2021-12-13 19:34:18 UTC using RuboCop version 1.23.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -21,6 +21,13 @@ Gemspec/RequiredRubyVersion: Exclude: - 'crowdin-api.gemspec' +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. +Layout/ExtraSpacing: + Exclude: + - 'bin/crowdin-console' + # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator. @@ -99,7 +106,7 @@ Style/CollectionCompact: Exclude: - 'lib/crowdin-api/core/request.rb' -# Offense count: 10 +# Offense count: 11 # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: @@ -111,6 +118,7 @@ Style/Documentation: - 'lib/crowdin-api/api-resources/storages.rb' - 'lib/crowdin-api/api-resources/translation_status.rb' - 'lib/crowdin-api/api-resources/translations.rb' + - 'lib/crowdin-api/api-resources/workflows.rb' - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' - 'lib/crowdin-api/core/api_errors_raiser.rb' @@ -123,3 +131,8 @@ Style/Documentation: Style/FrozenStringLiteralComment: Exclude: - 'bin/crowdin-console' + +# Offense count: 1 +Style/MixinUsage: + Exclude: + - 'bin/crowdin-console' diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index a2c412f..e80b27c 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -17,6 +17,7 @@ require 'crowdin-api/api-resources/storages' require 'crowdin-api/api-resources/translation_status' require 'crowdin-api/api-resources/translations' +require 'crowdin-api/api-resources/workflows' # Client require 'crowdin-api/client/version' diff --git a/lib/crowdin-api/api-resources/workflows.rb b/lib/crowdin-api/api-resources/workflows.rb new file mode 100644 index 0000000..e13ef55 --- /dev/null +++ b/lib/crowdin-api/api-resources/workflows.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Workflows + def list_workflow_steps(query = {}, project_id = config.project_id) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/workflow-steps", + query + ) + + request.perform + end + + def get_workflow_step(step_id = nil, project_id = config.project_id) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + step_id || raise_parameter_is_required_error(:step_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/workflow-steps/#{step_id}" + ) + + request.perform + end + + def list_workflow_templates(query = {}) + request = Web::Request.new( + self, + :get, + '/workflow-templates', + query + ) + + request.perform + end + + def get_workflow_template(template_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + template_id || raise_parameter_is_required_error(:template_id) + + request = Web::Request.new( + self, + :get, + "/workflow-templates/#{template_id}" + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 6e3dda4..f97a08b 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -27,6 +27,7 @@ class Client include ApiResources::Storages include ApiResources::TranslationStatus include ApiResources::Translations + include ApiResources::Workflows include Errors::ApiErrorsRaiser From c5c482e14e0b651916e8a3737d419ed8e116b66c Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 22:14:33 +0200 Subject: [PATCH 34/40] Source Strings module --- lib/crowdin-api.rb | 1 + .../api-resources/source_strings.rb | 78 +++++++++++++++++++ lib/crowdin-api/client/client.rb | 1 + lib/crowdin-api/core/api_errors_raiser.rb | 1 + 4 files changed, 81 insertions(+) create mode 100644 lib/crowdin-api/api-resources/source_strings.rb diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index e80b27c..4b1eb63 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -18,6 +18,7 @@ require 'crowdin-api/api-resources/translation_status' require 'crowdin-api/api-resources/translations' require 'crowdin-api/api-resources/workflows' +require 'crowdin-api/api-resources/source_strings' # Client require 'crowdin-api/client/version' diff --git a/lib/crowdin-api/api-resources/source_strings.rb b/lib/crowdin-api/api-resources/source_strings.rb new file mode 100644 index 0000000..f50131f --- /dev/null +++ b/lib/crowdin-api/api-resources/source_strings.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module SourceStrings + def list_strings(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/strings", + query + ) + + request.perform + end + + def add_string(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/strings", + query + ) + + request.perform + end + + def get_string(string_id = nil, query = {}) + project_id = query[:project_id] || config.project_id + + string_id || raise_parameter_is_required_error(:string_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/strings/#{string_id}", + query + ) + + request.perform + end + + def delete_string(string_id = nil, project_id = config.project_id) + string_id || raise_parameter_is_required_error(:string_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/strings/#{string_id}" + ) + + request.perform + end + + def edit_string(string_id = nil, query = {}) + project_id = query[:project_id] || config.project_id + + string_id || raise_parameter_is_required_error(:string_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/strings/#{string_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index f97a08b..090e652 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -28,6 +28,7 @@ class Client include ApiResources::TranslationStatus include ApiResources::Translations include ApiResources::Workflows + include ApiResources::SourceStrings include Errors::ApiErrorsRaiser diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb index 01cf85a..9deec6d 100644 --- a/lib/crowdin-api/core/api_errors_raiser.rb +++ b/lib/crowdin-api/core/api_errors_raiser.rb @@ -16,6 +16,7 @@ def raise_parameter_is_required_error(parameter) end # crowdin-console errors + def raise_api_token_is_required_error raise(ArgumentError, '--api-token option is required') end From 212730b239a3c93ba38916ae555ed8643eceb05f Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 22:26:50 +0200 Subject: [PATCH 35/40] Rubocop autogen config --- .rubocop_todo.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index dbd58e2..32267ad 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-12-13 19:34:18 UTC using RuboCop version 1.23.0. +# on 2021-12-13 20:26:23 UTC using RuboCop version 1.23.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -106,7 +106,7 @@ Style/CollectionCompact: Exclude: - 'lib/crowdin-api/core/request.rb' -# Offense count: 11 +# Offense count: 12 # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: @@ -115,6 +115,7 @@ Style/Documentation: - 'lib/crowdin-api/api-resources/languages.rb' - 'lib/crowdin-api/api-resources/projects.rb' - 'lib/crowdin-api/api-resources/source_files.rb' + - 'lib/crowdin-api/api-resources/source_strings.rb' - 'lib/crowdin-api/api-resources/storages.rb' - 'lib/crowdin-api/api-resources/translation_status.rb' - 'lib/crowdin-api/api-resources/translations.rb' From 1b54196158c63737e8ccf43056c8029eaba5e3c5 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 22:44:43 +0200 Subject: [PATCH 36/40] Source Files revisions --- lib/crowdin-api/api-resources/source_files.rb | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/lib/crowdin-api/api-resources/source_files.rb b/lib/crowdin-api/api-resources/source_files.rb index 7e28713..9618f54 100644 --- a/lib/crowdin-api/api-resources/source_files.rb +++ b/lib/crowdin-api/api-resources/source_files.rb @@ -55,9 +55,7 @@ def delete_branch(branch_id = nil, project_id = config.project_id) request.perform end - def edit_branch(branch_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def edit_branch(branch_id = nil, query = {}, project_id = config.project_id) branch_id || raise_parameter_is_required_error(:branch_id) project_id || raise_project_id_is_required_error @@ -123,9 +121,7 @@ def delete_directory(directory_id = nil, project_id = config.project_id) request.perform end - def edit_directory(directory_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def edit_directory(directory_id = nil, query = {}, project_id = config.project_id) directory_id || raise_parameter_is_required_error(:directory_id) project_id || raise_project_id_is_required_error @@ -168,7 +164,7 @@ def list_files(query = {}, project_id = config.project_id) # # or you can specify project_id # - # crowdin.add_file({}, your_project_id) + # crowdin.add_file({ storage_id: your_storage_id, name: 'your_filename' }, your_project_id) # def add_file(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error @@ -196,9 +192,7 @@ def get_file(file_id = nil, project_id = config.project_id) request.perform end - def update_or_restore_file(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def update_or_restore_file(file_id = nil, query = {}, project_id = config.project_id) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error @@ -225,9 +219,7 @@ def delete_file(file_id = nil, project_id = config.project_id) request.perform end - def edit_file(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def edit_file(file_id = nil, query = {}, project_id = config.project_id) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error @@ -273,15 +265,13 @@ def download_file(destination = nil, file_id = nil, project_id = config.project_ # # when you're initialized Crowdin Client with a project_id # - # crowdin.list_file_revisions(your_file_id, { limit: your_value }) + # crowdin.list_file_revisions(your_file_id, limit: your_value) # # or you can specify project_id # - # crowdin.list_file_revisions(your_file_id, { project_id: your_project_id }) + # crowdin.list_file_revisions(your_file_id, { limit: your_value }, your_project_id) # - def list_file_revisions(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def list_file_revisions(file_id = nil, query = {}, project_id = config.project_id) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error From 7a229330294a16f6e5c15cde9e40a8d60b85d1b5 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 22:45:56 +0200 Subject: [PATCH 37/40] Source String project_id revision --- lib/crowdin-api/api-resources/source_strings.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/crowdin-api/api-resources/source_strings.rb b/lib/crowdin-api/api-resources/source_strings.rb index f50131f..ce3f100 100644 --- a/lib/crowdin-api/api-resources/source_strings.rb +++ b/lib/crowdin-api/api-resources/source_strings.rb @@ -29,9 +29,7 @@ def add_string(query = {}, project_id = config.project_id) request.perform end - def get_string(string_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def get_string(string_id = nil, query = {}, project_id = config.project_id) string_id || raise_parameter_is_required_error(:string_id) project_id || raise_project_id_is_required_error @@ -58,9 +56,7 @@ def delete_string(string_id = nil, project_id = config.project_id) request.perform end - def edit_string(string_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def edit_string(string_id = nil, query = {}, project_id = config.project_id) string_id || raise_parameter_is_required_error(:string_id) project_id || raise_project_id_is_required_error From 770f04d7b6ac26f22feb1d22c1db0a5cf8a594dc Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 22:47:49 +0200 Subject: [PATCH 38/40] Translation Status project_id revision --- .../api-resources/translation_status.rb | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/crowdin-api/api-resources/translation_status.rb b/lib/crowdin-api/api-resources/translation_status.rb index c6ad6fb..05264c0 100644 --- a/lib/crowdin-api/api-resources/translation_status.rb +++ b/lib/crowdin-api/api-resources/translation_status.rb @@ -3,11 +3,9 @@ module Crowdin module ApiResources module TranslationStatus - def get_branch_progress(branch_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - branch_id || raise_parameter_is_required_error(:branch_id) - project_id || raise_project_id_is_required_error + def get_branch_progress(branch_id = nil, query = {}, project_id = config.project_id) + branch_id || raise_parameter_is_required_error(:branch_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -19,9 +17,7 @@ def get_branch_progress(branch_id = nil, query = {}) request.perform end - def get_directory_progress(directory_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def get_directory_progress(directory_id = nil, query = {}, project_id = config.project_id) directory_id || raise_parameter_is_required_error(:directory_id) project_id || raise_project_id_is_required_error @@ -35,9 +31,7 @@ def get_directory_progress(directory_id = nil, query = {}) request.perform end - def get_file_progress(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def get_file_progress(file_id = nil, query = {}, project_id = config.project_id) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error @@ -51,9 +45,7 @@ def get_file_progress(file_id = nil, query = {}) request.perform end - def get_language_progress(language_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def get_language_progress(language_id = nil, query = {}, project_id = config.project_id) language_id || raise_parameter_is_required_error(:language_id) project_id || raise_project_id_is_required_error From 4f436e0cd98bd11b4150979e7fb490c801e84071 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 22:49:58 +0200 Subject: [PATCH 39/40] Translations project_id revision --- lib/crowdin-api/api-resources/translations.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/crowdin-api/api-resources/translations.rb b/lib/crowdin-api/api-resources/translations.rb index 78fb877..0dcfd8b 100644 --- a/lib/crowdin-api/api-resources/translations.rb +++ b/lib/crowdin-api/api-resources/translations.rb @@ -29,9 +29,7 @@ def apply_pre_translation(query = {}, project_id = config.project_id) request.perform end - def build_project_directory_translation(directory_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def build_project_directory_translation(directory_id = nil, query = {}, project_id = config.project_id) directory_id || raise_parameter_is_required_error(:directory_id) project_id || raise_project_id_is_required_error @@ -45,9 +43,7 @@ def build_project_directory_translation(directory_id = nil, query = {}) request.perform end - def build_project_file_translation(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def build_project_file_translation(file_id = nil, query = {}, project_id = config.project_id) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error @@ -90,9 +86,7 @@ def build_project_translation(query = {}, project_id = config.project_id) request.perform end - def upload_translations(language_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - + def upload_translations(language_id = nil, query = {}, project_id = config.project_id) language_id || raise_parameter_is_required_error(:language_id) project_id || raise_project_id_is_required_error From 199ff594303a4fd016053be3db1026f287b4a08f Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 13 Dec 2021 22:53:26 +0200 Subject: [PATCH 40/40] Readme changes --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 14f45f0..1bb7b94 100644 --- a/README.md +++ b/README.md @@ -126,8 +126,10 @@ filename = crowdin.download_file(your_destination, your_file_id, your_project_id # project_id is optional, as it can be initialized with a Crowdin Client # File revisions -file_revisions = crowdin.list_file_revisions(your_file_id, { limit: 10, project_id: your_project_id }) -# project_id is optional, as it can be initialized with a Crowdin Client +# with initialized project_id in your Client +file_revisions = crowdin.list_file_revisions(your_file_id, limit: 10) +# or you can specify your project_id +file_revisions = crowdin.list_file_revisions(your_file_id, { limit: 10 }, your_project_id) ``` ### Command-Line Client