From 09f198916af60177e33ff0b8627766bb1da7b0b9 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Fri, 31 Dec 2021 20:17:21 +0200 Subject: [PATCH 1/9] Added requirements to readme --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1bb7b94..104a64e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ For more about Crowdin API v2 see the documentation: - [Crowdin](https://support.crowdin.com/api/v2/) - [Crowdin Enterprise](https://support.crowdin.com/enterprise/api/) -### Status +## Status [![Gem](https://img.shields.io/gem/v/crowdin-api?logo=ruby&cacheSeconds=1800)](https://rubygems.org/gems/crowdin-api) [![Gem](https://img.shields.io/gem/dt/crowdin-api?cacheSeconds=1800)](https://rubygems.org/gems/crowdin-api) @@ -31,6 +31,9 @@ For more about Crowdin API v2 see the documentation: * [Contributing](#contributing) * [License](#license) +## Requirements +* Ruby >= 2.4 + ## Installation Add this line to your application's Gemfile: From 557ce1a8c37a37e050c0e0cc8bb9b30fab0198d2 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Mon, 3 Jan 2022 10:35:53 +0200 Subject: [PATCH 2/9] Some changes and improvements --- README.md | 8 ++-- bin/crowdin-console | 5 ++- lib/crowdin-api.rb | 3 +- lib/crowdin-api/api-resources/languages.rb | 1 + lib/crowdin-api/api-resources/projects.rb | 4 +- lib/crowdin-api/api-resources/source_files.rb | 2 + lib/crowdin-api/api-resources/storages.rb | 4 ++ lib/crowdin-api/api-resources/workflows.rb | 5 +++ lib/crowdin-api/client/client.rb | 43 ++++++++++++++----- lib/crowdin-api/client/configuration.rb | 16 ++++--- lib/crowdin-api/core/api_errors_raiser.rb | 12 +----- lib/crowdin-api/core/clc_errors_raiser.rb | 15 +++++++ lib/crowdin-api/core/client_errors_raiser.rb | 12 ++++++ lib/crowdin-api/core/errors.rb | 3 +- spec/client/client-instance_spec.rb | 14 ++++++ .../configuration-instance_spec.rb} | 14 +++--- spec/crowdin-api_spec.rb | 7 --- 17 files changed, 117 insertions(+), 51 deletions(-) create mode 100644 lib/crowdin-api/core/clc_errors_raiser.rb create mode 100644 lib/crowdin-api/core/client_errors_raiser.rb create mode 100644 spec/client/client-instance_spec.rb rename spec/{core/config-instance_spec.rb => client/configuration-instance_spec.rb} (82%) delete mode 100644 spec/crowdin-api_spec.rb diff --git a/README.md b/README.md index 104a64e..24b41ef 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ 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 +# 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 @@ -120,7 +120,7 @@ project = crowdin.get_project(your_project_id) # Add Storage storage = crowdin.add_storage(File.open('YourFilename.extension', 'r')) -# or you can specify only filename +# or you can specify only path to file storage = crowdin.add_storage('YourFilename.extension') # Download file @@ -146,9 +146,9 @@ $ bundle exec crowdin-console --enable-logger --api-token API_TOKEN --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 DOMAIN --project-id PROJECT_ID ``` -Note: you can specify full organization domain by adding .com +Note: you can specify full organization domain by adding '.com' When execute you'll have IRB console with configured *@crowdin* instance diff --git a/bin/crowdin-console b/bin/crowdin-console index 0020aff..5891efa 100755 --- a/bin/crowdin-console +++ b/bin/crowdin-console @@ -17,7 +17,8 @@ require 'optparse' # bundle exec crowdin-console --enterprise --api-token YOUR_ENTERPRISE_API_TOKEN --organization-domain YOUR_DOMAIN # -include Crowdin::Errors::ApiErrorsRaiser +require 'crowdin-api/core/clc_errors_raiser' +include Crowdin::Errors::ClcErrorsRaiser options = {} @@ -27,7 +28,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('--project-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! diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index 4b1eb63..c57b04d 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -8,6 +8,7 @@ # Core modules require 'crowdin-api/core/errors' require 'crowdin-api/core/api_errors_raiser' +require 'crowdin-api/core/client_errors_raiser' require 'crowdin-api/core/request' # Api modules @@ -20,7 +21,7 @@ require 'crowdin-api/api-resources/workflows' require 'crowdin-api/api-resources/source_strings' -# Client +# Client modules require 'crowdin-api/client/version' require 'crowdin-api/client/configuration' require 'crowdin-api/client/client' diff --git a/lib/crowdin-api/api-resources/languages.rb b/lib/crowdin-api/api-resources/languages.rb index f910371..243be48 100644 --- a/lib/crowdin-api/api-resources/languages.rb +++ b/lib/crowdin-api/api-resources/languages.rb @@ -14,6 +14,7 @@ def list_languages(query = {}) request.perform end + # # Add custom language. # # === Parameters diff --git a/lib/crowdin-api/api-resources/projects.rb b/lib/crowdin-api/api-resources/projects.rb index e004192..209898d 100644 --- a/lib/crowdin-api/api-resources/projects.rb +++ b/lib/crowdin-api/api-resources/projects.rb @@ -62,7 +62,9 @@ def edit_project(project_id = nil, query = {}) request.perform end - # For Enterprise mode only + # + # -- For Enterprise mode only -- + # def list_groups(query = {}) config.enterprise_mode? || raise_only_for_enterprise_mode_error diff --git a/lib/crowdin-api/api-resources/source_files.rb b/lib/crowdin-api/api-resources/source_files.rb index 9618f54..330a53e 100644 --- a/lib/crowdin-api/api-resources/source_files.rb +++ b/lib/crowdin-api/api-resources/source_files.rb @@ -148,6 +148,7 @@ def list_files(query = {}, project_id = config.project_id) request.perform end + # # Add custom language. # # === Parameters @@ -250,6 +251,7 @@ def download_file(destination = nil, file_id = nil, project_id = config.project_ request.perform end + # # List file revisions. # # === Parameters diff --git a/lib/crowdin-api/api-resources/storages.rb b/lib/crowdin-api/api-resources/storages.rb index e9840c4..1a4b1b1 100644 --- a/lib/crowdin-api/api-resources/storages.rb +++ b/lib/crowdin-api/api-resources/storages.rb @@ -3,6 +3,7 @@ module Crowdin module ApiResources module Storages + # # Get storages list. # # === Parameters @@ -26,6 +27,7 @@ def list_storages(query = {}) request.perform end + # # Add storage. # # === Parameters @@ -54,6 +56,7 @@ def add_storage(file = nil) request.perform end + # # Get storage information. # # === Parameters @@ -76,6 +79,7 @@ def get_storage(storage_id = nil) request.perform end + # # Delete storage. # # === Parameters diff --git a/lib/crowdin-api/api-resources/workflows.rb b/lib/crowdin-api/api-resources/workflows.rb index e13ef55..ffb0673 100644 --- a/lib/crowdin-api/api-resources/workflows.rb +++ b/lib/crowdin-api/api-resources/workflows.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +# +# -- For Enterprise mode only -- +# module Crowdin module ApiResources module Workflows @@ -32,6 +35,8 @@ def get_workflow_step(step_id = nil, project_id = config.project_id) end def list_workflow_templates(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + request = Web::Request.new( self, :get, diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 090e652..901229c 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -14,29 +14,45 @@ # crowdin.list_projects # module Crowdin + # + # A wrapper and interface to the Crowdin api. Please visit the Crowdin developers site + # for a full explanation of what each of the Crowdin api methods expect and perform. + # + # https://support.crowdin.com/api/v2/ + # class Client - # A wrapper and interface to the Crowdin api. Please visit the Crowdin developers - # site for a full explanation of what each of the Crowdin api methods - # expect and perform. - # - # https://support.crowdin.com/api/v2/ - # + include ApiResources::Storages include ApiResources::Languages include ApiResources::Projects + include ApiResources::Workflows # Enterprise include ApiResources::SourceFiles - include ApiResources::Storages - include ApiResources::TranslationStatus include ApiResources::Translations - include ApiResources::Workflows include ApiResources::SourceStrings + # Sting Translations + # String Comments + # Screenshots + # Glossaries + # Translation Memory + # Machine Translation Engines # Enterprise + include ApiResources::TranslationStatus + # Reports + # Tasks + # Issues + # Users + # Teams # Enterprise + # Vendors # Enterprise + # Webhooks + # Dictionaries + # Distributions + # Labels include Errors::ApiErrorsRaiser - - attr_accessor :logger + include Errors::ClientErrorsRaiser attr_reader :config attr_reader :connection attr_reader :options + attr_reader :logger def initialize(&block) build_configuration(&block) @@ -51,6 +67,11 @@ def log!(message) !config.logger_enabled? || logger.debug(message) end + def logger=(logger) + raise_logger_are_not_enabled_error unless config.logger_enabled? + @logger = logger + end + private def build_configuration diff --git a/lib/crowdin-api/client/configuration.rb b/lib/crowdin-api/client/configuration.rb index 064b0b7..8006685 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -9,8 +9,7 @@ class Configuration attr_reader :target_api_url - alias logger_enabled? enable_logger - alias enterprise_mode? organization_domain + alias logger_enabled? enable_logger def initialize @target_api_url = '/api/v2' @@ -20,16 +19,16 @@ def options { headers: {}, timeout: nil, - json: true + json: true } end def headers { - 'Accept' => 'application/json', + 'Accept' => 'application/json', 'Authorization' => "Bearer #{api_token}", - 'Content-Type' => 'application/json', - 'User-Agent' => "crowdin-rb/#{Crowdin::Client::VERSION}/#{RUBY_VERSION}/#{RUBY_PLATFORM}" + 'Content-Type' => 'application/json', + 'User-Agent' => "crowdin-rb/#{Crowdin::Client::VERSION}/#{RUBY_VERSION}/#{RUBY_PLATFORM}" } end @@ -40,5 +39,10 @@ def base_url 'https://api.crowdin.com' end end + + def organization_domain? + !!organization_domain + end + alias enterprise_mode? organization_domain? end end diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb index 9deec6d..15167cf 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(OnlyForEnterpriseMode, 'This method can be called only for Enterprise mode') + raise(OnlyForEnterpriseModeError, 'This method can be called only for Enterprise mode') end def raise_project_id_is_required_error @@ -14,16 +14,6 @@ def raise_project_id_is_required_error def raise_parameter_is_required_error(parameter) raise(ArgumentError, ":#{parameter} is required") end - - # crowdin-console errors - - def raise_api_token_is_required_error - raise(ArgumentError, '--api-token option is required') - end - - def raise_organization_domain_is_required_error - raise(ArgumentError, '--organization-domain option is required for Enterprise mode') - end end end end diff --git a/lib/crowdin-api/core/clc_errors_raiser.rb b/lib/crowdin-api/core/clc_errors_raiser.rb new file mode 100644 index 0000000..c1d539c --- /dev/null +++ b/lib/crowdin-api/core/clc_errors_raiser.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Crowdin + module Errors + module ClcErrorsRaiser + def raise_api_token_is_required_error + raise(ArgumentError, '--api-token option is required') + end + + def raise_organization_domain_is_required_error + raise(ArgumentError, '--organization-domain option is required for Enterprise mode') + end + end + end +end diff --git a/lib/crowdin-api/core/client_errors_raiser.rb b/lib/crowdin-api/core/client_errors_raiser.rb new file mode 100644 index 0000000..c9bfc1b --- /dev/null +++ b/lib/crowdin-api/core/client_errors_raiser.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Crowdin + module Errors + module ClientErrorsRaiser + def raise_logger_are_not_enabled_error + raise(LoggerAreNotEnabledError, 'Logger are not enabled in your Client configuration, enable it ' + + 'before setting your own logger') + end + end + end +end diff --git a/lib/crowdin-api/core/errors.rb b/lib/crowdin-api/core/errors.rb index 3aa75c5..5841e00 100644 --- a/lib/crowdin-api/core/errors.rb +++ b/lib/crowdin-api/core/errors.rb @@ -2,6 +2,7 @@ module Crowdin module Errors - class OnlyForEnterpriseMode < StandardError; end + class OnlyForEnterpriseModeError < StandardError; end + class LoggerAreNotEnabledError < StandardError; end end end diff --git a/spec/client/client-instance_spec.rb b/spec/client/client-instance_spec.rb new file mode 100644 index 0000000..b9e4bab --- /dev/null +++ b/spec/client/client-instance_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +describe 'Config instance' do + before do + @crowdin = Crowdin::Client.new do |config| + config.api_token = 'api_token' + config.project_id = 1 + end + end + + it 'should have a version' do + expect(Crowdin::Client::VERSION).to_not be_nil + end +end diff --git a/spec/core/config-instance_spec.rb b/spec/client/configuration-instance_spec.rb similarity index 82% rename from spec/core/config-instance_spec.rb rename to spec/client/configuration-instance_spec.rb index 7719f87..67319e1 100644 --- a/spec/core/config-instance_spec.rb +++ b/spec/client/configuration-instance_spec.rb @@ -8,7 +8,7 @@ end end - it 'show have a #project_id' do + it 'should have a #project_id' do expect(@crowdin.config.project_id).to_not be_nil end @@ -16,12 +16,12 @@ expect(@crowdin.config.api_token).to_not be_nil end - it '#target_api_url should equal /api/v2 by default' do + it '#target_api_url should equal \'api/v2\' by default' do expect(@crowdin.config.target_api_url).to eq('/api/v2') end describe '#logger_enabled?' do - it 'should be false by default' do + it 'should equal false by default' do expect(@crowdin.config.logger_enabled?).to be_falsey end @@ -35,11 +35,11 @@ end describe '#enterprise_mode?' do - it 'should be false by default' do + it 'should equal false by default' do expect(@crowdin.config.enterprise_mode?).to be_falsey end - it 'should equal specified arguments' do + it 'should equal true if user specify organization domain' do @crowdin = Crowdin::Client.new do |config| config.organization_domain = 'organization_domain' end @@ -49,7 +49,7 @@ end describe '#base_url' do - it 'should equal https://api.crowdin.com by default' do + it 'should equal \'https://api.crowdin.com\' by default' do expect(@crowdin.config.base_url).to eq('https://api.crowdin.com') end @@ -61,7 +61,7 @@ expect(@crowdin.config.base_url).to eq('https://organization_domain.api.crowdin.com') end - it 'should equal full specified organization domain when user specify full url (with .com)' do + 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 diff --git a/spec/crowdin-api_spec.rb b/spec/crowdin-api_spec.rb deleted file mode 100644 index 89bd928..0000000 --- a/spec/crowdin-api_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -describe Crowdin::Client do - it 'should have a version' do - expect(Crowdin::Client::VERSION).to_not be_nil - end -end From 6d6be37b5d61717890ea96ad86258eb363af0187 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Wed, 5 Jan 2022 09:50:24 +0200 Subject: [PATCH 3/9] Linter revisions --- .github/workflows/test-and-lint.yml | 2 +- .rubocop_todo.yml | 26 +++++++++++--------- lib/crowdin-api/core/client_errors_raiser.rb | 2 +- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test-and-lint.yml b/.github/workflows/test-and-lint.yml index 0a36a19..da9eeeb 100644 --- a/.github/workflows/test-and-lint.yml +++ b/.github/workflows/test-and-lint.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ['2.6', '2.7', '3.0'] + ruby-version: [ '2.6', '2.7', '3.0' ] steps: - name: Checkout code uses: actions/checkout@v2 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 32267ad..780ebcb 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 20:26:23 UTC using RuboCop version 1.23.0. +# on 2022-01-05 07:49:15 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,12 +21,15 @@ Gemspec/RequiredRubyVersion: Exclude: - 'crowdin-api.gemspec' -# Offense count: 2 +# Offense count: 4 # Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. -Layout/ExtraSpacing: +# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. +# SupportedHashRocketStyles: key, separator, table +# SupportedColonStyles: key, separator, table +# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit +Layout/HashAlignment: Exclude: - - 'bin/crowdin-console' + - 'lib/crowdin-api/client/configuration.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -55,7 +58,7 @@ Metrics/MethodLength: # Offense count: 2 # Configuration parameters: CountComments, CountAsOne. Metrics/ModuleLength: - Max: 205 + Max: 200 # Offense count: 1 # Configuration parameters: CountKeywordArgs, MaxOptionalParameters. @@ -74,8 +77,8 @@ Naming/AccessorMethodName: Naming/FileName: Exclude: - 'lib/crowdin-api.rb' - - 'spec/core/config-instance_spec.rb' - - 'spec/crowdin-api_spec.rb' + - 'spec/client/client-instance_spec.rb' + - 'spec/client/configuration-instance_spec.rb' # Offense count: 3 # Configuration parameters: EnforcedStyleForLeadingUnderscores. @@ -91,7 +94,7 @@ Naming/RescuedExceptionsVariableName: Exclude: - 'lib/crowdin-api/core/request.rb' -# Offense count: 7 +# Offense count: 8 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: separated, grouped @@ -106,7 +109,7 @@ Style/CollectionCompact: Exclude: - 'lib/crowdin-api/core/request.rb' -# Offense count: 12 +# Offense count: 13 # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: @@ -120,9 +123,10 @@ Style/Documentation: - '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' + - 'lib/crowdin-api/core/clc_errors_raiser.rb' + - 'lib/crowdin-api/core/client_errors_raiser.rb' - 'lib/crowdin-api/core/request.rb' # Offense count: 1 diff --git a/lib/crowdin-api/core/client_errors_raiser.rb b/lib/crowdin-api/core/client_errors_raiser.rb index c9bfc1b..d812f93 100644 --- a/lib/crowdin-api/core/client_errors_raiser.rb +++ b/lib/crowdin-api/core/client_errors_raiser.rb @@ -4,7 +4,7 @@ module Crowdin module Errors module ClientErrorsRaiser def raise_logger_are_not_enabled_error - raise(LoggerAreNotEnabledError, 'Logger are not enabled in your Client configuration, enable it ' + + raise(LoggerAreNotEnabledError, 'Logger are not enabled in your Client configuration, enable it ' \ 'before setting your own logger') end end From 6d08f3914e7b5aead36d13a1a58f74a0bf90507f Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Fri, 14 Jan 2022 19:50:30 +0200 Subject: [PATCH 4/9] remove destination as required param --- lib/crowdin-api/api-resources/source_files.rb | 1 - lib/crowdin-api/api-resources/translations.rb | 1 - lib/crowdin-api/core/request.rb | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/crowdin-api/api-resources/source_files.rb b/lib/crowdin-api/api-resources/source_files.rb index d39b702..42d39d8 100644 --- a/lib/crowdin-api/api-resources/source_files.rb +++ b/lib/crowdin-api/api-resources/source_files.rb @@ -235,7 +235,6 @@ def edit_file(file_id = nil, query = {}, project_id = config.project_id) end def download_file(file_id = nil, destination = nil, project_id = config.project_id) - destination || raise_parameter_is_required_error(:destination) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error diff --git a/lib/crowdin-api/api-resources/translations.rb b/lib/crowdin-api/api-resources/translations.rb index 6d72a50..679b0b9 100644 --- a/lib/crowdin-api/api-resources/translations.rb +++ b/lib/crowdin-api/api-resources/translations.rb @@ -101,7 +101,6 @@ def upload_translations(language_id = nil, query = {}, project_id = config.proje end def download_project_translations(build_id = nil, destination = 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 diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index 7272e95..10d95df 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -68,7 +68,7 @@ def perform_payload(query) end def download_file(url) - download = URI.parse(url).open + download = URI.parse(url).open destination = @destination || download.meta['content-disposition'] .match(/filename=("?)(.+)\1/)[2] From fbe95ea72645f2a2d10d88e644dcb3b35b37167e Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Fri, 14 Jan 2022 20:01:43 +0200 Subject: [PATCH 5/9] bump version --- README.md | 8 +++++--- lib/crowdin-api/client/version.rb | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6ba1cb9..899d320 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ For more about Crowdin API v2 see the documentation: Add this line to your application's Gemfile: ```gemfile -gem 'crowdin-api', '~> 1.1.0' +gem 'crowdin-api', '~> 1.2.0' ``` And then execute: @@ -73,7 +73,8 @@ crowdin = Crowdin::Client.new do |config| config.api_token = 'YourApiToken' end -# Or you can intialize Enterprise Client instance by specifying your organization_domain in config options +# 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' @@ -125,7 +126,8 @@ storage = crowdin.add_storage('YourFilename.extension') # Download file filename = crowdin.download_file(your_file_id, your_destination, your_project_id) -# your_destination - filename or full path to file, default - saving to currect directory with default filename +# your_destination - filename or full path to file, optional +# without destination option - saving to currect directory with default filename # project_id is optional, as it can be initialized with a Crowdin Client # File revisions diff --git a/lib/crowdin-api/client/version.rb b/lib/crowdin-api/client/version.rb index 21563ba..ac1e208 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.1.1' + VERSION = '1.2.0' end end From 3b855283af5c4b06dbd6529149fcc03d5efc4ff4 Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Sat, 15 Jan 2022 02:25:29 +0200 Subject: [PATCH 6/9] readme typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 899d320..c2f94d6 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ storage = crowdin.add_storage('YourFilename.extension') # Download file filename = crowdin.download_file(your_file_id, your_destination, your_project_id) # your_destination - filename or full path to file, optional -# without destination option - saving to currect directory with default filename +# without destination option - saving to current directory with a default filename # project_id is optional, as it can be initialized with a Crowdin Client # File revisions From b9ddf929f4cadd97272b15d1e06cf4add36e814b Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Sat, 15 Jan 2022 03:13:34 +0200 Subject: [PATCH 7/9] readme updates --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c2f94d6..60feb15 100644 --- a/README.md +++ b/README.md @@ -90,8 +90,8 @@ crowdin = Crowdin::Client.new do |config| 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 +# 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 ``` @@ -125,9 +125,9 @@ storage = crowdin.add_storage(File.open('YourFilename.extension', 'r')) storage = crowdin.add_storage('YourFilename.extension') # Download file -filename = crowdin.download_file(your_file_id, your_destination, your_project_id) -# your_destination - filename or full path to file, optional -# without destination option - saving to current directory with a default filename +file = crowdin.download_file(your_file_id, your_destination, your_project_id) +# your_destination - filename or absolute path to file, optional +# Without destination option method will save file to current directory with a default filename # project_id is optional, as it can be initialized with a Crowdin Client # File revisions From 92a8f14949c18f4111fb987e0a30a630b8d0c1ed Mon Sep 17 00:00:00 2001 From: Korbut Kirill Date: Fri, 21 Jan 2022 03:29:14 +0200 Subject: [PATCH 8/9] Readme updates --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 60feb15..a37eb21 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,11 @@ For more about Crowdin API v2 see the documentation: [![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) ## Table of Contents +* [Requirements](#requirements) * [Installation](#installation) * [Quick Start](#quick-start) * [Initialization](#initialization) - * [How to call methods](#how-to-call-methods) + * [Usage](#usage) * [Command-Line Client](#command-line-client) * [Seeking Assistance](#seeking-assistance) * [Contributing](#contributing) From dafc90f40a92ea94ed1c5b9106e808da5052a8e3 Mon Sep 17 00:00:00 2001 From: Kirill Korbut Date: Mon, 14 Feb 2022 08:06:08 +0200 Subject: [PATCH 9/9] Crowdin delivery --- .gitignore | 4 +- .rubocop_todo.yml | 51 ++-- README.md | 9 +- bin/crowdin-console | 5 +- lib/crowdin-api.rb | 27 ++- lib/crowdin-api/api-resources/dictionaries.rb | 34 +++ .../api-resources/distributions.rb | 99 ++++++++ lib/crowdin-api/api-resources/glossaries.rb | 217 +++++++++++++++++ lib/crowdin-api/api-resources/labels.rb | 117 ++++++++++ .../machine_translation_engines.rb | 74 ++++++ lib/crowdin-api/api-resources/projects.rb | 16 +- lib/crowdin-api/api-resources/reports.rb | 138 +++++++++++ lib/crowdin-api/api-resources/screenshots.rb | 186 +++++++++++++++ lib/crowdin-api/api-resources/storages.rb | 2 +- .../api-resources/string_comments.rb | 73 ++++++ .../api-resources/string_translations.rb | 220 ++++++++++++++++++ lib/crowdin-api/api-resources/tasks.rb | 113 +++++++++ lib/crowdin-api/api-resources/teams.rb | 144 ++++++++++++ .../api-resources/translation_memory.rb | 145 ++++++++++++ lib/crowdin-api/api-resources/users.rb | 129 ++++++++++ lib/crowdin-api/api-resources/vendors.rb | 21 ++ lib/crowdin-api/api-resources/webhooks.rb | 73 ++++++ lib/crowdin-api/api-resources/workflows.rb | 4 +- lib/crowdin-api/client/client.rb | 72 +++--- lib/crowdin-api/core/api_errors_raiser.rb | 19 -- lib/crowdin-api/core/clc_errors_raiser.rb | 15 -- lib/crowdin-api/core/client_errors_raiser.rb | 12 - lib/crowdin-api/core/errors_raisers.rb | 39 ++++ lib/crowdin-api/core/request.rb | 4 +- lib/crowdin-api/core/utils.rb | 10 + 30 files changed, 1941 insertions(+), 131 deletions(-) create mode 100644 lib/crowdin-api/api-resources/dictionaries.rb create mode 100644 lib/crowdin-api/api-resources/distributions.rb create mode 100644 lib/crowdin-api/api-resources/glossaries.rb create mode 100644 lib/crowdin-api/api-resources/labels.rb create mode 100644 lib/crowdin-api/api-resources/machine_translation_engines.rb create mode 100644 lib/crowdin-api/api-resources/reports.rb create mode 100644 lib/crowdin-api/api-resources/screenshots.rb create mode 100644 lib/crowdin-api/api-resources/string_comments.rb create mode 100644 lib/crowdin-api/api-resources/string_translations.rb create mode 100644 lib/crowdin-api/api-resources/tasks.rb create mode 100644 lib/crowdin-api/api-resources/teams.rb create mode 100644 lib/crowdin-api/api-resources/translation_memory.rb create mode 100644 lib/crowdin-api/api-resources/users.rb create mode 100644 lib/crowdin-api/api-resources/vendors.rb create mode 100644 lib/crowdin-api/api-resources/webhooks.rb delete mode 100644 lib/crowdin-api/core/api_errors_raiser.rb delete mode 100644 lib/crowdin-api/core/clc_errors_raiser.rb delete mode 100644 lib/crowdin-api/core/client_errors_raiser.rb create mode 100644 lib/crowdin-api/core/errors_raisers.rb create mode 100644 lib/crowdin-api/core/utils.rb diff --git a/.gitignore b/.gitignore index 98bc812..cc2edc4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,12 @@ # Ignore Ruby logs -.ruby-version .idea # Ignore MacOS logs .DS_Store -# Ignore versioning file +# Ignore versioning files Gemfile.lock +.ruby-version # Ignore tests coverage folder coverage diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 780ebcb..cef7d7f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2022-01-05 07:49:15 UTC using RuboCop version 1.23.0. +# on 2022-02-14 06:05:44 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 @@ -50,24 +50,26 @@ Metrics/AbcSize: Metrics/BlockLength: Max: 54 -# Offense count: 4 +# Offense count: 12 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. Metrics/MethodLength: Max: 18 -# Offense count: 2 +# Offense count: 8 # Configuration parameters: CountComments, CountAsOne. Metrics/ModuleLength: - Max: 200 + Max: 199 -# Offense count: 1 -# Configuration parameters: CountKeywordArgs, MaxOptionalParameters. +# Offense count: 2 +# Configuration parameters: CountKeywordArgs. Metrics/ParameterLists: + MaxOptionalParameters: 4 Max: 6 -# Offense count: 1 +# Offense count: 2 Naming/AccessorMethodName: Exclude: + - 'lib/crowdin-api/api-resources/users.rb' - 'lib/crowdin-api/core/request.rb' # Offense count: 3 @@ -109,25 +111,10 @@ Style/CollectionCompact: Exclude: - 'lib/crowdin-api/core/request.rb' -# Offense count: 13 +# Offense count: 22 # Configuration parameters: AllowedConstants. Style/Documentation: - Exclude: - - 'spec/**/*' - - 'test/**/*' - - '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' - - 'lib/crowdin-api/api-resources/workflows.rb' - - 'lib/crowdin-api/client/configuration.rb' - - 'lib/crowdin-api/core/api_errors_raiser.rb' - - 'lib/crowdin-api/core/clc_errors_raiser.rb' - - 'lib/crowdin-api/core/client_errors_raiser.rb' - - 'lib/crowdin-api/core/request.rb' + Enabled: false # Offense count: 1 # Cop supports --auto-correct. @@ -141,3 +128,19 @@ Style/FrozenStringLiteralComment: Style/MixinUsage: Exclude: - 'bin/crowdin-console' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: implicit, explicit +Style/RescueStandardError: + Exclude: + - 'lib/crowdin-api/api-resources/labels.rb' + - 'lib/crowdin-api/api-resources/string_translations.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 122 diff --git a/README.md b/README.md index a37eb21..459e23e 100644 --- a/README.md +++ b/README.md @@ -120,15 +120,20 @@ projects = crowdin.list_projects(offset: 10, limit: 20) # Get specified project project = crowdin.get_project(your_project_id) +# Edit project +project = crowdin.edit_project(project_id, [{op: 'replace', + path: '/name', + value: 'your_new_project_name'}]) + # Add Storage storage = crowdin.add_storage(File.open('YourFilename.extension', 'r')) -# or you can specify only path to file +# or you can specify only absolute path to file storage = crowdin.add_storage('YourFilename.extension') # Download file file = crowdin.download_file(your_file_id, your_destination, your_project_id) # your_destination - filename or absolute path to file, optional -# Without destination option method will save file to current directory with a default filename +# Without destination option file will be saved to the current directory with a default filename # project_id is optional, as it can be initialized with a Crowdin Client # File revisions diff --git a/bin/crowdin-console b/bin/crowdin-console index 5891efa..c5a6f73 100755 --- a/bin/crowdin-console +++ b/bin/crowdin-console @@ -17,7 +17,6 @@ require 'optparse' # bundle exec crowdin-console --enterprise --api-token YOUR_ENTERPRISE_API_TOKEN --organization-domain YOUR_DOMAIN # -require 'crowdin-api/core/clc_errors_raiser' include Crowdin::Errors::ClcErrorsRaiser options = {} @@ -40,8 +39,8 @@ if options[:enterprise] @crowdin = Crowdin::Client.new do |config| config.api_token = options[:api_token] config.organization_domain = options[:organization_domain] - config.enable_logger = options[:enable_logger] || false - config.project_id = options[:project_id] || nil + config.enable_logger = options[:enable_logger] || false + config.project_id = options[:project_id] || nil end else @crowdin = Crowdin::Client.new do |config| diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index c57b04d..7cd665a 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -7,19 +7,34 @@ # Core modules require 'crowdin-api/core/errors' -require 'crowdin-api/core/api_errors_raiser' -require 'crowdin-api/core/client_errors_raiser' +require 'crowdin-api/core/errors_raisers' require 'crowdin-api/core/request' +require 'crowdin-api/core/utils' -# Api modules +# API modules +require 'crowdin-api/api-resources/storages' require 'crowdin-api/api-resources/languages' require 'crowdin-api/api-resources/projects' +require 'crowdin-api/api-resources/workflows' require 'crowdin-api/api-resources/source_files' -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' require 'crowdin-api/api-resources/source_strings' +require 'crowdin-api/api-resources/string_translations' +require 'crowdin-api/api-resources/string_comments' +require 'crowdin-api/api-resources/screenshots' +require 'crowdin-api/api-resources/glossaries' +require 'crowdin-api/api-resources/translation_memory' +require 'crowdin-api/api-resources/machine_translation_engines' +require 'crowdin-api/api-resources/reports' +require 'crowdin-api/api-resources/tasks' +require 'crowdin-api/api-resources/users' +require 'crowdin-api/api-resources/teams' +require 'crowdin-api/api-resources/vendors' +require 'crowdin-api/api-resources/webhooks' +require 'crowdin-api/api-resources/dictionaries' +require 'crowdin-api/api-resources/distributions' +require 'crowdin-api/api-resources/labels' +require 'crowdin-api/api-resources/translation_status' # Client modules require 'crowdin-api/client/version' diff --git a/lib/crowdin-api/api-resources/dictionaries.rb b/lib/crowdin-api/api-resources/dictionaries.rb new file mode 100644 index 0000000..85edb57 --- /dev/null +++ b/lib/crowdin-api/api-resources/dictionaries.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Dictionaries + def list_dictionaries(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/dictionaries", + query + ) + + request.perform + end + + def edit_dictionary(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 + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/dictionaries/#{language_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/distributions.rb b/lib/crowdin-api/api-resources/distributions.rb new file mode 100644 index 0000000..cdf67ee --- /dev/null +++ b/lib/crowdin-api/api-resources/distributions.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Distributions + def list_distributions(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/distributions", + query + ) + + request.perform + end + + def add_distribution(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/distributions", + query + ) + + request.perform + end + + def get_distribution(hash = nil, project_id = config.project_id) + hash || raise_parameter_is_required_error(:hash) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/distributions/#{hash}" + ) + + request.perform + end + + def delete_distribution(hash = nil, project_id = config.project_id) + hash || raise_parameter_is_required_error(:hash) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/distributions/#{hash}" + ) + + request.perform + end + + def edit_distribution(hash = nil, query = {}, project_id = config.project_id) + hash || raise_parameter_is_required_error(:hash) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/distributions/#{hash}", + query + ) + + request.perform + end + + def get_destribution_release(hash = nil, project_id = config.project_id) + hash || raise_parameter_is_required_error(:hash) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/distributions/#{hash}/release" + ) + + request.perform + end + + def release_distribution(hash = nil, project_id = config.project_id) + hash || raise_parameter_is_required_error(:hash) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/distributions/#{hash}/release" + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/glossaries.rb b/lib/crowdin-api/api-resources/glossaries.rb new file mode 100644 index 0000000..610b2cf --- /dev/null +++ b/lib/crowdin-api/api-resources/glossaries.rb @@ -0,0 +1,217 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Glossaries + def list_glossaries(query = {}) + request = Web::Request.new( + self, + :get, + '/glossaries', + query + ) + + request.perform + end + + def add_glossary(query = {}) + request = Web::Request.new( + self, + :post, + '/glossaries', + query + ) + + request.perform + end + + def get_glossary(glossary_id = nil, query = {}) + glossary_id || raise_parameter_is_required_error(:glossary_id) + + request = Web::Request.new( + self, + :get, + "/glossaries/#{glossary_id}", + query + ) + + request.perform + end + + def delete_glossary(glossary_id = nil, query = {}) + glossary_id || raise_parameter_is_required_error(:glossary_id) + + request = Web::Request.new( + self, + :delete, + "/glossaries/#{glossary_id}", + query + ) + + request.perform + end + + def edit_glossary(glossary_id = nil, query = {}) + glossary_id || raise_parameter_is_required_error(:glossary_id) + + request = Web::Request.new( + self, + :patch, + "/glossaries/#{glossary_id}", + query + ) + + request.perform + end + + def export_glossary(query = {}, glossary_id = nil, destination = nil) + glossary_id || raise_parameter_is_required_error(:glossary_id) + + request = Web::Request.new( + self, + :post, + "/glossaries/#{glossary_id}/exports", + query, + {}, + destination + ) + + request.perform + end + + def check_glossary_export_status(glossary_id = nil, export_id = nil) + glossary_id || raise_parameter_is_required_error(:glossary_id) + export_id || raise_parameter_is_required_error(:export_id) + + request = Web::Request.new( + self, + :get, + "/glossaries/#{glossary_id}/exports/#{export_id}" + ) + + request.perform + end + + def download_glossary(glossary_id = nil, export_id = nil, destination = nil) + glossary_id || raise_parameter_is_required_error(:glossary_id) + export_id || raise_parameter_is_required_error(:export_id) + + request = Web::Request.new( + self, + :get, + "/glossaries/#{glossary_id}/exports/#{export_id}/download", + {}, + {}, + destination + ) + + request.perform + end + + def import_glossary(glossary_id = nil, query = {}) + glossary_id || raise_parameter_is_required_error(:glossary_id) + + request = Web::Request.new( + self, + :post, + "/glossaries/#{glossary_id}/imports", + query + ) + + request.perform + end + + def check_glossary_import_status(glossary_id = nil, import_id = nil) + glossary_id || raise_parameter_is_required_error(:glossary_id) + import_id || raise_parameter_is_required_error(:import_id) + + request = Web::Request.new( + self, + :get, + "/glossaries/#{glossary_id}/imports/#{import_id}" + ) + + request.perform + end + + def list_terms(glossary_id = nil, query = {}) + glossary_id || raise_parameter_is_required_error(:glossary_id) + + request = Web::Request.new( + self, + :get, + "/glossaries/#{glossary_id}/terms", + query + ) + + request.perform + end + + def add_term(glossary_id = nil, query = {}) + glossary_id || raise_parameter_is_required_error(:glossary_id) + + request = Web::Request.new( + self, + :post, + "/glossaries/#{glossary_id}/terms", + query + ) + + request.perform + end + + def clear_glossary(glossary_id = nil, query = {}) + glossary_id || raise_parameter_is_required_error(:glossary_id) + + request = Web::Request.new( + self, + :delete, + "/glossaries/#{glossary_id}/terms", + query + ) + + request.perform + end + + def get_term(glossary_id = nil, term_id = nil) + glossary_id || raise_parameter_is_required_error(:glossary_id) + term_id || raise_parameter_is_required_error(:term_id) + + request = Web::Request.new( + self, + :get, + "/glossaries/#{glossary_id}/terms/#{term_id}" + ) + + request.perform + end + + def delete_term(glossary_id = nil, term_id = nil) + glossary_id || raise_parameter_is_required_error(:glossary_id) + term_id || raise_parameter_is_required_error(:term_id) + + request = Web::Request.new( + self, + :delete, + "/glossaries/#{glossary_id}/terms/#{term_id}" + ) + + request.perform + end + + def edit_term(glossary_id = nil, term_id = nil, query = {}) + glossary_id || raise_parameter_is_required_error(:glossary_id) + term_id || raise_parameter_is_required_error(:term_id) + + request = Web::Request.new( + self, + :patch, + "/glossaries/#{glossary_id}/terms/#{term_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/labels.rb b/lib/crowdin-api/api-resources/labels.rb new file mode 100644 index 0000000..4e1095d --- /dev/null +++ b/lib/crowdin-api/api-resources/labels.rb @@ -0,0 +1,117 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Labels + def list_lables(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/labels", + query + ) + + request.perform + end + + def add_label(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/labels", + query + ) + + request.perform + end + + def get_label(label_id = nil, project_id = config.project_id) + label_id || raise_parameter_is_required_error(:label_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/labels/#{label_id}" + ) + + request.perform + end + + def delete_label(label_id = nil, project_id = config.project_id) + label_id || raise_parameter_is_required_error(:label_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/labels/#{label_id}" + ) + + request.perform + end + + def edit_label(label_id = nil, query = {}, project_id = config.project_id) + label_id || raise_parameter_is_required_error(:label_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/labels/#{label_id}", + query + ) + + request.perform + end + + def assign_label_to_strings(label_id = nil, query = {}, project_id = config.project_id) + label_id || raise_parameter_is_required_error(:label_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/labels/#{label_id}/strings", + query + ) + + request.perform + end + + # For now Rest Client Resource aren't support payload for DELETE requests. + # So we have workaround through Rest Client Execute for this case + def unassign_label_from_strings(label_id = nil, query = {}, project_id = config.project_id) + # label_id || raise_parameter_is_required_error(:label_id) + # project_id || raise_project_id_is_required_error + # + # request = Web::Request.new( + # self, + # :delete, + # "/projects/#{project_id}/labels/#{label_id}", + # query + # ) + # + # request.perform + + response = ::RestClient::Request.execute( + { + method: :delete, + url: config.base_url + config.target_api_url + "/projects/#{project_id}/labels/#{label_id}", + payload: query.to_json + }.merge(@options) + ) + + begin + JSON.parse(response) + rescue + {} + end + end + end + end +end diff --git a/lib/crowdin-api/api-resources/machine_translation_engines.rb b/lib/crowdin-api/api-resources/machine_translation_engines.rb new file mode 100644 index 0000000..a3608e4 --- /dev/null +++ b/lib/crowdin-api/api-resources/machine_translation_engines.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + # -- For Enterprise mode only -- + module MachineTranslationEngines + def list_mts(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + self, + :get, + '/mts', + query + ) + + request.perform + end + + def add_mt(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + self, + :post, + '/mts', + query + ) + + request.perform + end + + def get_mt(mt_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + mt_id || raise_parameter_is_required_error(:mt_id) + + request = Web::Request.new( + self, + :get, + "/mts/#{mt_id}" + ) + + request.perform + end + + def delete_mt(mt_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + mt_id || raise_parameter_is_required_error(:mt_id) + + request = Web::Request.new( + self, + :delete, + "/mts/#{mt_id}" + ) + + request.perform + end + + def edit_mt(mt_id = nil, query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + mt_id || raise_parameter_is_required_error(:mt_id) + + request = Web::Request.new( + self, + :patch, + "/mts/#{mt_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/projects.rb b/lib/crowdin-api/api-resources/projects.rb index 209898d..aefe84a 100644 --- a/lib/crowdin-api/api-resources/projects.rb +++ b/lib/crowdin-api/api-resources/projects.rb @@ -49,6 +49,20 @@ def delete_project(project_id = nil) request.perform end + # + # Edit project. + # + # === Parameters + # + # * +:projectId+ [Integer] - Project Identifier. Get via list_projects. Can be initialized with the Crowdin Client + # * +:value+ [String] - String or Array of strings. + # * +:op+ [String] - Path operation to perform. + # * +:projectId+ [String ] - Enum, for instance '/name'. Full list you can see on the developers site. + # + # === Example + # + # crowdin.edit_project(project_id, [{op: 'replace', path: '/name', value: 'your_new_project_name'}]) + # def edit_project(project_id = nil, query = {}) project_id || raise_parameter_is_required_error(:project_id) @@ -62,9 +76,7 @@ def edit_project(project_id = nil, query = {}) request.perform end - # # -- For Enterprise mode only -- - # def list_groups(query = {}) config.enterprise_mode? || raise_only_for_enterprise_mode_error diff --git a/lib/crowdin-api/api-resources/reports.rb b/lib/crowdin-api/api-resources/reports.rb new file mode 100644 index 0000000..a28b071 --- /dev/null +++ b/lib/crowdin-api/api-resources/reports.rb @@ -0,0 +1,138 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Reports + def generate_report(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/reports", + query + ) + + request.perform + end + + def check_report_generation_status(report_id = nil, project_id = config.project_id) + report_id || raise_parameter_is_required_error(:report_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/reports/#{report_id}" + ) + + request.perform + end + + def download_report(report_id = nil, destination = nil, project_id = config.project_id) + report_id || raise_parameter_is_required_error(:report_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/reports/#{report_id}/download", + {}, + {}, + destination + ) + + request.perform + end + + # -- For Enterprise mode only -- + + def generate_group_report(group_id = nil, query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) + + request = Web::Request.new( + self, + :post, + "/groups/#{group_id}/reports", + query + ) + + request.perform + end + + def check_group_report_generation_status(group_id = nil, report_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) + report_id || raise_parameter_is_required_error(:report_id) + + request = Web::Request.new( + self, + :get, + "/groups/#{group_id}/reports/#{report_id}" + ) + + request.perform + end + + def download_group_report(group_id = nil, report_id = nil, destination = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) + report_id || raise_parameter_is_required_error(:report_id) + + request = Web::Request.new( + self, + :get, + "/groups/#{group_id}/reports/#{report_id}/download", + {}, + {}, + destination + ) + + request.perform + end + + def generate_organization_report(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + self, + :post, + '/reports', + query + ) + + request.perform + end + + def check_organization_report_generation_status(report_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + report_id || raise_parameter_is_required_error(:report_id) + + request = Web::Request.new( + self, + :get, + "/reports/#{report_id}" + ) + + request.perform + end + + def download_organization_report(report_id = nil, destination = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + report_id || raise_parameter_is_required_error(:report_id) + + request = Web::Request.new( + self, + :get, + "/reports/#{report_id}/download", + {}, + {}, + destination + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/screenshots.rb b/lib/crowdin-api/api-resources/screenshots.rb new file mode 100644 index 0000000..524d2ed --- /dev/null +++ b/lib/crowdin-api/api-resources/screenshots.rb @@ -0,0 +1,186 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Screenshots + def list_screenshots(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/screenshots", + query + ) + + request.perform + end + + def add_screenshot(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/screenshots", + query + ) + + request.perform + end + + def get_screenshot(screenshot_id = nil, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/screenshots/#{screenshot_id}" + ) + + request.perform + end + + def update_screenshot(screenshot_id = nil, query = {}, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :put, + "/projects/#{project_id}/screenshots/#{screenshot_id}", + query + ) + + request.perform + end + + def delete_screenshot(screenshot_id = nil, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/screenshots/#{screenshot_id}" + ) + + request.perform + end + + def edit_screenshot(screenshot_id = nil, query = {}, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/screenshots/#{screenshot_id}", + query + ) + + request.perform + end + + def list_tags(screenshot_id = nil, query = {}, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/screenshots/#{screenshot_id}/tags", + query + ) + + request.perform + end + + # Auto tag + def replace_tags(screenshot_id = nil, query = {}, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :put, + "/projects/#{project_id}/screenshots/#{screenshot_id}/tags", + query + ) + + request.perform + end + + def add_tag(screenshot_id = nil, query = {}, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/screenshots/#{screenshot_id}/tags", + query + ) + + request.perform + end + + def clear_tags(screenshot_id = nil, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/screenshots/#{screenshot_id}/tags" + ) + + request.perform + end + + def get_tag(screenshot_id = nil, tag_id = nil, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + tag_id || raise_parameter_is_required_error(:tag_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}" + ) + + request.perform + end + + def delete_tag(screenshot_id = nil, tag_id = nil, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + tag_id || raise_parameter_is_required_error(:tag_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}" + ) + + request.perform + end + + def edit_tag(screenshot_id = nil, tag_id = nil, query = {}, project_id = config.project_id) + screenshot_id || raise_parameter_is_required_error(:screenshot_id) + tag_id || raise_parameter_is_required_error(:tag_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/storages.rb b/lib/crowdin-api/api-resources/storages.rb index 1a4b1b1..3676890 100644 --- a/lib/crowdin-api/api-resources/storages.rb +++ b/lib/crowdin-api/api-resources/storages.rb @@ -32,7 +32,7 @@ def list_storages(query = {}) # # === Parameters # - # * +File+ - File class object or path to file + # * +File+ - File class object or full path to file # # === Example # diff --git a/lib/crowdin-api/api-resources/string_comments.rb b/lib/crowdin-api/api-resources/string_comments.rb new file mode 100644 index 0000000..ee18860 --- /dev/null +++ b/lib/crowdin-api/api-resources/string_comments.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module StringComments + def list_string_comments(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/comments", + query + ) + + request.perform + end + + def add_string_comment(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/comments", + query + ) + + request.perform + end + + def get_string_comment(string_comment_id = nil, project_id = config.project_id) + string_comment_id || raise_parameter_is_required_error(:string_comment_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/comments/#{string_comment_id}" + ) + + request.perform + end + + def delete_string_comment(string_comment_id = nil, project_id = config.project_id) + string_comment_id || raise_parameter_is_required_error(:string_comment_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/comments/#{string_comment_id}" + ) + + request.perform + end + + def edit_string_comment(string_comment_id = nil, query = {}, project_id = config.project_id) + string_comment_id || raise_parameter_is_required_error(:string_comment_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/comments/#{string_comment_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/string_translations.rb b/lib/crowdin-api/api-resources/string_translations.rb new file mode 100644 index 0000000..ed65876 --- /dev/null +++ b/lib/crowdin-api/api-resources/string_translations.rb @@ -0,0 +1,220 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module StringTranslations + def list_translation_approvals(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/approvals", + query + ) + + request.perform + end + + def add_approval(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/approvals", + query + ) + + request.perform + end + + def get_approval(approval_id = nil, project_id = config.project_id) + approval_id || raise_parameter_is_required_error(:approval_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/approvals/#{approval_id}" + ) + + request.perform + end + + def remove_approval(approval_id = nil, project_id = config.project_id) + approval_id || raise_parameter_is_required_error(:approval_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/approvals/#{approval_id}" + ) + + request.perform + end + + def list_language_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 + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/languages/#{language_id}/translations", + query + ) + + request.perform + end + + def list_string_translations(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/translations", + query + ) + + request.perform + end + + def add_translation(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/translations", + query + ) + + request.perform + end + + # For now Rest Client Resource aren't support payload for DELETE requests. + # So we have workaround through Rest Client Execute for this case + def delete_string_translations(query = {}, project_id = config.project_id) + # project_id || raise_project_id_is_required_error + # + # request = Web::Request.new( + # self, + # :delete, + # "/projects/#{project_id}/translations", + # query + # ) + # + # request.perform + + response = ::RestClient::Request.execute( + { + method: :delete, + url: config.base_url + config.target_api_url + "/projects/#{project_id}/translations", + payload: query.to_json + }.merge(@options) + ) + + begin + JSON.parse(response) + rescue + {} + end + end + + def get_translation(trnslation_id = nil, query = {}, project_id = config.project_id) + trnslation_id || raise_parameter_is_required_error(:trnslation_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/translations/#{trnslation_id}", + query + ) + + request.perform + end + + def restore_translation(trnslation_id = nil, project_id = config.project_id) + trnslation_id || raise_parameter_is_required_error(:trnslation_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :put, + "/projects/#{project_id}/translations/#{trnslation_id}" + ) + + request.perform + end + + def delete_translation(trnslation_id = nil, project_id = config.project_id) + trnslation_id || raise_parameter_is_required_error(:trnslation_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/translations/#{trnslation_id}" + ) + + request.perform + end + + def list_translation_votes(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/votes", + query + ) + + request.perform + end + + def add_vote(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/votes", + query + ) + + request.perform + end + + def get_vote(vote_id = nil, project_id = config.project_id) + vote_id || raise_parameter_is_required_error(:vote_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/votes/#{vote_id}" + ) + + request.perform + end + + def edit_screenshot(vote_id = nil, project_id = config.project_id) + vote_id || raise_parameter_is_required_error(:vote_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/votes/#{vote_id}" + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/tasks.rb b/lib/crowdin-api/api-resources/tasks.rb new file mode 100644 index 0000000..fdda7d6 --- /dev/null +++ b/lib/crowdin-api/api-resources/tasks.rb @@ -0,0 +1,113 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Tasks + def list_tasks(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/tasks", + query + ) + + request.perform + end + + def add_task(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/tasks", + query + ) + + request.perform + end + + def export_task_strings(task_id = nil, destination = nil, project_id = config.project_id) + task_id || raise_parameter_is_required_error(:task_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/tasks/#{task_id}/exports", + {}, + {}, + destination + ) + + request.perform + end + + def get_task(task_id = nil, project_id = config.project_id) + task_id || raise_parameter_is_required_error(:task_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/tasks/#{task_id}" + ) + + request.perform + end + + def delete_task(task_id = nil, project_id = config.project_id) + task_id || raise_parameter_is_required_error(:task_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/tasks/#{task_id}" + ) + + request.perform + end + + def edit_task(task_id = nil, query = {}, project_id = config.project_id) + task_id || raise_parameter_is_required_error(:task_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/tasks/#{task_id}", + query + ) + + request.perform + end + + def list_user_tasks(query = {}) + request = Web::Request.new( + self, + :get, + '/user/tasks', + query + ) + + request.perform + end + + def edit_task_archived_status(task_id = nil, query = {}) + task_id || raise_parameter_is_required_error(:task_id) + + request = Web::Request.new( + self, + :get, + "/user/tasks/#{task_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/teams.rb b/lib/crowdin-api/api-resources/teams.rb new file mode 100644 index 0000000..a4567db --- /dev/null +++ b/lib/crowdin-api/api-resources/teams.rb @@ -0,0 +1,144 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + # -- For Enterprise mode only -- + module Teams + def add_team_to_project(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, + :post, + "/projects/#{project_id}/teams", + query + ) + + request.perform + end + + def list_teams(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + self, + :get, + '/teams', + query + ) + + request.perform + end + + def add_team(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + self, + :post, + '/teams', + query + ) + + request.perform + end + + def get_team(team_id = nil, query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + self, + :get, + "/teams/#{team_id}", + query + ) + + request.perform + end + + def delete_team(team_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + self, + :delete, + "/teams/#{team_id}" + ) + + request.perform + end + + def edit_team(team_id = nil, query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + self, + :patch, + "/teams/#{team_id}", + query + ) + + request.perform + end + + def team_members_list(team_id = nil, query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + self, + :get, + "/teams/#{team_id}/members", + query + ) + + request.perform + end + + def add_team_members(team_id = nil, query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + self, + :post, + "/teams/#{team_id}/members", + query + ) + + request.perform + end + + def delete_all_team_members(team_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + self, + :delete, + "/teams/#{team_id}/members" + ) + + request.perform + end + + def delete_team_member(team_id = nil, member_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + member_id || raise_parameter_is_required_error(:member_id) + + request = Web::Request.new( + self, + :delete, + "/teams/#{team_id}/members/#{member_id}" + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/translation_memory.rb b/lib/crowdin-api/api-resources/translation_memory.rb new file mode 100644 index 0000000..ae33513 --- /dev/null +++ b/lib/crowdin-api/api-resources/translation_memory.rb @@ -0,0 +1,145 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module TranslationMemory + def list_tms(query = {}) + request = Web::Request.new( + self, + :get, + '/tms', + query + ) + + request.perform + end + + def add_tm(query = {}) + request = Web::Request.new( + self, + :post, + '/tms', + query + ) + + request.perform + end + + def get_tm(tm_id = nil) + tm_id || raise_parameter_is_required_error(:tm_id) + + request = Web::Request.new( + self, + :get, + "/tms/#{tm_id}" + ) + + request.perform + end + + def delete_tm(tm_id = nil) + tm_id || raise_parameter_is_required_error(:tm_id) + + request = Web::Request.new( + self, + :delete, + "/tms/#{tm_id}" + ) + + request.perform + end + + def edit_tm(tm_id = nil, query = {}) + tm_id || raise_parameter_is_required_error(:tm_id) + + request = Web::Request.new( + self, + :delete, + "/tms/#{tm_id}", + query + ) + + request.perform + end + + def clear_tm(tm_id = nil) + tm_id || raise_parameter_is_required_error(:tm_id) + + request = Web::Request.new( + self, + :delete, + "/tms/#{tm_id}/segments" + ) + + request.perform + end + + def export_tm(tm_id = nil) + tm_id || raise_parameter_is_required_error(:tm_id) + + request = Web::Request.new( + self, + :post, + "/tms/#{tm_id}/exports" + ) + + request.perform + end + + def check_tm_export_status(tm_id = nil, export_id = nil) + tm_id || raise_parameter_is_required_error(:tm_id) + export_id || raise_parameter_is_required_error(:export_id) + + request = Web::Request.new( + self, + :get, + "/tms/#{tm_id}/exports/#{export_id}" + ) + + request.perform + end + + def download_tm(tm_id = nil, export_id = nil, destination = nil) + tm_id || raise_parameter_is_required_error(:tm_id) + export_id || raise_parameter_is_required_error(:export_id) + + request = Web::Request.new( + self, + :get, + "/tms/#{tm_id}/exports/#{export_id}/download", + {}, + {}, + destination + ) + + request.perform + end + + def import_tm(tm_id = nil, query = {}) + tm_id || raise_parameter_is_required_error(:tm_id) + + request = Web::Request.new( + self, + :post, + "/tms/#{tm_id}/imports", + query + ) + + request.perform + end + + def check_tm_import_status(tm_id = nil, import_id = nil) + tm_id || raise_parameter_is_required_error(:tm_id) + import_id || raise_parameter_is_required_error(:import_id) + + request = Web::Request.new( + self, + :get, + "/tms/#{tm_id}/imports/#{import_id}" + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/users.rb b/lib/crowdin-api/api-resources/users.rb new file mode 100644 index 0000000..8a09a8b --- /dev/null +++ b/lib/crowdin-api/api-resources/users.rb @@ -0,0 +1,129 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Users + def get_authenticated_user + request = Web::Request.new( + self, + :get, + '/user' + ) + + request.perform + end + + def list_project_members(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/members", + query + ) + + request.perform + end + + def get_member_info(member_id = nil, project_id = config.project_id) + member_id || raise_parameter_is_required_error(:member_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/members/#{member_id}" + ) + + request.perform + end + + # -- For Enterprise mode only -- + + def add_project_member(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, + :post, + "/projects/#{project_id}/members", + query + ) + + request.perform + end + + def get_project_member_permissions(member_id = nil, project_id = config.project_id) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + member_id || raise_parameter_is_required_error(:member_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/members/#{member_id}" + ) + + request.perform + end + + def replace_project_permissions(member_id = nil, query = {}, project_id = config.project_id) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + member_id || raise_parameter_is_required_error(:member_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :put, + "/projects/#{project_id}/members/#{member_id}", + query + ) + + request.perform + end + + def delete_member_from_project(member_id = nil, query = {}, project_id = config.project_id) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + member_id || raise_parameter_is_required_error(:member_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/members/#{member_id}", + query + ) + + request.perform + end + + def list_users(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + self, + :get, + '/users', + query + ) + + request.perform + end + + def get_user(user_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + user_id || raise_parameter_is_required_error(:user_id) + + request = Web::Request.new( + self, + :get, + "/users/#{user_id}" + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/vendors.rb b/lib/crowdin-api/api-resources/vendors.rb new file mode 100644 index 0000000..c8c2a8a --- /dev/null +++ b/lib/crowdin-api/api-resources/vendors.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + # -- For Enterprise mode only -- + module Vendors + def list_vendors(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + self, + :get, + '/vendors', + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/webhooks.rb b/lib/crowdin-api/api-resources/webhooks.rb new file mode 100644 index 0000000..648a1d2 --- /dev/null +++ b/lib/crowdin-api/api-resources/webhooks.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Webhooks + def list_webhooks(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/webhooks", + query + ) + + request.perform + end + + def add_webhook(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/webhooks", + query + ) + + request.perform + end + + def get_webhook(webhook_id = nil, project_id = config.project_id) + webhook_id || raise_parameter_is_required_error(:webhook_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/webhooks/#{webhook_id}" + ) + + request.perform + end + + def delete_webhook(webhook_id = nil, project_id = config.project_id) + webhook_id || raise_parameter_is_required_error(:webhook_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/webhooks/#{webhook_id}" + ) + + request.perform + end + + def edit_screenshot(webhook_id = nil, query = {}, project_id = config.project_id) + webhook_id || raise_parameter_is_required_error(:webhook_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/webhooks/#{webhook_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/workflows.rb b/lib/crowdin-api/api-resources/workflows.rb index ffb0673..341ec06 100644 --- a/lib/crowdin-api/api-resources/workflows.rb +++ b/lib/crowdin-api/api-resources/workflows.rb @@ -1,10 +1,8 @@ # frozen_string_literal: true -# -# -- For Enterprise mode only -- -# module Crowdin module ApiResources + # -- For Enterprise mode only -- module Workflows def list_workflow_steps(query = {}, project_id = config.project_id) config.enterprise_mode? || raise_only_for_enterprise_mode_error diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 901229c..16962ca 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -1,53 +1,45 @@ # frozen_string_literal: true # -# The Crowdin::Client library is used for interactions with a crowdin.com website. +# A wrapper and interface to the Crowdin API. Please visit the Crowdin developers site +# for a full explanation of what each of the Crowdin api methods expect and perform. # -# == Example -# -# require 'crowdin-api' -# -# crowdin = Crowdin::Client.new do |config| -# config.api_token = 'YOUR_API_TOKEN' -# end -# -# crowdin.list_projects +# https://support.crowdin.com/api/v2/ +# https://support.crowdin.com/enterprise/api/ # module Crowdin # - # A wrapper and interface to the Crowdin api. Please visit the Crowdin developers site - # for a full explanation of what each of the Crowdin api methods expect and perform. + # === Example # - # https://support.crowdin.com/api/v2/ + # require 'crowdin-api' + # + # crowdin = Crowdin::Client.new do |config| + # config.api_token = 'YOUR_API_TOKEN' + # end + # + # crowdin.list_projects # class Client - include ApiResources::Storages - include ApiResources::Languages - include ApiResources::Projects - include ApiResources::Workflows # Enterprise - include ApiResources::SourceFiles - include ApiResources::Translations - include ApiResources::SourceStrings - # Sting Translations - # String Comments - # Screenshots - # Glossaries - # Translation Memory - # Machine Translation Engines # Enterprise - include ApiResources::TranslationStatus - # Reports - # Tasks - # Issues - # Users - # Teams # Enterprise - # Vendors # Enterprise - # Webhooks - # Dictionaries - # Distributions - # Labels - - include Errors::ApiErrorsRaiser - include Errors::ClientErrorsRaiser + extend Utils + + # API Resources modules + API_RESOURCES_MODULES = %i[Storages Languages Projects Workflows SourceFiles Translations SourceStrings + StringTranslations StringComments Screenshots Glossaries TranslationMemory + MachineTranslationEngines Reports Tasks Users Teams Vendors Webhooks + Dictionaries Distributions Labels TranslationStatus].freeze + + # Error Raisers modules + ERROR_RAISERS_MODULES = %i[ApiErrorsRaiser ClientErrorsRaiser].freeze + + # Processing all API Resources modules to include them to the Client + API_RESOURCES_MODULES.each do |module_name| + Client.send(:include, fetch_module_full_name_from_string("Crowdin::ApiResources::#{module_name}")) + end + + # Processing all Error Raisers modules to include them to the Client + ERROR_RAISERS_MODULES.each do |module_name| + Client.send(:include, fetch_module_full_name_from_string("Crowdin::Errors::#{module_name}")) + end attr_reader :config attr_reader :connection diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb deleted file mode 100644 index 15167cf..0000000 --- a/lib/crowdin-api/core/api_errors_raiser.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module Errors - module ApiErrorsRaiser - def raise_only_for_enterprise_mode_error - raise(OnlyForEnterpriseModeError, 'This method can be called only for Enterprise mode') - end - - def raise_project_id_is_required_error - 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 - end - end -end diff --git a/lib/crowdin-api/core/clc_errors_raiser.rb b/lib/crowdin-api/core/clc_errors_raiser.rb deleted file mode 100644 index c1d539c..0000000 --- a/lib/crowdin-api/core/clc_errors_raiser.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module Errors - module ClcErrorsRaiser - def raise_api_token_is_required_error - raise(ArgumentError, '--api-token option is required') - end - - def raise_organization_domain_is_required_error - raise(ArgumentError, '--organization-domain option is required for Enterprise mode') - end - end - end -end diff --git a/lib/crowdin-api/core/client_errors_raiser.rb b/lib/crowdin-api/core/client_errors_raiser.rb deleted file mode 100644 index d812f93..0000000 --- a/lib/crowdin-api/core/client_errors_raiser.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module Errors - module ClientErrorsRaiser - def raise_logger_are_not_enabled_error - raise(LoggerAreNotEnabledError, 'Logger are not enabled in your Client configuration, enable it ' \ - 'before setting your own logger') - end - end - end -end diff --git a/lib/crowdin-api/core/errors_raisers.rb b/lib/crowdin-api/core/errors_raisers.rb new file mode 100644 index 0000000..ff39c0f --- /dev/null +++ b/lib/crowdin-api/core/errors_raisers.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Crowdin + module Errors + # Client errors raiser + module ClientErrorsRaiser + def raise_logger_are_not_enabled_error + raise(LoggerAreNotEnabledError, 'Logger are not enabled in your Client configuration, enable it ' \ + 'before setting your own logger') + end + end + + # Command-Line Client errors raiser + module ClcErrorsRaiser + def raise_api_token_is_required_error + raise(ArgumentError, '--api-token option is required') + end + + def raise_organization_domain_is_required_error + raise(ArgumentError, '--organization-domain option is required for Enterprise mode') + end + end + + # API errors raiser + module ApiErrorsRaiser + def raise_only_for_enterprise_mode_error + raise(OnlyForEnterpriseModeError, 'This method can be called only for Enterprise mode') + end + + def raise_project_id_is_required_error + 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 + end + end +end diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index 10d95df..5b73153 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -30,7 +30,7 @@ def process_request! rescue StandardError => error client.log! error - @errors << "Something went wrong while proccessing request. Details - #{error.class}" + @errors << "Something went wrong while request proccessing. Details - #{error.class}" end def process_response! @@ -55,7 +55,7 @@ def process_response! rescue StandardError => error client.log! error - @errors << "Something went wrong while proccessing response. Details - #{error.class}" + @errors << "Something went wrong while response proccessing. Details - #{error.class}" fetch_errors end diff --git a/lib/crowdin-api/core/utils.rb b/lib/crowdin-api/core/utils.rb new file mode 100644 index 0000000..80320c0 --- /dev/null +++ b/lib/crowdin-api/core/utils.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Crowdin + module Utils + # Method for extract module namespaces from a string + def fetch_module_full_name_from_string(string) + string.split('::').inject(Module) { |acc, val| acc.const_get(val) } + end + end +end