From dafc90f40a92ea94ed1c5b9106e808da5052a8e3 Mon Sep 17 00:00:00 2001 From: Kirill Korbut Date: Mon, 14 Feb 2022 08:06:08 +0200 Subject: [PATCH] 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