diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index cef7d7f..b739bdc 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,13 +1,13 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2022-02-14 06:05:44 UTC using RuboCop version 1.23.0. +# on 2022-05-09 23:17:26 UTC using RuboCop version 1.26.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 # versions of RuboCop, may require this file to be generated again. # Offense count: 1 -# Cop supports --auto-correct. +# This cop supports safe auto-correction (--auto-correct). # Configuration parameters: Include. # Include: **/*.gemspec Gemspec/RequireMFA: @@ -21,8 +21,19 @@ Gemspec/RequiredRubyVersion: Exclude: - 'crowdin-api.gemspec' +# Offense count: 18 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. +Layout/ExtraSpacing: + Exclude: + - 'lib/crowdin-api/api_resources/machine_translation_engines.rb' + - 'lib/crowdin-api/api_resources/reports.rb' + - 'lib/crowdin-api/api_resources/teams.rb' + - 'lib/crowdin-api/api_resources/users.rb' + - 'lib/crowdin-api/api_resources/workflows.rb' + # Offense count: 4 -# Cop supports --auto-correct. +# This cop supports safe auto-correction (--auto-correct). # Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. # SupportedHashRocketStyles: key, separator, table # SupportedColonStyles: key, separator, table @@ -31,36 +42,68 @@ Layout/HashAlignment: Exclude: - 'lib/crowdin-api/client/configuration.rb' -# Offense count: 1 -# Cop supports --auto-correct. +# Offense count: 13 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: normal, indented_internal_methods +Layout/IndentationConsistency: + Exclude: + - 'lib/crowdin-api/client/client.rb' + - 'lib/crowdin-api/core/request.rb' + - 'lib/crowdin-api/core/send_request.rb' + +# Offense count: 13 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: Width, IgnoredPatterns. +Layout/IndentationWidth: + Exclude: + - 'lib/crowdin-api/client/client.rb' + - 'lib/crowdin-api/core/request.rb' + - 'lib/crowdin-api/core/send_request.rb' + +# Offense count: 22 +# This cop supports safe auto-correction (--auto-correct). # Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator. # SupportedStylesForExponentOperator: space, no_space Layout/SpaceAroundOperators: Exclude: - 'crowdin-api.gemspec' + - 'lib/crowdin-api/api_resources/machine_translation_engines.rb' + - 'lib/crowdin-api/api_resources/reports.rb' + - 'lib/crowdin-api/api_resources/teams.rb' + - 'lib/crowdin-api/api_resources/users.rb' + - 'lib/crowdin-api/api_resources/workflows.rb' + - 'spec/api_resources/projects_spec.rb' + +# Offense count: 1 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods. +Lint/UnusedMethodArgument: + Exclude: + - 'lib/crowdin-api/core/send_request.rb' # Offense count: 2 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: Max: 23 -# Offense count: 1 +# Offense count: 4 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. # IgnoredMethods: refine Metrics/BlockLength: - Max: 54 + Max: 171 -# Offense count: 12 +# Offense count: 13 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. Metrics/MethodLength: Max: 18 -# Offense count: 8 +# Offense count: 9 # Configuration parameters: CountComments, CountAsOne. Metrics/ModuleLength: Max: 199 -# Offense count: 2 +# Offense count: 3 # Configuration parameters: CountKeywordArgs. Metrics/ParameterLists: MaxOptionalParameters: 4 @@ -69,55 +112,62 @@ Metrics/ParameterLists: # Offense count: 2 Naming/AccessorMethodName: Exclude: - - 'lib/crowdin-api/api-resources/users.rb' - - 'lib/crowdin-api/core/request.rb' + - 'lib/crowdin-api/api_resources/users.rb' + - 'lib/crowdin-api/core/send_request.rb' -# Offense count: 3 +# Offense count: 1 # Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, CheckDefinitionPathHierarchyRoots, Regex, IgnoreExecutableScripts, AllowedAcronyms. # CheckDefinitionPathHierarchyRoots: lib, spec, test, src # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS Naming/FileName: Exclude: - 'lib/crowdin-api.rb' - - 'spec/client/client-instance_spec.rb' - - 'spec/client/configuration-instance_spec.rb' -# Offense count: 3 +# Offense count: 2 # Configuration parameters: EnforcedStyleForLeadingUnderscores. # SupportedStylesForLeadingUnderscores: disallowed, required, optional Naming/MemoizedInstanceVariableName: Exclude: - 'lib/crowdin-api/client/client.rb' -# Offense count: 3 -# Cop supports --auto-correct. +# Offense count: 6 +# This cop supports safe auto-correction (--auto-correct). # Configuration parameters: PreferredName. Naming/RescuedExceptionsVariableName: Exclude: - - 'lib/crowdin-api/core/request.rb' + - 'lib/crowdin-api/core/send_request.rb' -# Offense count: 8 -# Cop supports --auto-correct. +# Offense count: 7 +# This cop supports safe auto-correction (--auto-correct). # Configuration parameters: EnforcedStyle. # SupportedStyles: separated, grouped Style/AccessorGrouping: Exclude: - - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' + - 'lib/crowdin-api/core/request.rb' -# Offense count: 2 -# Cop supports --auto-correct. +# Offense count: 4 +# This cop supports unsafe auto-correction (--auto-correct-all). Style/CollectionCompact: Exclude: - 'lib/crowdin-api/core/request.rb' + - 'lib/crowdin-api/core/send_request.rb' -# Offense count: 22 +# Offense count: 24 # Configuration parameters: AllowedConstants. Style/Documentation: Enabled: false # Offense count: 1 -# Cop supports --auto-correct. +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: allowed_in_returns, forbidden +Style/DoubleNegation: + Exclude: + - 'lib/crowdin-api/client/configuration.rb' + +# Offense count: 1 +# This cop supports safe auto-correction (--auto-correct). # Configuration parameters: EnforcedStyle. # SupportedStyles: always, always_true, never Style/FrozenStringLiteralComment: @@ -130,17 +180,32 @@ Style/MixinUsage: - 'bin/crowdin-console' # Offense count: 2 -# Cop supports --auto-correct. +# This cop supports safe auto-correction (--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' + - 'lib/crowdin-api/api_resources/labels.rb' + - 'lib/crowdin-api/api_resources/string_translations.rb' + +# Offense count: 1 +# This cop supports unsafe auto-correction (--auto-correct-all). +# Configuration parameters: Mode. +Style/StringConcatenation: + Exclude: + - 'lib/crowdin-api/api_resources/projects.rb' + +# Offense count: 8 +# This cop supports safe auto-correction (--auto-correct). +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Exclude: + - 'spec/api_resources/projects_spec.rb' # Offense count: 1 -# Cop supports --auto-correct. +# This cop supports safe auto-correction (--auto-correct). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Layout/LineLength: - Max: 122 + Max: 121 diff --git a/README.md b/README.md index 2846e9a..e89a333 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ For more about Crowdin API v2 see the documentation: Add this line to your application's Gemfile: ```gemfile -gem 'crowdin-api', '~> 1.2.1' +gem 'crowdin-api', '~> 1.3.0' ``` And then execute: @@ -74,7 +74,7 @@ crowdin = Crowdin::Client.new do |config| config.api_token = 'YourApiToken' end -# Or you can intialize Enterprise Client instance by specifying your +# Or you can initialize Enterprise Client instance by specifying your # organization_domain in config options crowdin = Crowdin::Client.new do |config| config.api_token = 'YourEnterpriseApiToken' @@ -82,7 +82,7 @@ crowdin = Crowdin::Client.new do |config| end # Note: we use full specified organization domain if that includes '.com' # config.organization_domain = your_domain -> https://your_domain.api.crowdin.com -# config.organization_domain = your_domain.com -> your_domain.com +# config.organization_domain = your_domain.com -> https://your_domain.com # All supported Crowdin Client config options now: crowdin = Crowdin::Client.new do |config| @@ -121,9 +121,9 @@ projects = crowdin.list_projects(offset: 10, limit: 20) project = crowdin.get_project(your_project_id) # Edit project -project = crowdin.edit_project(project_id, [{op: 'replace', - path: '/name', - value: 'your_new_project_name'}]) +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')) @@ -141,6 +141,8 @@ file = crowdin.download_file(your_file_id, your_destination, your_project_id) file_revisions = crowdin.list_file_revisions(your_file_id, limit: 10) # or you can specify your project_id file_revisions = crowdin.list_file_revisions(your_file_id, { limit: 10 }, your_project_id) + +# Note: more examples you can find in spec folder ``` ### Command-Line Client diff --git a/bin/crowdin-console b/bin/crowdin-console index c5a6f73..e9b561c 100755 --- a/bin/crowdin-console +++ b/bin/crowdin-console @@ -12,7 +12,7 @@ require 'optparse' # # bundle exec crowdin-console --enable-logger --api-token YOUR_API_TOKEN --project-id YOUR_PROJECT_ID # -# or Enterpise API: +# or Enterprise API: # # bundle exec crowdin-console --enterprise --api-token YOUR_ENTERPRISE_API_TOKEN --organization-domain YOUR_DOMAIN # diff --git a/bin/setup b/bin/setup deleted file mode 100755 index 180bb73..0000000 --- a/bin/setup +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -set -euo pipefail -IFS=$'\n\t' - -bundle install diff --git a/crowdin-api.gemspec b/crowdin-api.gemspec index f352fce..7698a06 100644 --- a/crowdin-api.gemspec +++ b/crowdin-api.gemspec @@ -13,7 +13,6 @@ Gem::Specification.new do |gem| gem.license = 'MIT' gem.files = `git ls-files`.split("\n") - gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") gem.require_paths = ['lib'] gem.bindir = 'bin' gem.executables << 'crowdin-console' @@ -25,9 +24,9 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'bundler', '~> 2.2', '>= 2.2.32' gem.add_development_dependency 'codecov', '~> 0.6.0' + gem.add_development_dependency 'pry', '~> 0.14.1' gem.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6' gem.add_development_dependency 'rspec', '~> 3.10' gem.add_development_dependency 'rubocop', '~> 1.23' - gem.add_development_dependency 'sinatra', '~> 2.1' gem.add_development_dependency 'webmock', '~> 3.14' end diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index 7cd665a..fe4ac1d 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -1,5 +1,16 @@ # frozen_string_literal: true +module Crowdin + # 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 +end + # Libs require 'json' require 'open-uri' @@ -9,32 +20,14 @@ require 'crowdin-api/core/errors' require 'crowdin-api/core/errors_raisers' require 'crowdin-api/core/request' -require 'crowdin-api/core/utils' +require 'crowdin-api/core/send_request' # 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/translations' -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' +Crowdin::API_RESOURCES_MODULES.each do |api_resource| + require "crowdin-api/api_resources/#{api_resource.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase}" +rescue LoadError + # Ignored +end # Client modules require 'crowdin-api/client/version' diff --git a/lib/crowdin-api/api-resources/languages.rb b/lib/crowdin-api/api-resources/languages.rb deleted file mode 100644 index 243be48..0000000 --- a/lib/crowdin-api/api-resources/languages.rb +++ /dev/null @@ -1,82 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module ApiResources - module Languages - def list_languages(query = {}) - request = Web::Request.new( - self, - :get, - '/languages', - query - ) - - request.perform - end - - # - # Add custom language. - # - # === Parameters - # - # * +:name+ [String] - Language name - # * +:code+ [String] - Custom language code - # * +:localeCode+ [String] - Custom language locale code - # * +:textDirection+ [String ltr/rtl] - Text direction in custom language - # * +:pluralCategoryNames+ [String] - Array with category names - # * +:threeLettersCode+ [String] - Custom language 3 letters code. Format: ISO 6393 code - # - # === Example - # - # crowdin.add_custom_language(name: 'your_name', code: 'your_code' ..) - # - def add_custom_language(query = {}) - request = Web::Request.new( - self, - :post, - '/languages', - query - ) - - request.perform - end - - def get_language(language_id = nil) - language_id || raise_parameter_is_required_error(:language_id) - - request = Web::Request.new( - self, - :get, - "/languages/#{language_id}" - ) - - request.perform - end - - def delete_custom_language(language_id = nil) - language_id || raise_parameter_is_required_error(:language_id) - - request = Web::Request.new( - self, - :delete, - "/languages/#{language_id}" - ) - - request.perform - end - - def edit_custom_language(language_id = nil) - language_id || raise_parameter_is_required_error(:language_id) - - request = Web::Request.new( - self, - :patch, - "/languages/#{language_id}", - query - ) - - request.perform - 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 deleted file mode 100644 index a3608e4..0000000 --- a/lib/crowdin-api/api-resources/machine_translation_engines.rb +++ /dev/null @@ -1,74 +0,0 @@ -# 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 deleted file mode 100644 index aefe84a..0000000 --- a/lib/crowdin-api/api-resources/projects.rb +++ /dev/null @@ -1,148 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module ApiResources - module Projects - def list_projects(query = {}) - request = Web::Request.new( - self, - :get, - '/projects', - query - ) - - request.perform - end - - def add_project(query = {}) - request = Web::Request.new( - self, - :post, - '/projects', - query - ) - - request.perform - end - - def get_project(project_id = nil) - project_id || raise_parameter_is_required_error(:project_id) - - request = Web::Request.new( - self, - :get, - "/projects/#{project_id}" - ) - - request.perform - end - - def delete_project(project_id = nil) - project_id || raise_parameter_is_required_error(:project_id) - - request = Web::Request.new( - self, - :delete, - "/projects/#{project_id}" - ) - - 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) - - request = Web::Request.new( - self, - :patch, - "/projects/#{project_id}", - query - ) - - request.perform - end - - # -- For Enterprise mode only -- - - def list_groups(query = {}) - config.enterprise_mode? || raise_only_for_enterprise_mode_error - - request = Web::Request.new( - self, - :get, - '/groups', - query - ) - - request.perform - end - - def add_group(query = {}) - config.enterprise_mode? || raise_only_for_enterprise_mode_error - - request = Web::Request.new( - self, - :post, - '/groups', - query - ) - - request.perform - end - - def get_group(group_id = nil) - config.enterprise_mode? || raise_only_for_enterprise_mode_error - group_id || raise_parameter_is_required_error(:group_id) - - request = Web::Request.new( - self, - :get, - "/groups/#{group_id}" - ) - - request.perform - end - - def delete_group(group_id = nil) - config.enterprise_mode? || raise_only_for_enterprise_mode_error - group_id || raise_parameter_is_required_error(:group_id) - - request = Web::Request.new( - self, - :delete, - "/groups/#{group_id}" - ) - - request.perform - end - - def edit_group(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, - :patch, - "/groups/#{group_id}", - query - ) - - request.perform - end - end - end -end diff --git a/lib/crowdin-api/api-resources/reports.rb b/lib/crowdin-api/api-resources/reports.rb deleted file mode 100644 index a28b071..0000000 --- a/lib/crowdin-api/api-resources/reports.rb +++ /dev/null @@ -1,138 +0,0 @@ -# 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/storages.rb b/lib/crowdin-api/api-resources/storages.rb deleted file mode 100644 index 3676890..0000000 --- a/lib/crowdin-api/api-resources/storages.rb +++ /dev/null @@ -1,106 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module ApiResources - module Storages - # - # Get storages list. - # - # === Parameters - # - # Optional: - # * +:limit+ [Integer 1..500] - A maximum number of items to retrieve, default - 25 - # * +:offset+ [Integer >= 0] - A starting offset in the collection, default - 0 - # - # === Example - # - # crowdin.list_projects(limit: your_value) - # - def list_storages(query = {}) - request = Web::Request.new( - self, - :get, - '/storages', - query - ) - - request.perform - end - - # - # Add storage. - # - # === Parameters - # - # * +File+ - File class object or full path to file - # - # === Example - # - # crowdin.add_storage(File.open('your_filename.extension', 'r')) - # or - # crowdin.add_storage('your_filename.extension') - # - def add_storage(file = nil) - file || raise_parameter_is_required_error(:file) - - file = file.is_a?(File) ? file : File.open(file, 'r') - - request = Web::Request.new( - self, - :post, - '/storages', - file, - { 'Content-Type' => 'application/octet-stream', 'Crowdin-API-FileName' => File.basename(file) } - ) - - request.perform - end - - # - # Get storage information. - # - # === Parameters - # - # * +:storage_id+ [Integer] - Storage Identifier. Get via list_storages - # - # === Example - # - # crowdin.get_storage(your_storage_id) - # - def get_storage(storage_id = nil) - storage_id || raise_parameter_is_required_error(:storage_id) - - request = Web::Request.new( - self, - :get, - "/storages/#{storage_id}" - ) - - request.perform - end - - # - # Delete storage. - # - # === Parameters - # - # * +:storage_id+ [Integer] - Storage Identifier. Get via list_storages - # - # === Example - # - # crowdin.delete_storage(your_storage_id) - # - def delete_storage(storage_id = nil) - storage_id || raise_parameter_is_required_error(:storage_id) - - request = Web::Request.new( - self, - :delete, - "/storages/#{storage_id}" - ) - - request.perform - end - end - end -end diff --git a/lib/crowdin-api/api-resources/teams.rb b/lib/crowdin-api/api-resources/teams.rb deleted file mode 100644 index a4567db..0000000 --- a/lib/crowdin-api/api-resources/teams.rb +++ /dev/null @@ -1,144 +0,0 @@ -# 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/users.rb b/lib/crowdin-api/api-resources/users.rb deleted file mode 100644 index 8a09a8b..0000000 --- a/lib/crowdin-api/api-resources/users.rb +++ /dev/null @@ -1,129 +0,0 @@ -# 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 deleted file mode 100644 index c8c2a8a..0000000 --- a/lib/crowdin-api/api-resources/vendors.rb +++ /dev/null @@ -1,21 +0,0 @@ -# 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/workflows.rb b/lib/crowdin-api/api-resources/workflows.rb deleted file mode 100644 index 341ec06..0000000 --- a/lib/crowdin-api/api-resources/workflows.rb +++ /dev/null @@ -1,62 +0,0 @@ -# frozen_string_literal: true - -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 - project_id || raise_project_id_is_required_error - - request = Web::Request.new( - self, - :get, - "/projects/#{project_id}/workflow-steps", - query - ) - - request.perform - end - - def get_workflow_step(step_id = nil, project_id = config.project_id) - config.enterprise_mode? || raise_only_for_enterprise_mode_error - step_id || raise_parameter_is_required_error(:step_id) - project_id || raise_project_id_is_required_error - - request = Web::Request.new( - self, - :get, - "/projects/#{project_id}/workflow-steps/#{step_id}" - ) - - request.perform - end - - def list_workflow_templates(query = {}) - config.enterprise_mode? || raise_only_for_enterprise_mode_error - - request = Web::Request.new( - self, - :get, - '/workflow-templates', - query - ) - - request.perform - end - - def get_workflow_template(template_id = nil) - config.enterprise_mode? || raise_only_for_enterprise_mode_error - template_id || raise_parameter_is_required_error(:template_id) - - request = Web::Request.new( - self, - :get, - "/workflow-templates/#{template_id}" - ) - - request.perform - end - end - end -end diff --git a/lib/crowdin-api/api-resources/dictionaries.rb b/lib/crowdin-api/api_resources/dictionaries.rb similarity index 63% rename from lib/crowdin-api/api-resources/dictionaries.rb rename to lib/crowdin-api/api_resources/dictionaries.rb index 85edb57..547d22b 100644 --- a/lib/crowdin-api/api-resources/dictionaries.rb +++ b/lib/crowdin-api/api_resources/dictionaries.rb @@ -7,13 +7,12 @@ def list_dictionaries(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/dictionaries", - query + "#{config.target_api_url}/projects/#{project_id}/dictionaries", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def edit_dictionary(language_id = nil, query = {}, project_id = config.project_id) @@ -21,13 +20,12 @@ def edit_dictionary(language_id = nil, query = {}, project_id = config.project_i project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/dictionaries/#{language_id}", - query + "#{config.target_api_url}/projects/#{project_id}/dictionaries/#{language_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/distributions.rb b/lib/crowdin-api/api_resources/distributions.rb similarity index 60% rename from lib/crowdin-api/api-resources/distributions.rb rename to lib/crowdin-api/api_resources/distributions.rb index cdf67ee..26120f6 100644 --- a/lib/crowdin-api/api-resources/distributions.rb +++ b/lib/crowdin-api/api_resources/distributions.rb @@ -7,26 +7,24 @@ def list_distributions(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/distributions", - query + "#{config.target_api_url}/projects/#{project_id}/distributions", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/distributions", - query + "#{config.target_api_url}/projects/#{project_id}/distributions", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_distribution(hash = nil, project_id = config.project_id) @@ -34,12 +32,11 @@ def get_distribution(hash = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/distributions/#{hash}" + "#{config.target_api_url}/projects/#{project_id}/distributions/#{hash}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_distribution(hash = nil, project_id = config.project_id) @@ -47,12 +44,11 @@ def delete_distribution(hash = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/distributions/#{hash}" + "#{config.target_api_url}/projects/#{project_id}/distributions/#{hash}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_distribution(hash = nil, query = {}, project_id = config.project_id) @@ -60,26 +56,24 @@ def edit_distribution(hash = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/distributions/#{hash}", - query + "#{config.target_api_url}/projects/#{project_id}/distributions/#{hash}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end - def get_destribution_release(hash = nil, project_id = config.project_id) + def get_distribution_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, + connection, :get, - "/projects/#{project_id}/distributions/#{hash}/release" + "#{config.target_api_url}/projects/#{project_id}/distributions/#{hash}/release" ) - - request.perform + Web::SendRequest.new(request).perform end def release_distribution(hash = nil, project_id = config.project_id) @@ -87,12 +81,11 @@ def release_distribution(hash = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/distributions/#{hash}/release" + "#{config.target_api_url}/projects/#{project_id}/distributions/#{hash}/release" ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/glossaries.rb b/lib/crowdin-api/api_resources/glossaries.rb similarity index 55% rename from lib/crowdin-api/api-resources/glossaries.rb rename to lib/crowdin-api/api_resources/glossaries.rb index 610b2cf..8803ccd 100644 --- a/lib/crowdin-api/api-resources/glossaries.rb +++ b/lib/crowdin-api/api_resources/glossaries.rb @@ -5,78 +5,69 @@ module ApiResources module Glossaries def list_glossaries(query = {}) request = Web::Request.new( - self, + connection, :get, - '/glossaries', - query + "#{config.target_api_url}/glossaries", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def add_glossary(query = {}) request = Web::Request.new( - self, + connection, :post, - '/glossaries', - query + "#{config.target_api_url}/glossaries", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_glossary(glossary_id = nil, query = {}) glossary_id || raise_parameter_is_required_error(:glossary_id) request = Web::Request.new( - self, + connection, :get, - "/glossaries/#{glossary_id}", - query + "#{config.target_api_url}/glossaries/#{glossary_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end - def delete_glossary(glossary_id = nil, query = {}) + def delete_glossary(glossary_id = nil) glossary_id || raise_parameter_is_required_error(:glossary_id) request = Web::Request.new( - self, + connection, :delete, - "/glossaries/#{glossary_id}", - query + "#{config.target_api_url}/glossaries/#{glossary_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_glossary(glossary_id = nil, query = {}) glossary_id || raise_parameter_is_required_error(:glossary_id) request = Web::Request.new( - self, + connection, :patch, - "/glossaries/#{glossary_id}", - query + "#{config.target_api_url}/glossaries/#{glossary_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/glossaries/#{glossary_id}/exports", - query, - {}, - destination + "#{config.target_api_url}/glossaries/#{glossary_id}/exports", + { params: query } ) - - request.perform + Web::SendRequest.new(request, destination).perform end def check_glossary_export_status(glossary_id = nil, export_id = nil) @@ -84,12 +75,11 @@ def check_glossary_export_status(glossary_id = nil, export_id = nil) export_id || raise_parameter_is_required_error(:export_id) request = Web::Request.new( - self, + connection, :get, - "/glossaries/#{glossary_id}/exports/#{export_id}" + "#{config.target_api_url}/glossaries/#{glossary_id}/exports/#{export_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def download_glossary(glossary_id = nil, export_id = nil, destination = nil) @@ -97,28 +87,23 @@ def download_glossary(glossary_id = nil, export_id = nil, destination = nil) export_id || raise_parameter_is_required_error(:export_id) request = Web::Request.new( - self, + connection, :get, - "/glossaries/#{glossary_id}/exports/#{export_id}/download", - {}, - {}, - destination + "#{config.target_api_url}/glossaries/#{glossary_id}/exports/#{export_id}/download" ) - - request.perform + Web::SendRequest.new(request, destination).perform end def import_glossary(glossary_id = nil, query = {}) glossary_id || raise_parameter_is_required_error(:glossary_id) request = Web::Request.new( - self, + connection, :post, - "/glossaries/#{glossary_id}/imports", - query + "#{config.target_api_url}/glossaries/#{glossary_id}/imports", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def check_glossary_import_status(glossary_id = nil, import_id = nil) @@ -126,51 +111,51 @@ def check_glossary_import_status(glossary_id = nil, import_id = nil) import_id || raise_parameter_is_required_error(:import_id) request = Web::Request.new( - self, + connection, :get, - "/glossaries/#{glossary_id}/imports/#{import_id}" + "#{config.target_api_url}/glossaries/#{glossary_id}/imports/#{import_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def list_terms(glossary_id = nil, query = {}) glossary_id || raise_parameter_is_required_error(:glossary_id) request = Web::Request.new( - self, + connection, :get, - "/glossaries/#{glossary_id}/terms", - query + "#{config.target_api_url}/glossaries/#{glossary_id}/terms", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def add_term(glossary_id = nil, query = {}) glossary_id || raise_parameter_is_required_error(:glossary_id) request = Web::Request.new( - self, + connection, :post, - "/glossaries/#{glossary_id}/terms", - query + "#{config.target_api_url}/glossaries/#{glossary_id}/terms", + { params: query } ) - - request.perform + Web::SendRequest.new(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 + response = ::RestClient::Request.execute( + { + method: :delete, + url: config.base_url + config.target_api_url + "/glossaries/#{glossary_id}/terms", + payload: query.to_json + }.merge(@options) ) - request.perform + response.body.empty? ? response.code : JSON.parse(response.body) + rescue StandardError => e + e.message end def get_term(glossary_id = nil, term_id = nil) @@ -178,12 +163,11 @@ def get_term(glossary_id = nil, term_id = nil) term_id || raise_parameter_is_required_error(:term_id) request = Web::Request.new( - self, + connection, :get, - "/glossaries/#{glossary_id}/terms/#{term_id}" + "#{config.target_api_url}/glossaries/#{glossary_id}/terms/#{term_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_term(glossary_id = nil, term_id = nil) @@ -191,12 +175,11 @@ def delete_term(glossary_id = nil, term_id = nil) term_id || raise_parameter_is_required_error(:term_id) request = Web::Request.new( - self, + connection, :delete, - "/glossaries/#{glossary_id}/terms/#{term_id}" + "#{config.target_api_url}/glossaries/#{glossary_id}/terms/#{term_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_term(glossary_id = nil, term_id = nil, query = {}) @@ -204,13 +187,12 @@ def edit_term(glossary_id = nil, term_id = nil, query = {}) term_id || raise_parameter_is_required_error(:term_id) request = Web::Request.new( - self, + connection, :patch, - "/glossaries/#{glossary_id}/terms/#{term_id}", - query + "#{config.target_api_url}/glossaries/#{glossary_id}/terms/#{term_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/labels.rb b/lib/crowdin-api/api_resources/labels.rb similarity index 59% rename from lib/crowdin-api/api-resources/labels.rb rename to lib/crowdin-api/api_resources/labels.rb index 4e1095d..04439f0 100644 --- a/lib/crowdin-api/api-resources/labels.rb +++ b/lib/crowdin-api/api_resources/labels.rb @@ -3,30 +3,28 @@ module Crowdin module ApiResources module Labels - def list_lables(query = {}, project_id = config.project_id) + def list_labels(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/labels", - query + "#{config.target_api_url}/projects/#{project_id}/labels", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/labels", - query + "#{config.target_api_url}/projects/#{project_id}/labels", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_label(label_id = nil, project_id = config.project_id) @@ -34,12 +32,11 @@ def get_label(label_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/labels/#{label_id}" + "#{config.target_api_url}/projects/#{project_id}/labels/#{label_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_label(label_id = nil, project_id = config.project_id) @@ -47,12 +44,11 @@ def delete_label(label_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/labels/#{label_id}" + "#{config.target_api_url}/projects/#{project_id}/labels/#{label_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_label(label_id = nil, query = {}, project_id = config.project_id) @@ -60,13 +56,12 @@ def edit_label(label_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/labels/#{label_id}", - query + "#{config.target_api_url}/projects/#{project_id}/labels/#{label_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def assign_label_to_strings(label_id = nil, query = {}, project_id = config.project_id) @@ -74,29 +69,17 @@ def assign_label_to_strings(label_id = nil, query = {}, project_id = config.proj project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/labels/#{label_id}/strings", - query + "#{config.target_api_url}/projects/#{project_id}/labels/#{label_id}/strings", + { params: query } ) - - request.perform + Web::SendRequest.new(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 + label_id || raise_parameter_is_required_error(:label_id) + project_id || raise_project_id_is_required_error response = ::RestClient::Request.execute( { @@ -106,11 +89,9 @@ def unassign_label_from_strings(label_id = nil, query = {}, project_id = config. }.merge(@options) ) - begin - JSON.parse(response) - rescue - {} - end + response.body.empty? ? response.code : JSON.parse(response.body) + rescue StandardError => e + e.message end end end diff --git a/lib/crowdin-api/api_resources/languages.rb b/lib/crowdin-api/api_resources/languages.rb new file mode 100644 index 0000000..8461517 --- /dev/null +++ b/lib/crowdin-api/api_resources/languages.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Languages + def list_languages(query = {}) + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/languages", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def add_custom_language(query = {}) + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/languages", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_language(language_id = nil) + language_id || raise_parameter_is_required_error(:language_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/languages/#{language_id}" + ) + Web::SendRequest.new(request).perform + end + + def delete_custom_language(language_id = nil) + language_id || raise_parameter_is_required_error(:language_id) + + request = Web::Request.new( + connection, + :delete, + "#{config.target_api_url}/languages/#{language_id}" + ) + Web::SendRequest.new(request).perform + end + + def edit_custom_language(language_id = nil, query = {}) + language_id || raise_parameter_is_required_error(:language_id) + + request = Web::Request.new( + connection, + :patch, + "#{config.target_api_url}/languages/#{language_id}", + { params: query } + ) + Web::SendRequest.new(request).perform + 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..264ec27 --- /dev/null +++ b/lib/crowdin-api/api_resources/machine_translation_engines.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module MachineTranslationEngines + def list_mts(query = {}) + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/mts", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_mt(mt_id = nil) + mt_id || raise_parameter_is_required_error(:mt_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/mts/#{mt_id}" + ) + Web::SendRequest.new(request).perform + end + + def translate_via_mt(mt_id = nil, query = {}) + mt_id || raise_parameter_is_required_error(:mt_id) + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/mts/#{mt_id}/translations", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + # -- For Enterprise mode only -- + + def edit_mt(mt_id = nil, query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + mt_id || raise_parameter_is_required_error(:mt_id) + + request = Web::Request.new( + connection, + :patch, + "#{config.target_api_url}/mts/#{mt_id}", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def add_mt(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/mts", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def delete_mt(mt_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + mt_id || raise_parameter_is_required_error(:mt_id) + + request = Web::Request.new( + connection, + :delete, + "#{config.target_api_url}/mts/#{mt_id}" + ) + Web::SendRequest.new(request).perform + end + end + end +end diff --git a/lib/crowdin-api/api_resources/projects.rb b/lib/crowdin-api/api_resources/projects.rb new file mode 100644 index 0000000..0b84194 --- /dev/null +++ b/lib/crowdin-api/api_resources/projects.rb @@ -0,0 +1,124 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Projects + def list_projects(query = {}) + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/projects", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def add_project(query = {}) + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/projects", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_project(project_id = nil) + project_id || raise_parameter_is_required_error(:project_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/projects/#{project_id}" + ) + Web::SendRequest.new(request).perform + end + + def delete_project(project_id = nil) + project_id || raise_parameter_is_required_error(:project_id) + + request = Web::Request.new( + connection, + :delete, + "#{config.target_api_url}/projects/#{project_id}" + ) + Web::SendRequest.new(request).perform + end + + def edit_project(project_id = nil, query = {}) + project_id || raise_parameter_is_required_error(:project_id) + + request = Web::Request.new( + connection, + :patch, + "#{config.target_api_url}/projects/#{project_id}", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + # -- For Enterprise mode only -- + + def list_groups(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/groups", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def add_group(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/groups", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_group(group_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/groups/#{group_id}" + ) + Web::SendRequest.new(request).perform + end + + def delete_group(group_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) + + request = Web::Request.new( + connection, + :delete, + "#{config.target_api_url}/groups/#{group_id}" + ) + Web::SendRequest.new(request).perform + end + + def edit_group(group_id = nil, query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) + + request = Web::Request.new( + connection, + :patch, + "#{config.target_api_url}/groups/#{group_id}", + { params: query } + ) + Web::SendRequest.new(request).perform + end + end + end +end diff --git a/lib/crowdin-api/api_resources/reports.rb b/lib/crowdin-api/api_resources/reports.rb new file mode 100644 index 0000000..91011e0 --- /dev/null +++ b/lib/crowdin-api/api_resources/reports.rb @@ -0,0 +1,120 @@ +# 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( + connection, + :post, + "#{config.target_api_url}/projects/#{project_id}/reports", + { params: query } + ) + Web::SendRequest.new(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( + connection, + :get, + "#{config.target_api_url}/projects/#{project_id}/reports/#{report_id}" + ) + Web::SendRequest.new(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( + connection, + :get, + "#{config.target_api_url}/projects/#{project_id}/reports/#{report_id}/download" + ) + Web::SendRequest.new(request, destination).perform + end + + # -- For Enterprise mode only -- + + def generate_group_report(group_id = nil, query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/groups/#{group_id}/reports", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def check_group_report_generation_status(group_id = nil, report_id = nil) + 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( + connection, + :get, + "#{config.target_api_url}/groups/#{group_id}/reports/#{report_id}" + ) + Web::SendRequest.new(request).perform + end + + def download_group_report(group_id = nil, report_id = nil, destination = nil) + 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( + connection, + :get, + "#{config.target_api_url}/groups/#{group_id}/reports/#{report_id}/download" + ) + Web::SendRequest.new(request, destination).perform + end + + def generate_organization_report(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/reports", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def check_organization_report_generation_status(report_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + report_id || raise_parameter_is_required_error(:report_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/reports/#{report_id}" + ) + Web::SendRequest.new(request).perform + end + + def download_organization_report(report_id = nil, destination = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + report_id || raise_parameter_is_required_error(:report_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/reports/#{report_id}/download" + ) + Web::SendRequest.new(request, destination).perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/screenshots.rb b/lib/crowdin-api/api_resources/screenshots.rb similarity index 63% rename from lib/crowdin-api/api-resources/screenshots.rb rename to lib/crowdin-api/api_resources/screenshots.rb index 524d2ed..da22d11 100644 --- a/lib/crowdin-api/api-resources/screenshots.rb +++ b/lib/crowdin-api/api_resources/screenshots.rb @@ -7,26 +7,24 @@ def list_screenshots(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/screenshots", - query + "#{config.target_api_url}/projects/#{project_id}/screenshots", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/screenshots", - query + "#{config.target_api_url}/projects/#{project_id}/screenshots", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_screenshot(screenshot_id = nil, project_id = config.project_id) @@ -34,12 +32,11 @@ def get_screenshot(screenshot_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/screenshots/#{screenshot_id}" + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def update_screenshot(screenshot_id = nil, query = {}, project_id = config.project_id) @@ -47,13 +44,12 @@ def update_screenshot(screenshot_id = nil, query = {}, project_id = config.proje project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :put, - "/projects/#{project_id}/screenshots/#{screenshot_id}", - query + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def delete_screenshot(screenshot_id = nil, project_id = config.project_id) @@ -61,12 +57,11 @@ def delete_screenshot(screenshot_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/screenshots/#{screenshot_id}" + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_screenshot(screenshot_id = nil, query = {}, project_id = config.project_id) @@ -74,13 +69,12 @@ def edit_screenshot(screenshot_id = nil, query = {}, project_id = config.project project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/screenshots/#{screenshot_id}", - query + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def list_tags(screenshot_id = nil, query = {}, project_id = config.project_id) @@ -88,28 +82,25 @@ def list_tags(screenshot_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/screenshots/#{screenshot_id}/tags", - query + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :put, - "/projects/#{project_id}/screenshots/#{screenshot_id}/tags", - query + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def add_tag(screenshot_id = nil, query = {}, project_id = config.project_id) @@ -117,13 +108,12 @@ def add_tag(screenshot_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/screenshots/#{screenshot_id}/tags", - query + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def clear_tags(screenshot_id = nil, project_id = config.project_id) @@ -131,12 +121,11 @@ def clear_tags(screenshot_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/screenshots/#{screenshot_id}/tags" + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags" ) - - request.perform + Web::SendRequest.new(request).perform end def get_tag(screenshot_id = nil, tag_id = nil, project_id = config.project_id) @@ -145,12 +134,11 @@ def get_tag(screenshot_id = nil, tag_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}" + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_tag(screenshot_id = nil, tag_id = nil, project_id = config.project_id) @@ -159,12 +147,11 @@ def delete_tag(screenshot_id = nil, tag_id = nil, project_id = config.project_id project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}" + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_tag(screenshot_id = nil, tag_id = nil, query = {}, project_id = config.project_id) @@ -173,13 +160,12 @@ def edit_tag(screenshot_id = nil, tag_id = nil, query = {}, project_id = config. project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}", - query + "#{config.target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/source_files.rb b/lib/crowdin-api/api_resources/source_files.rb similarity index 58% rename from lib/crowdin-api/api-resources/source_files.rb rename to lib/crowdin-api/api_resources/source_files.rb index 42d39d8..b36015e 100644 --- a/lib/crowdin-api/api-resources/source_files.rb +++ b/lib/crowdin-api/api_resources/source_files.rb @@ -7,26 +7,24 @@ def list_branches(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/branches", - query + "#{config.target_api_url}/projects/#{project_id}/branches", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def add_branch(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/branches", - query + "#{config.target_api_url}/projects/#{project_id}/branches", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_branch(branch_id = nil, project_id = config.project_id) @@ -34,12 +32,11 @@ def get_branch(branch_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/branches/#{branch_id}" + "#{config.target_api_url}/projects/#{project_id}/branches/#{branch_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_branch(branch_id = nil, project_id = config.project_id) @@ -47,12 +44,11 @@ def delete_branch(branch_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/branches/#{branch_id}" + "#{config.target_api_url}/projects/#{project_id}/branches/#{branch_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_branch(branch_id = nil, query = {}, project_id = config.project_id) @@ -60,39 +56,36 @@ def edit_branch(branch_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/branches/#{branch_id}", - query + "#{config.target_api_url}/projects/#{project_id}/branches/#{branch_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def list_directories(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/directories", - query + "#{config.target_api_url}/projects/#{project_id}/directories", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def add_directory(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/directories", - query + "#{config.target_api_url}/projects/#{project_id}/directories", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_directory(directory_id = nil, project_id = config.project_id) @@ -100,12 +93,11 @@ def get_directory(directory_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/directories/#{directory_id}" + "#{config.target_api_url}/projects/#{project_id}/directories/#{directory_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_directory(directory_id = nil, project_id = config.project_id) @@ -113,12 +105,11 @@ def delete_directory(directory_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/directories/#{directory_id}" + "#{config.target_api_url}/projects/#{project_id}/directories/#{directory_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_directory(directory_id = nil, query = {}, project_id = config.project_id) @@ -126,58 +117,36 @@ def edit_directory(directory_id = nil, query = {}, project_id = config.project_i project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/directories/#{directory_id}", - query + "#{config.target_api_url}/projects/#{project_id}/directories/#{directory_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def list_files(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/files", - query + "#{config.target_api_url}/projects/#{project_id}/files", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end - # - # Add custom language. - # - # === Parameters - # - # * +:projectId+ [Integer] - Project Identifier. Get via list_projects. Can be initialized with the Crowdin Client - # * +:storageId+ [Integer] - Storage Identifier. Get via list_storages - # * +:name+ [String] - File name. Note: Can't contain \ / : * ? " < > | symbols. ZIP files are not allowed. - # - # === Example - # - # when you're initialized Crowdin Client with a project_id - # - # crowdin.add_file(storage_id: your_storage_id, name: 'your_filename') - # - # or you can specify project_id - # - # crowdin.add_file({ storage_id: your_storage_id, name: 'your_filename' }, your_project_id) - # def add_file(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/files", - query + "#{config.target_api_url}/projects/#{project_id}/files", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_file(file_id = nil, project_id = config.project_id) @@ -185,12 +154,11 @@ def get_file(file_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/files/#{file_id}" + "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def update_or_restore_file(file_id = nil, query = {}, project_id = config.project_id) @@ -198,13 +166,12 @@ def update_or_restore_file(file_id = nil, query = {}, project_id = config.projec project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :put, - "/projects/#{project_id}/files/#{file_id}", - query + "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def delete_file(file_id = nil, project_id = config.project_id) @@ -212,12 +179,11 @@ def delete_file(file_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/files/#{file_id}" + "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_file(file_id = nil, query = {}, project_id = config.project_id) @@ -225,13 +191,12 @@ def edit_file(file_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/files/#{file_id}", - query + "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def download_file(file_id = nil, destination = nil, project_id = config.project_id) @@ -239,51 +204,24 @@ def download_file(file_id = nil, destination = nil, project_id = config.project_ project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/files/#{file_id}/download", - {}, - {}, - destination + "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}/download" ) - - request.perform + Web::SendRequest.new(request, destination).perform end - # - # List file revisions. - # - # === Parameters - # - # * +:projectId+ [Integer] - Project Identifier. Get via list_projects. Can be initialized with the Crowdin Client - # * +:fileId+ [Integer] - File Identifier. Get via list_files - # - # Optional: - # * +:limit+ [Integer 1..500] - A maximum number of items to retrieve, default - 25 - # * +:offset+ [Integer >= 0] - A starting offset in the collection, default - 0 - # - # === Example - # - # when you're initialized Crowdin Client with a project_id - # - # crowdin.list_file_revisions(your_file_id, limit: your_value) - # - # or you can specify project_id - # - # crowdin.list_file_revisions(your_file_id, { limit: your_value }, your_project_id) - # def list_file_revisions(file_id = nil, query = {}, project_id = config.project_id) file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/files/#{file_id}/revisions", - query + "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}/revisions", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_file_revision(file_id = nil, revision_id = nil, project_id = config.project_id) @@ -292,12 +230,11 @@ def get_file_revision(file_id = nil, revision_id = nil, project_id = config.proj project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/files/#{file_id}/revisions/#{revision_id}" + "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}/revisions/#{revision_id}" ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/source_strings.rb b/lib/crowdin-api/api_resources/source_strings.rb similarity index 61% rename from lib/crowdin-api/api-resources/source_strings.rb rename to lib/crowdin-api/api_resources/source_strings.rb index ce3f100..736132d 100644 --- a/lib/crowdin-api/api-resources/source_strings.rb +++ b/lib/crowdin-api/api_resources/source_strings.rb @@ -7,26 +7,24 @@ def list_strings(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/strings", - query + "#{config.target_api_url}/projects/#{project_id}/strings", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def add_string(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/strings", - query + "#{config.target_api_url}/projects/#{project_id}/strings", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_string(string_id = nil, query = {}, project_id = config.project_id) @@ -34,13 +32,12 @@ def get_string(string_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/strings/#{string_id}", - query + "#{config.target_api_url}/projects/#{project_id}/strings/#{string_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def delete_string(string_id = nil, project_id = config.project_id) @@ -48,12 +45,11 @@ def delete_string(string_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/strings/#{string_id}" + "#{config.target_api_url}/projects/#{project_id}/strings/#{string_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_string(string_id = nil, query = {}, project_id = config.project_id) @@ -61,13 +57,12 @@ def edit_string(string_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/strings/#{string_id}", - query + "#{config.target_api_url}/projects/#{project_id}/strings/#{string_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api_resources/storages.rb b/lib/crowdin-api/api_resources/storages.rb new file mode 100644 index 0000000..2071e2d --- /dev/null +++ b/lib/crowdin-api/api_resources/storages.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Storages + def list_storages(query = {}) + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/storages", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def add_storage(file = nil) + file || raise_parameter_is_required_error(:file) + + file = file.is_a?(File) ? file : File.open(file, 'r') + headers = { 'Content-Type' => 'application/octet-stream', 'Crowdin-API-FileName' => File.basename(file) } + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/storages", + { params: file, headers: headers } + ) + Web::SendRequest.new(request).perform + end + + def get_storage(storage_id = nil) + storage_id || raise_parameter_is_required_error(:storage_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/storages/#{storage_id}" + ) + Web::SendRequest.new(request).perform + end + + def delete_storage(storage_id = nil) + storage_id || raise_parameter_is_required_error(:storage_id) + + request = Web::Request.new( + connection, + :delete, + "#{config.target_api_url}/storages/#{storage_id}" + ) + Web::SendRequest.new(request).perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/string_comments.rb b/lib/crowdin-api/api_resources/string_comments.rb similarity index 63% rename from lib/crowdin-api/api-resources/string_comments.rb rename to lib/crowdin-api/api_resources/string_comments.rb index ee18860..ff7dff6 100644 --- a/lib/crowdin-api/api-resources/string_comments.rb +++ b/lib/crowdin-api/api_resources/string_comments.rb @@ -7,26 +7,24 @@ def list_string_comments(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/comments", - query + "#{config.target_api_url}/projects/#{project_id}/comments", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/comments", - query + "#{config.target_api_url}/projects/#{project_id}/comments", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_string_comment(string_comment_id = nil, project_id = config.project_id) @@ -34,12 +32,11 @@ def get_string_comment(string_comment_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/comments/#{string_comment_id}" + "#{config.target_api_url}/projects/#{project_id}/comments/#{string_comment_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_string_comment(string_comment_id = nil, project_id = config.project_id) @@ -47,12 +44,11 @@ def delete_string_comment(string_comment_id = nil, project_id = config.project_i project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/comments/#{string_comment_id}" + "#{config.target_api_url}/projects/#{project_id}/comments/#{string_comment_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_string_comment(string_comment_id = nil, query = {}, project_id = config.project_id) @@ -60,13 +56,12 @@ def edit_string_comment(string_comment_id = nil, query = {}, project_id = config project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/comments/#{string_comment_id}", - query + "#{config.target_api_url}/projects/#{project_id}/comments/#{string_comment_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/string_translations.rb b/lib/crowdin-api/api_resources/string_translations.rb similarity index 51% rename from lib/crowdin-api/api-resources/string_translations.rb rename to lib/crowdin-api/api_resources/string_translations.rb index ed65876..3304c32 100644 --- a/lib/crowdin-api/api-resources/string_translations.rb +++ b/lib/crowdin-api/api_resources/string_translations.rb @@ -7,26 +7,24 @@ def list_translation_approvals(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/approvals", - query + "#{config.target_api_url}/projects/#{project_id}/approvals", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/approvals", - query + "#{config.target_api_url}/projects/#{project_id}/approvals", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_approval(approval_id = nil, project_id = config.project_id) @@ -34,12 +32,11 @@ def get_approval(approval_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/approvals/#{approval_id}" + "#{config.target_api_url}/projects/#{project_id}/approvals/#{approval_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def remove_approval(approval_id = nil, project_id = config.project_id) @@ -47,12 +44,11 @@ def remove_approval(approval_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/approvals/#{approval_id}" + "#{config.target_api_url}/projects/#{project_id}/approvals/#{approval_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def list_language_translations(language_id = nil, query = {}, project_id = config.project_id) @@ -60,54 +56,40 @@ def list_language_translations(language_id = nil, query = {}, project_id = confi project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/languages/#{language_id}/translations", - query + "#{config.target_api_url}/projects/#{project_id}/languages/#{language_id}/translations", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :get, - "/projects/#{project_id}/translations", - query + "#{config.target_api_url}/projects/#{project_id}/translations", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/translations", - query + "#{config.target_api_url}/projects/#{project_id}/translations", + { params: query } ) - - request.perform + Web::SendRequest.new(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 + project_id || raise_project_id_is_required_error response = ::RestClient::Request.execute( { @@ -117,77 +99,70 @@ def delete_string_translations(query = {}, project_id = config.project_id) }.merge(@options) ) - begin - JSON.parse(response) - rescue - {} - end + response.body.empty? ? response.code : JSON.parse(response.body) + rescue StandardError => e + e.message 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 + def get_translation(translation_id = nil, query = {}, project_id = config.project_id) + translation_id || raise_parameter_is_required_error(:translation_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/translations/#{trnslation_id}", - query + "#{config.target_api_url}/projects/#{project_id}/translations/#{translation_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(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 + def restore_translation(translation_id = nil, project_id = config.project_id) + translation_id || raise_parameter_is_required_error(:translation_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :put, - "/projects/#{project_id}/translations/#{trnslation_id}" + "#{config.target_api_url}/projects/#{project_id}/translations/#{translation_id}" ) - - request.perform + Web::SendRequest.new(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 + def delete_translation(translation_id = nil, project_id = config.project_id) + translation_id || raise_parameter_is_required_error(:translation_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/translations/#{trnslation_id}" + "#{config.target_api_url}/projects/#{project_id}/translations/#{translation_id}" ) - - request.perform + Web::SendRequest.new(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, + connection, :get, - "/projects/#{project_id}/votes", - query + "#{config.target_api_url}/projects/#{project_id}/votes", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/votes", - query + "#{config.target_api_url}/projects/#{project_id}/votes", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_vote(vote_id = nil, project_id = config.project_id) @@ -195,12 +170,11 @@ def get_vote(vote_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/votes/#{vote_id}" + "#{config.target_api_url}/projects/#{project_id}/votes/#{vote_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_screenshot(vote_id = nil, project_id = config.project_id) @@ -208,12 +182,11 @@ def edit_screenshot(vote_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/votes/#{vote_id}" + "#{config.target_api_url}/projects/#{project_id}/votes/#{vote_id}" ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/tasks.rb b/lib/crowdin-api/api_resources/tasks.rb similarity index 59% rename from lib/crowdin-api/api-resources/tasks.rb rename to lib/crowdin-api/api_resources/tasks.rb index fdda7d6..0e9c37d 100644 --- a/lib/crowdin-api/api-resources/tasks.rb +++ b/lib/crowdin-api/api_resources/tasks.rb @@ -7,26 +7,24 @@ def list_tasks(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/tasks", - query + "#{config.target_api_url}/projects/#{project_id}/tasks", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/tasks", - query + "#{config.target_api_url}/projects/#{project_id}/tasks", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def export_task_strings(task_id = nil, destination = nil, project_id = config.project_id) @@ -34,15 +32,11 @@ def export_task_strings(task_id = nil, destination = nil, project_id = config.pr project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/tasks/#{task_id}/exports", - {}, - {}, - destination + "#{config.target_api_url}/projects/#{project_id}/tasks/#{task_id}/exports" ) - - request.perform + Web::SendRequest.new(request, destination).perform end def get_task(task_id = nil, project_id = config.project_id) @@ -50,12 +44,11 @@ def get_task(task_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/tasks/#{task_id}" + "#{config.target_api_url}/projects/#{project_id}/tasks/#{task_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_task(task_id = nil, project_id = config.project_id) @@ -63,12 +56,11 @@ def delete_task(task_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/tasks/#{task_id}" + "#{config.target_api_url}/projects/#{project_id}/tasks/#{task_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def edit_task(task_id = nil, query = {}, project_id = config.project_id) @@ -76,37 +68,34 @@ def edit_task(task_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :patch, - "/projects/#{project_id}/tasks/#{task_id}", - query + "#{config.target_api_url}/projects/#{project_id}/tasks/#{task_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def list_user_tasks(query = {}) request = Web::Request.new( - self, + connection, :get, - '/user/tasks', - query + "#{config.target_api_url}/user/tasks", + { params: query } ) - - request.perform + Web::SendRequest.new(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 + connection, + :patch, + "#{config.target_api_url}/user/tasks/#{task_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform 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..06b7c73 --- /dev/null +++ b/lib/crowdin-api/api_resources/teams.rb @@ -0,0 +1,135 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Teams + # -- For Enterprise mode only -- + + def add_team_to_project(query = {}, project_id = config.project_id) + enterprise_mode? || raise_only_for_enterprise_mode_error + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/projects/#{project_id}/teams", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def list_teams(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/teams", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def add_team(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/teams", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_team(team_id = nil, query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/teams/#{team_id}", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def delete_team(team_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + connection, + :delete, + "#{config.target_api_url}/teams/#{team_id}" + ) + Web::SendRequest.new(request).perform + end + + def edit_team(team_id = nil, query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + connection, + :patch, + "#{config.target_api_url}/teams/#{team_id}", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def team_members_list(team_id = nil, query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/teams/#{team_id}/members", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def add_team_members(team_id = nil, query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/teams/#{team_id}/members", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def delete_all_team_members(team_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + team_id || raise_parameter_is_required_error(:team_id) + + request = Web::Request.new( + connection, + :delete, + "#{config.target_api_url}/teams/#{team_id}/members" + ) + Web::SendRequest.new(request).perform + end + + def delete_team_member(team_id = nil, member_id = nil) + 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( + connection, + :delete, + "#{config.target_api_url}/teams/#{team_id}/members/#{member_id}" + ) + Web::SendRequest.new(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 similarity index 58% rename from lib/crowdin-api/api-resources/translation_memory.rb rename to lib/crowdin-api/api_resources/translation_memory.rb index ae33513..4284b5a 100644 --- a/lib/crowdin-api/api-resources/translation_memory.rb +++ b/lib/crowdin-api/api_resources/translation_memory.rb @@ -5,85 +5,78 @@ module ApiResources module TranslationMemory def list_tms(query = {}) request = Web::Request.new( - self, + connection, :get, - '/tms', - query + "#{config.target_api_url}/tms", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def add_tm(query = {}) request = Web::Request.new( - self, + connection, :post, - '/tms', - query + "#{config.target_api_url}/tms", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_tm(tm_id = nil) tm_id || raise_parameter_is_required_error(:tm_id) request = Web::Request.new( - self, + connection, :get, - "/tms/#{tm_id}" + "#{config.target_api_url}/tms/#{tm_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_tm(tm_id = nil) tm_id || raise_parameter_is_required_error(:tm_id) request = Web::Request.new( - self, + connection, :delete, - "/tms/#{tm_id}" + "#{config.target_api_url}/tms/#{tm_id}" ) - - request.perform + Web::SendRequest.new(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 + connection, + :patch, + "#{config.target_api_url}/tms/#{tm_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def clear_tm(tm_id = nil) tm_id || raise_parameter_is_required_error(:tm_id) request = Web::Request.new( - self, + connection, :delete, - "/tms/#{tm_id}/segments" + "#{config.target_api_url}/tms/#{tm_id}/segments" ) - - request.perform + Web::SendRequest.new(request).perform end def export_tm(tm_id = nil) tm_id || raise_parameter_is_required_error(:tm_id) request = Web::Request.new( - self, + connection, :post, - "/tms/#{tm_id}/exports" + "#{config.target_api_url}/tms/#{tm_id}/exports" ) - - request.perform + Web::SendRequest.new(request).perform end def check_tm_export_status(tm_id = nil, export_id = nil) @@ -91,12 +84,11 @@ def check_tm_export_status(tm_id = nil, export_id = nil) export_id || raise_parameter_is_required_error(:export_id) request = Web::Request.new( - self, + connection, :get, - "/tms/#{tm_id}/exports/#{export_id}" + "#{config.target_api_url}/tms/#{tm_id}/exports/#{export_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def download_tm(tm_id = nil, export_id = nil, destination = nil) @@ -104,28 +96,23 @@ def download_tm(tm_id = nil, export_id = nil, destination = nil) export_id || raise_parameter_is_required_error(:export_id) request = Web::Request.new( - self, + connection, :get, - "/tms/#{tm_id}/exports/#{export_id}/download", - {}, - {}, - destination + "#{config.target_api_url}/tms/#{tm_id}/exports/#{export_id}/download" ) - - request.perform + Web::SendRequest.new(request, destination).perform end def import_tm(tm_id = nil, query = {}) tm_id || raise_parameter_is_required_error(:tm_id) request = Web::Request.new( - self, + connection, :post, - "/tms/#{tm_id}/imports", - query + "#{config.target_api_url}/tms/#{tm_id}/imports", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def check_tm_import_status(tm_id = nil, import_id = nil) @@ -133,12 +120,11 @@ def check_tm_import_status(tm_id = nil, import_id = nil) import_id || raise_parameter_is_required_error(:import_id) request = Web::Request.new( - self, + connection, :get, - "/tms/#{tm_id}/imports/#{import_id}" + "#{config.target_api_url}/tms/#{tm_id}/imports/#{import_id}" ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/translation_status.rb b/lib/crowdin-api/api_resources/translation_status.rb similarity index 60% rename from lib/crowdin-api/api-resources/translation_status.rb rename to lib/crowdin-api/api_resources/translation_status.rb index 05264c0..8e3a47c 100644 --- a/lib/crowdin-api/api-resources/translation_status.rb +++ b/lib/crowdin-api/api_resources/translation_status.rb @@ -8,13 +8,12 @@ def get_branch_progress(branch_id = nil, query = {}, project_id = config.project project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/branches/#{branch_id}/languages/progress", - query + "#{config.target_api_url}/projects/#{project_id}/branches/#{branch_id}/languages/progress", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_directory_progress(directory_id = nil, query = {}, project_id = config.project_id) @@ -22,13 +21,12 @@ def get_directory_progress(directory_id = nil, query = {}, project_id = config.p project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/directories/#{directory_id}/languages/progress", - query + "#{config.target_api_url}/projects/#{project_id}/directories/#{directory_id}/languages/progress", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_file_progress(file_id = nil, query = {}, project_id = config.project_id) @@ -36,13 +34,12 @@ def get_file_progress(file_id = nil, query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/files/#{file_id}/languages/progress", - query + "#{config.target_api_url}/projects/#{project_id}/files/#{file_id}/languages/progress", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_language_progress(language_id = nil, query = {}, project_id = config.project_id) @@ -50,39 +47,36 @@ def get_language_progress(language_id = nil, query = {}, project_id = config.pro project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/languages/#{language_id}/progress", - query + "#{config.target_api_url}/projects/#{project_id}/languages/#{language_id}/progress", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_project_progress(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/languages/progress", - query + "#{config.target_api_url}/projects/#{project_id}/languages/progress", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_qa_progress(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/qa-checks", - query + "#{config.target_api_url}/projects/#{project_id}/qa-checks", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api-resources/translations.rb b/lib/crowdin-api/api_resources/translations.rb similarity index 61% rename from lib/crowdin-api/api-resources/translations.rb rename to lib/crowdin-api/api_resources/translations.rb index ab08424..0da678b 100644 --- a/lib/crowdin-api/api-resources/translations.rb +++ b/lib/crowdin-api/api_resources/translations.rb @@ -8,25 +8,23 @@ def pre_translation_status(pre_translation_id = nil, project_id = config.project project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/pre-translations/#{pre_translation_id}" + "#{config.target_api_url}/projects/#{project_id}/pre-translations/#{pre_translation_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def apply_pre_translation(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/pre-translations", - query + "#{config.target_api_url}/projects/#{project_id}/pre-translations", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def build_project_directory_translation(directory_id = nil, query = {}, project_id = config.project_id) @@ -34,13 +32,12 @@ def build_project_directory_translation(directory_id = nil, query = {}, project_ project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/translations/builds/directories/#{directory_id}", - query + "#{config.target_api_url}/projects/#{project_id}/translations/builds/directories/#{directory_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def build_project_file_translation(file_id = nil, query = {}, destination = nil, project_id = config.project_id) @@ -50,41 +47,36 @@ def build_project_file_translation(file_id = nil, query = {}, destination = nil, headers = query[:eTag] ? { 'If-None-Match' => query[:eTag] } : {} request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/translations/builds/files/#{file_id}", - query, - headers, - destination + "#{config.target_api_url}/projects/#{project_id}/translations/builds/files/#{file_id}", + { params: query, headers: headers } ) - - request.perform + Web::SendRequest.new(request, destination).perform end def list_project_builds(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/translations/builds", - query + "#{config.target_api_url}/projects/#{project_id}/translations/builds", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def build_project_translation(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/translations/builds", - query + "#{config.target_api_url}/projects/#{project_id}/translations/builds", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def upload_translations(language_id = nil, query = {}, project_id = config.project_id) @@ -92,13 +84,12 @@ def upload_translations(language_id = nil, query = {}, project_id = config.proje project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/translations/#{language_id}", - query + "#{config.target_api_url}/projects/#{project_id}/translations/#{language_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def download_project_translations(build_id = nil, destination = nil, project_id = config.project_id) @@ -106,15 +97,11 @@ def download_project_translations(build_id = nil, destination = nil, project_id project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/translations/builds/#{build_id}/download", - {}, - {}, - destination + "#{config.target_api_url}/projects/#{project_id}/translations/builds/#{build_id}/download" ) - - request.perform + Web::SendRequest.new(request, destination).perform end def check_project_build_status(build_id = nil, project_id = config.project_id) @@ -122,12 +109,11 @@ def check_project_build_status(build_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/translations/builds/#{build_id}" + "#{config.target_api_url}/projects/#{project_id}/translations/builds/#{build_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def cancel_build(build_id = nil, project_id = config.project_id) @@ -135,27 +121,23 @@ def cancel_build(build_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/translations/builds/#{build_id}" + "#{config.target_api_url}/projects/#{project_id}/translations/builds/#{build_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def export_project_translation(query = {}, destination = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :post, - "/projects/#{project_id}/translations/exports", - query, - {}, - destination + "#{config.target_api_url}/projects/#{project_id}/translations/exports", + { params: query } ) - - request.perform + Web::SendRequest.new(request, destination).perform 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..eed8af0 --- /dev/null +++ b/lib/crowdin-api/api_resources/users.rb @@ -0,0 +1,161 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Users + def get_authenticated_user + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/user" + ) + Web::SendRequest.new(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( + connection, + :get, + "#{config.target_api_url}/projects/#{project_id}/members", + { params: query } + ) + Web::SendRequest.new(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( + connection, + :get, + "#{config.target_api_url}/projects/#{project_id}/members/#{member_id}" + ) + Web::SendRequest.new(request).perform + end + + # -- For Enterprise mode only -- + + def add_project_member(query = {}, project_id = config.project_id) + enterprise_mode? || raise_only_for_enterprise_mode_error + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/projects/#{project_id}/members", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_project_member_permissions(member_id = nil, project_id = config.project_id) + 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( + connection, + :get, + "#{config.target_api_url}/projects/#{project_id}/members/#{member_id}" + ) + Web::SendRequest.new(request).perform + end + + def replace_project_permissions(member_id = nil, query = {}, project_id = config.project_id) + 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( + connection, + :put, + "#{config.target_api_url}/projects/#{project_id}/members/#{member_id}", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def delete_member_from_project(member_id = nil, query = {}, project_id = config.project_id) + 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 + + response = ::RestClient::Request.execute( + { + method: :delete, + url: config.base_url + config.target_api_url + "/projects/#{project_id}/members/#{member_id}", + payload: query.to_json + }.merge(@options) + ) + + response.body.empty? ? response.code : JSON.parse(response.body) + rescue StandardError => e + e.message + end + + def list_users(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/users", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def invite_user(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :post, + "#{config.target_api_url}/users", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_user(user_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + user_id || raise_parameter_is_required_error(:user_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/users/#{user_id}" + ) + Web::SendRequest.new(request).perform + end + + def delete_user(user_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + user_id || raise_parameter_is_required_error(:user_id) + + request = Web::Request.new( + connection, + :delete, + "#{config.target_api_url}/users/#{user_id}" + ) + Web::SendRequest.new(request).perform + end + + def edit_user(user_id = nil, query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + user_id || raise_parameter_is_required_error(:user_id) + + request = Web::Request.new( + connection, + :patch, + "#{config.target_api_url}/users/#{user_id}", + { params: query } + ) + Web::SendRequest.new(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..40673ae --- /dev/null +++ b/lib/crowdin-api/api_resources/vendors.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Vendors + # -- For Enterprise mode only -- + + def list_vendors(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/vendors", + { params: query } + ) + Web::SendRequest.new(request).perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/webhooks.rb b/lib/crowdin-api/api_resources/webhooks.rb similarity index 58% rename from lib/crowdin-api/api-resources/webhooks.rb rename to lib/crowdin-api/api_resources/webhooks.rb index 648a1d2..395c4de 100644 --- a/lib/crowdin-api/api-resources/webhooks.rb +++ b/lib/crowdin-api/api_resources/webhooks.rb @@ -7,26 +7,24 @@ def list_webhooks(query = {}, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/webhooks", - query + "#{config.target_api_url}/projects/#{project_id}/webhooks", + { params: query } ) - - request.perform + Web::SendRequest.new(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, + connection, :post, - "/projects/#{project_id}/webhooks", - query + "#{config.target_api_url}/projects/#{project_id}/webhooks", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end def get_webhook(webhook_id = nil, project_id = config.project_id) @@ -34,12 +32,11 @@ def get_webhook(webhook_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :get, - "/projects/#{project_id}/webhooks/#{webhook_id}" + "#{config.target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}" ) - - request.perform + Web::SendRequest.new(request).perform end def delete_webhook(webhook_id = nil, project_id = config.project_id) @@ -47,26 +44,24 @@ def delete_webhook(webhook_id = nil, project_id = config.project_id) project_id || raise_project_id_is_required_error request = Web::Request.new( - self, + connection, :delete, - "/projects/#{project_id}/webhooks/#{webhook_id}" + "#{config.target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}" ) - - request.perform + Web::SendRequest.new(request).perform end - def edit_screenshot(webhook_id = nil, query = {}, project_id = config.project_id) + def edit_webhook(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, + connection, :patch, - "/projects/#{project_id}/webhooks/#{webhook_id}", - query + "#{config.target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}", + { params: query } ) - - request.perform + Web::SendRequest.new(request).perform end end end diff --git a/lib/crowdin-api/api_resources/workflows.rb b/lib/crowdin-api/api_resources/workflows.rb new file mode 100644 index 0000000..3105c73 --- /dev/null +++ b/lib/crowdin-api/api_resources/workflows.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Workflows + # -- For Enterprise mode only -- + + def list_workflow_steps(query = {}, project_id = config.project_id) + enterprise_mode? || raise_only_for_enterprise_mode_error + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/projects/#{project_id}/workflow-steps", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_workflow_step(step_id = nil, project_id = config.project_id) + enterprise_mode? || raise_only_for_enterprise_mode_error + step_id || raise_parameter_is_required_error(:step_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/projects/#{project_id}/workflow-steps/#{step_id}" + ) + Web::SendRequest.new(request).perform + end + + def list_workflow_templates(query = {}) + enterprise_mode? || raise_only_for_enterprise_mode_error + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/workflow-templates", + { params: query } + ) + Web::SendRequest.new(request).perform + end + + def get_workflow_template(template_id = nil) + enterprise_mode? || raise_only_for_enterprise_mode_error + template_id || raise_parameter_is_required_error(:template_id) + + request = Web::Request.new( + connection, + :get, + "#{config.target_api_url}/workflow-templates/#{template_id}" + ) + Web::SendRequest.new(request).perform + end + end + end +end diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 16962ca..6fbb1f0 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -20,76 +20,85 @@ module Crowdin # crowdin.list_projects # class Client - 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 + # Processing 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}")) + Client.send(:include, Object.const_get("Crowdin::ApiResources::#{module_name}")) end - # Processing all Error Raisers modules to include them to the Client + # Processing 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}")) + Client.send(:include, Object.const_get("Crowdin::Errors::#{module_name}")) end + # Config instance that includes configuration options for the Client attr_reader :config + # Instance with established connection through RestClient to the Crowdin API attr_reader :connection + # Instance with options and headers for RestClient connection attr_reader :options + # Logger instance attr_reader :logger def initialize(&block) build_configuration(&block) - check_logger - check_rest_client_proxy + update_logger + update_rest_client_proxy build_connection end - def log!(message) - !config.logger_enabled? || logger.debug(message) + def log(message) + !logger_enabled? || logger.debug(message) end def logger=(logger) - raise_logger_are_not_enabled_error unless config.logger_enabled? - @logger = logger - end - - private + raise_logger_are_not_enabled_error unless logger_enabled? - def build_configuration - @config = Crowdin::Configuration.new - yield config if block_given? - end - - def build_connection - @connection ||= ::RestClient::Resource.new(config.base_url, build_options) + @logger = logger + update_rest_client_logger end - def build_options - @options ||= config.options.merge(headers: config.headers) + def enterprise_mode? + !!config.organization_domain end - def set_default_logger - require 'logger' - @logger ||= Logger.new($stderr) + def logger_enabled? + config.logger_enabled? end - def check_rest_client_proxy - ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false - end + private - def check_logger - config.logger_enabled? ? set_default_logger : config.enable_logger = false - end + def build_configuration + @config = Crowdin::Configuration.new + yield config if block_given? + end + + def build_connection + build_options + @connection ||= ::RestClient::Resource.new(config.base_url, options) + end + + def build_options + @options ||= config.options.merge(headers: config.headers) + end + + def set_default_logger + require 'logger' + @logger ||= Logger.new($stdout) + update_rest_client_logger + end + + def update_rest_client_logger + ::RestClient.log = @logger + end + + def update_rest_client_proxy + ENV['http_proxy'] ? ::RestClient.proxy = ENV.fetch('http_proxy') : false + end + + def update_logger + config.logger_enabled? ? set_default_logger : config.enable_logger = false + end end end diff --git a/lib/crowdin-api/client/configuration.rb b/lib/crowdin-api/client/configuration.rb index 8006685..eebf247 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -5,12 +5,12 @@ class Configuration attr_accessor :api_token attr_accessor :project_id attr_accessor :organization_domain + attr_accessor :enable_logger + alias logger_enabled? enable_logger attr_reader :target_api_url - alias logger_enabled? enable_logger - def initialize @target_api_url = '/api/v2' end @@ -33,16 +33,16 @@ def headers end def base_url - if enterprise_mode? - organization_domain.include?('.com') ? organization_domain : "https://#{organization_domain}.api.crowdin.com" - else - 'https://api.crowdin.com' - end - end - - def organization_domain? - !!organization_domain + @base_url ||= + if !!organization_domain + if organization_domain.include?('.com') + "https://#{organization_domain}" + else + "https://#{organization_domain}.api.crowdin.com" + end + else + 'https://api.crowdin.com' + end end - alias enterprise_mode? organization_domain? end end diff --git a/lib/crowdin-api/client/version.rb b/lib/crowdin-api/client/version.rb index e474b03..e10a753 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.2.1' + VERSION = '1.3.0' end end diff --git a/lib/crowdin-api/core/errors_raisers.rb b/lib/crowdin-api/core/errors_raisers.rb index ff39c0f..693868b 100644 --- a/lib/crowdin-api/core/errors_raisers.rb +++ b/lib/crowdin-api/core/errors_raisers.rb @@ -6,7 +6,7 @@ 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') + 'before setting your logger') end end diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index 5b73153..85b231f 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -3,116 +3,81 @@ module Crowdin module Web class Request - attr_reader :client - - def initialize(client, method, path, query = {}, headers = {}, destination = nil) - @client = client - @method = method - @full_path = client.config.target_api_url + path - @payload = perform_payload(query) - @headers = headers - @destination = destination - @errors = [] + attr_reader :connection + attr_reader :method + attr_reader :payload + + def initialize(connection, method, url, payload = {}) + @connection = connection[url] + @method = method + @payload = build_payload(payload) end - def perform - process_request! - process_response! + def get? + method.eql?(:get) end - private - - def process_request! - return @response = client.connection[@full_path].delete if delete_request? - return @response = client.connection[@full_path].get(@payload) if get_request? - - client.connection[@full_path].send(@method, @payload, @headers) { |response, _, _| @response = response } - rescue StandardError => error - client.log! error - - @errors << "Something went wrong while request proccessing. Details - #{error.class}" + def get + connection.get(prepare_payload(payload[:params])) end - def process_response! - return fetch_errors if @errors.any? - - begin - if @response - client.log! "args: #{@response.request.args}" - - if @response.body.empty? - @response.code - else - doc = JSON.parse(@response.body) - - client.log! "body: #{doc}" - - data = fetch_response_data(doc) - - @errors.any? ? fetch_errors : data - end - end - rescue StandardError => error - client.log! error - - @errors << "Something went wrong while response proccessing. Details - #{error.class}" - - fetch_errors - end + def delete? + method.eql?(:delete) end - def perform_payload(query) - return query if query.is_a?(File) - - get_request? ? { params: fetch_cleared_query(query) } : fetch_cleared_query(query).to_json + def delete + connection.delete end - def download_file(url) - download = URI.parse(url).open - destination = @destination || download.meta['content-disposition'] - .match(/filename=("?)(.+)\1/)[2] + def patch? + method.eql?(:patch) + end - IO.copy_stream(download, destination) + def patch + connection.patch(prepare_payload(payload[:params]), payload[:headers]) { |response, _, _| response } + end - destination - rescue StandardError => error - client.log! error + def post? + method.eql?(:post) + end - @errors << "Something went wrong while downloading file. Details - #{error.class}" + def post + connection.post(prepare_payload(payload[:params]), payload[:headers]) { |response, _, _| response } end - def fetch_errors - @errors.join(';') + def put? + method.eql?(:put) end - def fetch_response_data(doc) - if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].include?('response-content-disposition') - download_file(doc['data']['url']) - else - doc - end + def put + connection.put(prepare_payload(payload[:params]), payload[:headers]) { |response, _, _| response } end - def fetch_cleared_query(query) - case query - when Array - query.map do |el| - el.reject { |_, value| value.nil? } - end.reject(&:empty?) - when Hash - query.reject { |_, value| value.nil? } - else - query + private + + def build_payload(payload) + %i[params headers].each do |key| + payload[key] ||= {} + end + payload end - end - def get_request? - @method.eql?(:get) - end + def prepare_payload(params) + return params if params.is_a?(File) - def delete_request? - @method.eql?(:delete) - end + get? ? { params: fetch_cleared_params(params) } : fetch_cleared_params(params).to_json + end + + def fetch_cleared_params(params) + case params + when Array + params.map { |el| el.reject { |_, value| value.nil? } }.reject(&:empty?) + when Hash + params.reject { |_, value| value.nil? } + else + params + end + end end end end diff --git a/lib/crowdin-api/core/send_request.rb b/lib/crowdin-api/core/send_request.rb new file mode 100644 index 0000000..4a91e2b --- /dev/null +++ b/lib/crowdin-api/core/send_request.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +module Crowdin + module Web + class SendRequest + attr_reader :request + + def initialize(request, file_destination = nil) + @request = request + @file_destination = file_destination + @errors = [] + end + + def perform + parse_response(process_request) + end + + private + + def process_request + request.send(request.method) + rescue StandardError => e + @errors << "Something went wrong while request processing. Details - #{e.message}" + end + + def parse_response(response) + return @errors.join('; ') if @errors.any? + + begin + if response + if response.body.empty? + response.code + else + parsed_body = JSON.parse(response.body) + parsed_response = fetch_response_data(parsed_body) + + @errors.any? ? @errors.join('; ') : parsed_response + end + end + rescue StandardError => e + @errors << "Something went wrong while response processing. Details - #{e.message}" + @errors.join('; ') + end + end + + def fetch_response_data(doc) + if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].include?('response-content-disposition') + download_file(doc['data']['url']) + else + doc + end + end + + def download_file(url) + download = URI.parse(url).open + destination = @file_destination || download.meta['content-disposition'] + .match(/filename=("?)(.+)\1/)[2] + + IO.copy_stream(download, destination) + + destination + rescue StandardError => e + @errors << "Something went wrong while downloading file. Details - #{e.message}" + end + end + end +end diff --git a/lib/crowdin-api/core/utils.rb b/lib/crowdin-api/core/utils.rb deleted file mode 100644 index 80320c0..0000000 --- a/lib/crowdin-api/core/utils.rb +++ /dev/null @@ -1,10 +0,0 @@ -# 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 diff --git a/spec/api_resources/dictionaries_spec.rb b/spec/api_resources/dictionaries_spec.rb new file mode 100644 index 0000000..000dd0d --- /dev/null +++ b/spec/api_resources/dictionaries_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Dictionaries do + describe 'Default endpoints' do + describe '#list_dictionaries' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/dictionaries") + list_dictionaries = @crowdin.list_dictionaries({}, project_id) + expect(list_dictionaries).to eq(200) + end + end + + describe '#edit_dictionary' do + let(:language_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/dictionaries/#{language_id}") + edit_dictionary = @crowdin.edit_dictionary(language_id, {}, project_id) + expect(edit_dictionary).to eq(200) + end + end + end +end diff --git a/spec/api_resources/distributions_spec.rb b/spec/api_resources/distributions_spec.rb new file mode 100644 index 0000000..f2a4298 --- /dev/null +++ b/spec/api_resources/distributions_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Distributions do + describe 'Default endpoints' do + describe '#list_distributions' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/distributions") + list_distributions = @crowdin.list_distributions({}, project_id) + expect(list_distributions).to eq(200) + end + end + + describe '#add_distribution' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/distributions") + add_distribution = @crowdin.add_distribution({}, project_id) + expect(add_distribution).to eq(200) + end + end + + describe '#get_distribution' do + let(:hash) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/distributions/#{hash}") + get_distribution = @crowdin.get_distribution(hash, project_id) + expect(get_distribution).to eq(200) + end + end + + describe '#delete_distribution' do + let(:hash) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/distributions/#{hash}") + delete_distribution = @crowdin.delete_distribution(hash, project_id) + expect(delete_distribution).to eq(200) + end + end + + describe '#edit_distribution' do + let(:hash) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/distributions/#{hash}") + edit_distribution = @crowdin.edit_distribution(hash, {}, project_id) + expect(edit_distribution).to eq(200) + end + end + + describe '#get_distribution_release' do + let(:hash) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/distributions/#{hash}/release") + get_distribution_release = @crowdin.get_distribution_release(hash, project_id) + expect(get_distribution_release).to eq(200) + end + end + + describe '#release_distribution' do + let(:hash) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/distributions/#{hash}/release") + release_distribution = @crowdin.release_distribution(hash, project_id) + expect(release_distribution).to eq(200) + end + end + end +end diff --git a/spec/api_resources/glossaries_spec.rb b/spec/api_resources/glossaries_spec.rb new file mode 100644 index 0000000..39a984c --- /dev/null +++ b/spec/api_resources/glossaries_spec.rb @@ -0,0 +1,167 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Glossaries do + describe 'Default endpoints' do + describe '#list_glossaries' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/glossaries") + list_glossaries = @crowdin.list_glossaries + expect(list_glossaries).to eq(200) + end + end + + describe '#add_glossary' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/glossaries") + add_glossary = @crowdin.add_glossary + expect(add_glossary).to eq(200) + end + end + + describe '#get_glossary' do + let(:glossary_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}") + get_glossary = @crowdin.get_glossary(glossary_id) + expect(get_glossary).to eq(200) + end + end + + describe '#delete_glossary' do + let(:glossary_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}") + delete_glossary = @crowdin.delete_glossary(glossary_id) + expect(delete_glossary).to eq(200) + end + end + + describe '#edit_glossary' do + let(:glossary_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}") + edit_glossary = @crowdin.edit_glossary(glossary_id) + expect(edit_glossary).to eq(200) + end + end + + describe '#export_glossary' do + let(:glossary_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/exports") + export_glossary = @crowdin.export_glossary({}, glossary_id) + expect(export_glossary).to eq(200) + end + end + + describe '#check_glossary_export_status' do + let(:glossary_id) { 1 } + let(:export_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/exports/#{export_id}") + check_glossary_export_status = @crowdin.check_glossary_export_status(glossary_id, export_id) + expect(check_glossary_export_status).to eq(200) + end + end + + describe '#download_glossary' do + let(:glossary_id) { 1 } + let(:export_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/exports/#{export_id}/download") + download_glossary = @crowdin.download_glossary(glossary_id, export_id) + expect(download_glossary).to eq(200) + end + end + + describe '#import_glossary' do + let(:glossary_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/imports") + import_glossary = @crowdin.import_glossary(glossary_id) + expect(import_glossary).to eq(200) + end + end + + describe '#check_glossary_import_status' do + let(:glossary_id) { 1 } + let(:import_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/imports/#{import_id}") + check_glossary_import_status = @crowdin.check_glossary_import_status(glossary_id, import_id) + expect(check_glossary_import_status).to eq(200) + end + end + + describe '#list_terms' do + let(:glossary_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/terms") + list_terms = @crowdin.list_terms(glossary_id) + expect(list_terms).to eq(200) + end + end + + describe '#add_term' do + let(:glossary_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/terms") + add_term = @crowdin.add_term(glossary_id) + expect(add_term).to eq(200) + end + end + + describe '#clear_glossary' do + let(:glossary_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/terms") + clear_glossary = @crowdin.clear_glossary(glossary_id) + expect(clear_glossary).to eq(200) + end + end + + describe '#get_term' do + let(:glossary_id) { 1 } + let(:term_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/terms/#{term_id}") + get_term = @crowdin.get_term(glossary_id, term_id) + expect(get_term).to eq(200) + end + end + + describe '#delete_term' do + let(:glossary_id) { 1 } + let(:term_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/terms/#{term_id}") + delete_term = @crowdin.delete_term(glossary_id, term_id) + expect(delete_term).to eq(200) + end + end + + describe '#edit_term' do + let(:glossary_id) { 1 } + let(:term_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/glossaries/#{glossary_id}/terms/#{term_id}") + edit_term = @crowdin.edit_term(glossary_id, term_id) + expect(edit_term).to eq(200) + end + end + end +end diff --git a/spec/api_resources/labels_spec.rb b/spec/api_resources/labels_spec.rb new file mode 100644 index 0000000..097bfd7 --- /dev/null +++ b/spec/api_resources/labels_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Labels do + describe 'Default endpoints' do + describe '#list_labels' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/labels") + list_labels = @crowdin.list_labels({}, project_id) + expect(list_labels).to eq(200) + end + end + + describe '#add_label' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/labels") + add_label = @crowdin.add_label({}, project_id) + expect(add_label).to eq(200) + end + end + + describe '#get_label' do + let(:label_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/labels/#{label_id}") + get_label = @crowdin.get_label(label_id, project_id) + expect(get_label).to eq(200) + end + end + + describe '#delete_label' do + let(:label_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/labels/#{label_id}") + delete_label = @crowdin.delete_label(label_id, project_id) + expect(delete_label).to eq(200) + end + end + + describe '#edit_label' do + let(:label_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/labels/#{label_id}") + edit_label = @crowdin.edit_label(label_id, {}, project_id) + expect(edit_label).to eq(200) + end + end + + describe '#assign_label_to_strings' do + let(:label_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/labels/#{label_id}/strings") + assign_label_to_strings = @crowdin.assign_label_to_strings(label_id, {}, project_id) + expect(assign_label_to_strings).to eq(200) + end + end + + describe '#unassign_label_from_strings' do + let(:label_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/labels/#{label_id}") + unassign_label_from_strings = @crowdin.unassign_label_from_strings(label_id, {}, project_id) + expect(unassign_label_from_strings).to eq(200) + end + end + end +end diff --git a/spec/api_resources/languages_spec.rb b/spec/api_resources/languages_spec.rb new file mode 100644 index 0000000..f7cb34a --- /dev/null +++ b/spec/api_resources/languages_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Languages do + describe 'Default endpoints' do + describe '#list_languages' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/languages") + list_languages = @crowdin.list_languages + expect(list_languages).to eq(200) + end + end + + describe '#add_custom_language' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/languages") + add_custom_language = @crowdin.add_custom_language + expect(add_custom_language).to eq(200) + end + end + + describe '#get_language' do + let(:language_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/languages/#{language_id}") + get_language = @crowdin.get_language(language_id) + expect(get_language).to eq(200) + end + end + + describe '#delete_custom_language' do + let(:language_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/languages/#{language_id}") + delete_custom_language = @crowdin.delete_custom_language(language_id) + expect(delete_custom_language).to eq(200) + end + end + + describe '#edit_custom_language' do + let(:language_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/languages/#{language_id}") + edit_custom_language = @crowdin.edit_custom_language(language_id) + expect(edit_custom_language).to eq(200) + end + end + end +end diff --git a/spec/api_resources/machine_translation_engines_spec.rb b/spec/api_resources/machine_translation_engines_spec.rb new file mode 100644 index 0000000..f4ff676 --- /dev/null +++ b/spec/api_resources/machine_translation_engines_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::MachineTranslationEngines do + describe 'Default endpoints' do + describe '#list_mts' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/mts") + list_mts = @crowdin.list_mts + expect(list_mts).to eq(200) + end + end + + describe '#get_mt' do + let(:mt_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/mts/#{mt_id}") + get_mt = @crowdin.get_mt(mt_id) + expect(get_mt).to eq(200) + end + end + + describe '#translate_via_mt' do + let(:mt_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/mts/#{mt_id}/translations") + translate_via_mt = @crowdin.translate_via_mt(mt_id) + expect(translate_via_mt).to eq(200) + end + end + end + + describe 'Enterprise endpoints' do + describe '#edit_mt' do + let(:mt_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:patch, "https://domain.api.crowdin.com/#{target_api_url}/mts/#{mt_id}") + edit_mt = @crowdin.edit_mt(mt_id) + expect(edit_mt).to eq(200) + end + end + + describe '#add_mt' do + it 'when request are valid', :enterprise do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/mts") + add_mt = @crowdin.add_mt + expect(add_mt).to eq(200) + end + end + + describe '#delete_mt' do + let(:mt_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:delete, "https://domain.api.crowdin.com/#{target_api_url}/mts/#{mt_id}") + delete_mt = @crowdin.delete_mt(mt_id) + expect(delete_mt).to eq(200) + end + end + end +end diff --git a/spec/api_resources/projects_spec.rb b/spec/api_resources/projects_spec.rb new file mode 100644 index 0000000..3727d16 --- /dev/null +++ b/spec/api_resources/projects_spec.rb @@ -0,0 +1,215 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Projects do + describe 'Default endpoints' do + describe '#list_projects' do + let(:limit) { 1 } + let(:offset) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects") + list_projects = @crowdin.list_projects + expect(list_projects).to eq(200) + end + + it 'when request are valid and includes query parameters', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects") + .with(query: { limit: limit, offset: offset }) + list_projects = @crowdin.list_projects({ limit: limit, offset: offset }) + expect(list_projects).to eq(200) + end + end + + describe '#add_project' do + let(:project_name) { 'test_project' } + let(:source_language_id) { 'en' } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects") + .with(body: { name: project_name, sourceLanguageId: source_language_id }) + add_project = @crowdin.add_project(name: project_name, sourceLanguageId: source_language_id) + expect(add_project).to eq(200) + end + + let(:body) do + { "data" => { "id" => 1, "name" => "test_project" } } + end + + it 'when request are valid and returns body', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects") + .with(body: { name: project_name, sourceLanguageId: source_language_id }) + .to_return(body: body.to_json) + add_project = @crowdin.add_project(name: project_name, sourceLanguageId: source_language_id) + expect(add_project).to eq(body) + end + end + + describe '#get_project' do + before do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}") + end + + let(:project_id) { 1 } + + it 'when request are valid', :default do + get_project = @crowdin.get_project(project_id) + expect(get_project).to eq(200) + end + + it 'when request are invalid', :default do + expect { @crowdin.get_project }.to raise_error(ArgumentError) + end + end + + describe '#delete_project' do + before do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}") + end + + let(:project_id) { 1 } + + it 'when request are valid', :default do + delete_project = @crowdin.delete_project(project_id) + expect(delete_project).to eq(200) + end + + it 'when request are invalid', :default do + expect { @crowdin.delete_project }.to raise_error(ArgumentError) + end + end + + describe '#edit_project' do + let(:project_id) { 1 } + let(:body) { [{ op: 'replace', path: '/name', value: 'project_name' }] } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}") + .with(body: body.to_json) + edit_project = @crowdin.edit_project(project_id, body) + expect(edit_project).to eq(200) + end + + it 'when request are invalid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}") + expect { @crowdin.edit_project }.to raise_error(ArgumentError) + end + end + end + + describe 'Enterprise endpoints' do + describe '#list_groups' do + let(:limit) { 1 } + let(:offset) { 1 } + + it 'when Enterprise mode is not enabled', :default do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/groups") + expect { @crowdin.list_groups }.to raise_error(Crowdin::Errors::OnlyForEnterpriseModeError) + end + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/groups") + list_groups = @crowdin.list_groups + expect(list_groups).to eq(200) + end + + it 'when request are valid and includes query parameters', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/groups") + .with(query: { limit: limit, offset: offset }) + list_groups = @crowdin.list_groups({ limit: limit, offset: offset }) + expect(list_groups).to eq(200) + end + end + + describe '#add_group' do + let(:group_name) { 'test_group' } + + it 'when Enterprise mode is not enabled', :default do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/groups") + expect { @crowdin.add_group(name: group_name) }.to raise_error(Crowdin::Errors::OnlyForEnterpriseModeError) + end + + it 'when request are valid', :enterprise do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/groups") + .with(body: { name: group_name }) + add_group = @crowdin.add_group(name: group_name) + expect(add_group).to eq(200) + end + + let(:body) do + { "data"=> { "id"=>1, "name"=>"test_group" } } + end + + it 'when request are valid and returns body', :enterprise do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/groups") + .with(body: { name: group_name }) + .to_return(body: body.to_json) + add_group = @crowdin.add_group(name: group_name) + expect(add_group).to eq(body) + end + end + + describe '#get_group' do + before do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/groups/#{group_id}") + end + + let(:group_id) { 1 } + + it 'when Enterprise mode is not enabled', :default do + expect { @crowdin.get_group(group_id) }.to raise_error(Crowdin::Errors::OnlyForEnterpriseModeError) + end + + it 'when request are valid', :enterprise do + get_group = @crowdin.get_group(group_id) + expect(get_group).to eq(200) + end + + it 'when request are invalid', :enterprise do + expect { @crowdin.get_group }.to raise_error(ArgumentError) + end + end + + describe '#delete_group' do + before do + stub_request(:delete, "https://domain.api.crowdin.com/#{target_api_url}/groups/#{group_id}") + end + + let(:group_id) { 1 } + + it 'when Enterprise mode is not enabled', :default do + expect { @crowdin.delete_group(group_id) }.to raise_error(Crowdin::Errors::OnlyForEnterpriseModeError) + end + + it 'when request are valid', :enterprise do + delete_group = @crowdin.delete_group(group_id) + expect(delete_group).to eq(200) + end + + it 'when request are invalid', :enterprise do + expect { @crowdin.delete_group }.to raise_error(ArgumentError) + end + end + + describe '#edit_group' do + let(:group_id) { 1 } + let(:body) { [{ op: 'replace', path: '/name', value: 'group_name' }] } + + it 'when Enterprise mode is not enabled', :default do + stub_request(:patch, "https://domain.api.crowdin.com/#{target_api_url}/groups/#{group_id}") + expect { @crowdin.edit_group(group_id, body) }.to raise_error(Crowdin::Errors::OnlyForEnterpriseModeError) + end + + it 'when request are valid', :enterprise do + stub_request(:patch, "https://domain.api.crowdin.com/#{target_api_url}/groups/#{group_id}") + .with(body: body.to_json) + edit_group = @crowdin.edit_group(group_id, body) + expect(edit_group).to eq(200) + end + + it 'when request are invalid', :enterprise do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/groups/#{group_id}") + expect { @crowdin.edit_group }.to raise_error(ArgumentError) + end + end + end +end diff --git a/spec/api_resources/reports_spec.rb b/spec/api_resources/reports_spec.rb new file mode 100644 index 0000000..7f1883a --- /dev/null +++ b/spec/api_resources/reports_spec.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Reports do + describe 'Default endpoints' do + describe '#generate_report' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/reports") + generate_report = @crowdin.generate_report({}, project_id) + expect(generate_report).to eq(200) + end + end + + describe '#check_report_generation_status' do + let(:report_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/reports/#{report_id}") + check_report_generation_status = @crowdin.check_report_generation_status(report_id, project_id) + expect(check_report_generation_status).to eq(200) + end + end + + describe '#download_report' do + let(:report_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/reports/#{report_id}/download") + download_report = @crowdin.download_report(report_id, nil, project_id) + expect(download_report).to eq(200) + end + end + end + + describe 'Enterprise endpoints' do + describe '#generate_group_report' do + let(:group_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/groups/#{group_id}/reports") + generate_group_report = @crowdin.generate_group_report(group_id, project_id) + expect(generate_group_report).to eq(200) + end + end + + describe '#check_group_report_generation_status' do + let(:group_id) { 1 } + let(:report_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/groups/#{group_id}/reports/#{report_id}") + check_group_report_generation_status = @crowdin.check_group_report_generation_status(group_id, report_id) + expect(check_group_report_generation_status).to eq(200) + end + end + + describe '#download_group_report' do + let(:group_id) { 1 } + let(:report_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/groups/#{group_id}/reports/#{report_id}/download") + download_group_report = @crowdin.download_group_report(group_id, project_id) + expect(download_group_report).to eq(200) + end + end + + describe '#generate_organization_report' do + it 'when request are valid', :enterprise do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/reports") + generate_organization_report = @crowdin.generate_organization_report + expect(generate_organization_report).to eq(200) + end + end + + describe '#check_organization_report_generation_status' do + let(:report_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/reports/#{report_id}") + check_organization_report_generation_status = @crowdin.check_organization_report_generation_status(report_id) + expect(check_organization_report_generation_status).to eq(200) + end + end + + describe '#download_organization_report' do + let(:report_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/reports/#{report_id}/download") + download_organization_report = @crowdin.download_organization_report(report_id) + expect(download_organization_report).to eq(200) + end + end + end +end diff --git a/spec/api_resources/screenshots_spec.rb b/spec/api_resources/screenshots_spec.rb new file mode 100644 index 0000000..80bbd7e --- /dev/null +++ b/spec/api_resources/screenshots_spec.rb @@ -0,0 +1,134 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Screenshots do + describe 'Default endpoints' do + describe '#list_screenshots' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots") + list_screenshots = @crowdin.list_screenshots({}, project_id) + expect(list_screenshots).to eq(200) + end + end + + describe '#add_screenshot' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots") + add_screenshot = @crowdin.add_screenshot({}, project_id) + expect(add_screenshot).to eq(200) + end + end + + describe '#get_screenshot' do + let(:screenshot_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}") + get_screenshot = @crowdin.get_screenshot(screenshot_id, project_id) + expect(get_screenshot).to eq(200) + end + end + + describe '#update_screenshot' do + let(:screenshot_id) { 1 } + + it 'when request are valid', :default do + stub_request(:put, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}") + update_screenshot = @crowdin.update_screenshot(screenshot_id, {}, project_id) + expect(update_screenshot).to eq(200) + end + end + + describe '#delete_screenshot' do + let(:screenshot_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}") + delete_screenshot = @crowdin.delete_screenshot(screenshot_id, project_id) + expect(delete_screenshot).to eq(200) + end + end + + describe '#edit_screenshot' do + let(:screenshot_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}") + edit_screenshot = @crowdin.edit_screenshot(screenshot_id, {}, project_id) + expect(edit_screenshot).to eq(200) + end + end + + describe '#list_tags' do + let(:screenshot_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags") + list_tags = @crowdin.list_tags(screenshot_id, {}, project_id) + expect(list_tags).to eq(200) + end + end + + describe '#replace_tags' do + let(:screenshot_id) { 1 } + + it 'when request are valid', :default do + stub_request(:put, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags") + replace_tags = @crowdin.replace_tags(screenshot_id, {}, project_id) + expect(replace_tags).to eq(200) + end + end + + describe '#add_tag' do + let(:screenshot_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags") + add_tag = @crowdin.add_tag(screenshot_id, {}, project_id) + expect(add_tag).to eq(200) + end + end + + describe '#clear_tags' do + let(:screenshot_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags") + clear_tags = @crowdin.clear_tags(screenshot_id, project_id) + expect(clear_tags).to eq(200) + end + end + + describe '#get_tag' do + let(:screenshot_id) { 1 } + let(:tag_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}") + get_tag = @crowdin.get_tag(screenshot_id, tag_id, project_id) + expect(get_tag).to eq(200) + end + end + + describe '#delete_tag' do + let(:screenshot_id) { 1 } + let(:tag_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}") + delete_tag = @crowdin.delete_tag(screenshot_id, tag_id, project_id) + expect(delete_tag).to eq(200) + end + end + + describe '#edit_tag' do + let(:screenshot_id) { 1 } + let(:tag_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/screenshots/#{screenshot_id}/tags/#{tag_id}") + edit_tag = @crowdin.edit_tag(screenshot_id, tag_id, {}, project_id) + expect(edit_tag).to eq(200) + end + end + end +end diff --git a/spec/api_resources/source_files_spec.rb b/spec/api_resources/source_files_spec.rb new file mode 100644 index 0000000..6e12ef4 --- /dev/null +++ b/spec/api_resources/source_files_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::SourceFiles do + describe 'Default endpoints' do + describe '#list_branches' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/branches") + list_branches = @crowdin.list_branches({}, project_id) + expect(list_branches).to eq(200) + end + end + end +end diff --git a/spec/api_resources/source_strings_spec.rb b/spec/api_resources/source_strings_spec.rb new file mode 100644 index 0000000..818ea1a --- /dev/null +++ b/spec/api_resources/source_strings_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::SourceStrings do + describe 'Default endpoints' do + describe '#list_strings' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/strings") + list_strings = @crowdin.list_strings({}, project_id) + expect(list_strings).to eq(200) + end + end + + describe '#add_string' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/strings") + add_string = @crowdin.add_string({}, project_id) + expect(add_string).to eq(200) + end + end + + describe '#get_string' do + let(:string_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/strings/#{string_id}") + get_string = @crowdin.get_string(string_id, {}, project_id) + expect(get_string).to eq(200) + end + end + + describe '#delete_string' do + let(:string_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/strings/#{string_id}") + delete_string = @crowdin.delete_string(string_id, project_id) + expect(delete_string).to eq(200) + end + end + + describe '#edit_string' do + let(:string_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/strings/#{string_id}") + edit_string = @crowdin.edit_string(string_id, {}, project_id) + expect(edit_string).to eq(200) + end + end + end +end diff --git a/spec/api_resources/storages_spec.rb b/spec/api_resources/storages_spec.rb new file mode 100644 index 0000000..a804321 --- /dev/null +++ b/spec/api_resources/storages_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Storages do + describe 'Default endpoints' do + describe '#list_storages' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/storages") + list_storages = @crowdin.list_storages + expect(list_storages).to eq(200) + end + end + end +end diff --git a/spec/api_resources/string_comments_spec.rb b/spec/api_resources/string_comments_spec.rb new file mode 100644 index 0000000..7678417 --- /dev/null +++ b/spec/api_resources/string_comments_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::StringComments do + describe 'Default endpoints' do + describe '#list_string_comments' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/comments") + list_string_comments = @crowdin.list_string_comments({}, project_id) + expect(list_string_comments).to eq(200) + end + end + end +end diff --git a/spec/api_resources/string_translations_spec.rb b/spec/api_resources/string_translations_spec.rb new file mode 100644 index 0000000..ec8f35b --- /dev/null +++ b/spec/api_resources/string_translations_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::StringTranslations do + describe 'Default endpoints' do + describe '#list_translation_approvals' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/approvals") + list_translation_approvals = @crowdin.list_translation_approvals({}, project_id) + expect(list_translation_approvals).to eq(200) + end + end + end +end diff --git a/spec/api_resources/tasks_spec.rb b/spec/api_resources/tasks_spec.rb new file mode 100644 index 0000000..06c57c0 --- /dev/null +++ b/spec/api_resources/tasks_spec.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Tasks do + describe 'Default endpoints' do + describe '#list_tasks' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/tasks") + list_tasks = @crowdin.list_tasks({}, project_id) + expect(list_tasks).to eq(200) + end + end + + describe '#add_task' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/tasks") + add_task = @crowdin.add_task({}, project_id) + expect(add_task).to eq(200) + end + end + + describe '#export_task_strings' do + let(:task_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/tasks/#{task_id}/exports") + export_task_strings = @crowdin.export_task_strings(task_id, nil, project_id) + expect(export_task_strings).to eq(200) + end + end + + describe '#get_task' do + let(:task_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/tasks/#{task_id}") + get_task = @crowdin.get_task(task_id, project_id) + expect(get_task).to eq(200) + end + end + + describe '#delete_task' do + let(:task_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/tasks/#{task_id}") + delete_task = @crowdin.delete_task(task_id, project_id) + expect(delete_task).to eq(200) + end + end + + describe '#edit_task' do + let(:task_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/tasks/#{task_id}") + edit_task = @crowdin.edit_task(task_id, {}, project_id) + expect(edit_task).to eq(200) + end + end + + describe '#list_user_tasks' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/user/tasks") + list_user_tasks = @crowdin.list_user_tasks({}) + expect(list_user_tasks).to eq(200) + end + end + + describe '#edit_task_archived_status' do + let(:task_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/user/tasks/#{task_id}") + edit_task_archived_status = @crowdin.edit_task_archived_status(task_id, {}) + expect(edit_task_archived_status).to eq(200) + end + end + end +end diff --git a/spec/api_resources/teams_spec.rb b/spec/api_resources/teams_spec.rb new file mode 100644 index 0000000..72989c4 --- /dev/null +++ b/spec/api_resources/teams_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Teams do + describe 'Enterprise endpoints' do + describe '#add_team_to_project' do + it 'when request are valid', :enterprise do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/projects/#{project_id}/teams") + add_team_to_project = @crowdin.add_team_to_project({}, project_id) + expect(add_team_to_project).to eq(200) + end + end + end +end diff --git a/spec/api_resources/translation_memory_spec.rb b/spec/api_resources/translation_memory_spec.rb new file mode 100644 index 0000000..b4ae3aa --- /dev/null +++ b/spec/api_resources/translation_memory_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::TranslationMemory do + describe 'Default endpoints' do + describe '#list_tms' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/tms") + list_tms = @crowdin.list_tms + expect(list_tms).to eq(200) + end + end + end +end diff --git a/spec/api_resources/translation_status_spec.rb b/spec/api_resources/translation_status_spec.rb new file mode 100644 index 0000000..2f4d87d --- /dev/null +++ b/spec/api_resources/translation_status_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::TranslationStatus do + describe 'Default endpoints' do + describe '#get_branch_progress' do + let(:branch_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/branches/#{branch_id}/languages/progress") + get_branch_progress = @crowdin.get_branch_progress(branch_id, {}, project_id) + expect(get_branch_progress).to eq(200) + end + end + end +end diff --git a/spec/api_resources/translations_spec.rb b/spec/api_resources/translations_spec.rb new file mode 100644 index 0000000..e517b6f --- /dev/null +++ b/spec/api_resources/translations_spec.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Translations do + describe 'Default endpoints' do + describe '#pre_translation_status' do + let(:pre_translation_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/pre-translations/#{pre_translation_id}") + pre_translation_status = @crowdin.pre_translation_status(pre_translation_id, project_id) + expect(pre_translation_status).to eq(200) + end + end + + describe '#apply_pre_translation' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/pre-translations") + apply_pre_translation = @crowdin.apply_pre_translation({}, project_id) + expect(apply_pre_translation).to eq(200) + end + end + + describe '#build_project_directory_translation' do + let(:directory_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds/directories/#{directory_id}") + build_project_directory_translation = @crowdin.build_project_directory_translation(directory_id, {}, project_id) + expect(build_project_directory_translation).to eq(200) + end + end + + describe '#build_project_file_translation' do + let(:file_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds/files/#{file_id}") + build_project_file_translation = @crowdin.build_project_file_translation(file_id, {}, nil, project_id) + expect(build_project_file_translation).to eq(200) + end + end + + describe '#list_project_builds' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds") + list_project_builds = @crowdin.list_project_builds({}, project_id) + expect(list_project_builds).to eq(200) + end + end + + describe '#build_project_translation' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds") + build_project_translation = @crowdin.build_project_translation({}, project_id) + expect(build_project_translation).to eq(200) + end + end + + describe '#upload_translations' do + let(:language_id) { 1 } + + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/#{language_id}") + upload_translations = @crowdin.upload_translations(language_id, {}, project_id) + expect(upload_translations).to eq(200) + end + end + + describe '#download_project_translations' do + let(:build_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds/#{build_id}/download") + download_project_translations = @crowdin.download_project_translations(build_id, nil, project_id) + expect(download_project_translations).to eq(200) + end + end + + describe '#check_project_build_status' do + let(:build_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds/#{build_id}") + check_project_build_status = @crowdin.check_project_build_status(build_id, project_id) + expect(check_project_build_status).to eq(200) + end + end + + describe '#cancel_build' do + let(:build_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds/#{build_id}") + cancel_build = @crowdin.cancel_build(build_id, project_id) + expect(cancel_build).to eq(200) + end + end + + describe '#export_project_translation' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/exports") + export_project_translation = @crowdin.export_project_translation({}, nil, project_id) + expect(export_project_translation).to eq(200) + end + end + end +end diff --git a/spec/api_resources/users_spec.rb b/spec/api_resources/users_spec.rb new file mode 100644 index 0000000..9deee1a --- /dev/null +++ b/spec/api_resources/users_spec.rb @@ -0,0 +1,117 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Users do + describe 'Default endpoints' do + describe '#get_authenticated_user' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/user") + get_authenticated_user = @crowdin.get_authenticated_user + expect(get_authenticated_user).to eq(200) + end + end + + describe '#list_project_members' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/members") + list_project_members = @crowdin.list_project_members({}, project_id) + expect(list_project_members).to eq(200) + end + end + + describe '#get_member_info' do + let(:member_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/members/#{member_id}") + get_member_info = @crowdin.get_member_info(member_id, project_id) + expect(get_member_info).to eq(200) + end + end + end + + describe 'Enterprise endpoints' do + describe '#add_project_member' do + it 'when request are valid', :enterprise do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/projects/#{project_id}/members") + add_project_member = @crowdin.add_project_member({}, project_id) + expect(add_project_member).to eq(200) + end + end + + describe '#get_project_member_permissions' do + let(:member_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/projects/#{project_id}/members/#{member_id}") + get_project_member_permissions = @crowdin.get_project_member_permissions(member_id, project_id) + expect(get_project_member_permissions).to eq(200) + end + end + + describe '#replace_project_permissions' do + let(:member_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:put, "https://domain.api.crowdin.com/#{target_api_url}/projects/#{project_id}/members/#{member_id}") + replace_project_permissions = @crowdin.replace_project_permissions(member_id, {}, project_id) + expect(replace_project_permissions).to eq(200) + end + end + + describe '#delete_member_from_project' do + let(:member_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:delete, "https://domain.api.crowdin.com/#{target_api_url}/projects/#{project_id}/members/#{member_id}") + delete_member_from_project = @crowdin.delete_member_from_project(member_id, {}, project_id) + expect(delete_member_from_project).to eq(200) + end + end + + describe '#list_users' do + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/users") + list_users = @crowdin.list_users + expect(list_users).to eq(200) + end + end + + describe '#invite_user' do + it 'when request are valid', :enterprise do + stub_request(:post, "https://domain.api.crowdin.com/#{target_api_url}/users") + invite_user = @crowdin.invite_user + expect(invite_user).to eq(200) + end + end + + describe '#get_user' do + let(:user_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/users/#{user_id}") + get_user = @crowdin.get_user(user_id) + expect(get_user).to eq(200) + end + end + + describe '#delete_user' do + let(:user_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:delete, "https://domain.api.crowdin.com/#{target_api_url}/users/#{user_id}") + delete_user = @crowdin.delete_user(user_id) + expect(delete_user).to eq(200) + end + end + + describe '#edit_user' do + let(:user_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:patch, "https://domain.api.crowdin.com/#{target_api_url}/users/#{user_id}") + edit_user = @crowdin.edit_user(user_id) + expect(edit_user).to eq(200) + end + end + end +end diff --git a/spec/api_resources/vendors_spec.rb b/spec/api_resources/vendors_spec.rb new file mode 100644 index 0000000..52dbaed --- /dev/null +++ b/spec/api_resources/vendors_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Vendors do + describe 'Enterprise endpoints' do + describe '#list_vendors' do + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/vendors") + list_vendors = @crowdin.list_vendors + expect(list_vendors).to eq(200) + end + end + end +end diff --git a/spec/api_resources/webhooks_spec.rb b/spec/api_resources/webhooks_spec.rb new file mode 100644 index 0000000..57a11a0 --- /dev/null +++ b/spec/api_resources/webhooks_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Webhooks do + describe 'Default endpoints' do + describe '#list_webhooks' do + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/webhooks") + list_webhooks = @crowdin.list_webhooks({}, project_id) + expect(list_webhooks).to eq(200) + end + end + + describe '#add_webhook' do + it 'when request are valid', :default do + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/webhooks") + add_webhook = @crowdin.add_webhook({}, project_id) + expect(add_webhook).to eq(200) + end + end + + describe '#get_webhook' do + let(:webhook_id) { 1 } + + it 'when request are valid', :default do + stub_request(:get, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}") + get_webhook = @crowdin.get_webhook(webhook_id, project_id) + expect(get_webhook).to eq(200) + end + end + + describe '#delete_webhook' do + let(:webhook_id) { 1 } + + it 'when request are valid', :default do + stub_request(:delete, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}") + delete_webhook = @crowdin.delete_webhook(webhook_id, project_id) + expect(delete_webhook).to eq(200) + end + end + + describe '#edit_webhook' do + let(:webhook_id) { 1 } + + it 'when request are valid', :default do + stub_request(:patch, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/webhooks/#{webhook_id}") + edit_webhook = @crowdin.edit_webhook(webhook_id, {}, project_id) + expect(edit_webhook).to eq(200) + end + end + end +end diff --git a/spec/api_resources/workflows_spec.rb b/spec/api_resources/workflows_spec.rb new file mode 100644 index 0000000..71b3997 --- /dev/null +++ b/spec/api_resources/workflows_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +describe Crowdin::ApiResources::Workflows do + describe 'Enterprise endpoints' do + describe '#list_workflow_steps' do + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/projects/#{project_id}/workflow-steps") + list_workflow_steps = @crowdin.list_workflow_steps({}, project_id) + expect(list_workflow_steps).to eq(200) + end + end + + describe '#get_workflow_step' do + let(:step_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/projects/#{project_id}/workflow-steps/#{step_id}") + get_workflow_step = @crowdin.get_workflow_step(step_id, project_id) + expect(get_workflow_step).to eq(200) + end + end + + describe '#list_workflow_templates' do + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/workflow-templates") + list_workflow_templates = @crowdin.list_workflow_templates + expect(list_workflow_templates).to eq(200) + end + end + + describe '#get_workflow_template' do + let(:template_id) { 1 } + + it 'when request are valid', :enterprise do + stub_request(:get, "https://domain.api.crowdin.com/#{target_api_url}/workflow-templates/#{template_id}") + get_workflow_template = @crowdin.get_workflow_template(template_id) + expect(get_workflow_template).to eq(200) + end + end + end +end diff --git a/spec/client/client-instance_spec.rb b/spec/client/client-instance_spec.rb deleted file mode 100644 index b9e4bab..0000000 --- a/spec/client/client-instance_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -# 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/client/configuration-instance_spec.rb b/spec/client/configuration-instance_spec.rb deleted file mode 100644 index 67319e1..0000000 --- a/spec/client/configuration-instance_spec.rb +++ /dev/null @@ -1,72 +0,0 @@ -# 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 #project_id' do - expect(@crowdin.config.project_id).to_not be_nil - end - - it 'should have a #api_token' do - expect(@crowdin.config.api_token).to_not be_nil - end - - it '#target_api_url should equal \'api/v2\' by default' do - expect(@crowdin.config.target_api_url).to eq('/api/v2') - end - - describe '#logger_enabled?' do - it 'should equal false by default' do - expect(@crowdin.config.logger_enabled?).to be_falsey - end - - it 'should equal specified argument' do - @crowdin = Crowdin::Client.new do |config| - config.enable_logger = true - end - - expect(@crowdin.config.logger_enabled?).to be_truthy - end - end - - describe '#enterprise_mode?' do - it 'should equal false by default' do - expect(@crowdin.config.enterprise_mode?).to be_falsey - end - - it 'should equal true if user specify organization domain' do - @crowdin = Crowdin::Client.new do |config| - config.organization_domain = 'organization_domain' - end - - expect(@crowdin.config.enterprise_mode?).to be_truthy - end - end - - describe '#base_url' do - it 'should equal \'https://api.crowdin.com\' by default' do - expect(@crowdin.config.base_url).to eq('https://api.crowdin.com') - end - - it 'should equal specified organization domain' do - @crowdin = Crowdin::Client.new do |config| - config.organization_domain = 'organization_domain' - end - - 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 - @crowdin = Crowdin::Client.new do |config| - config.organization_domain = 'organization_domain.com' - end - - expect(@crowdin.config.base_url).to eq('organization_domain.com') - end - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 294cbbc..b4bb515 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,27 @@ # frozen_string_literal: true require 'simplecov' -SimpleCov.start - require 'codecov' + +SimpleCov.start SimpleCov.formatter = SimpleCov::Formatter::Codecov +require 'webmock/rspec' require 'crowdin-api' + +RSpec.shared_context 'crowdin_client_options' do + let(:api_token) { 'api_token' } + let(:project_id) { 1 } + let(:organization_domain) { 'domain' } + let(:full_organization_domain) { 'domain.com' } + let(:target_api_url) { 'api/v2' } +end + +RSpec.configure do |config| + config.include_context 'crowdin_client_options' + + config.before(:each) do |spec| + @crowdin = Crowdin::Client.new { |cfg| cfg.organization_domain = 'domain' } if spec.metadata[:enterprise] + @crowdin = Crowdin::Client.new if spec.metadata[:default] + end +end diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb new file mode 100644 index 0000000..9110fa5 --- /dev/null +++ b/spec/unit/client_spec.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +describe 'Crowdin Client' do + it 'should have a list of API Resources' do + expect(Crowdin::API_RESOURCES_MODULES).to_not be_nil + end + + it 'should have a list of Error Raisers' do + expect(Crowdin::ERROR_RAISERS_MODULES).to_not be_nil + end + + it 'should have a version' do + expect(Crowdin::Client::VERSION).to_not be_nil + end + + describe 'Crowdin Client with a default options' do + it 'should have a RestClient connection', :default do + expect(@crowdin.connection.class).to eq(RestClient::Resource) + end + + it 'should have options and headers for RestClient', :default do + expect(@crowdin.options.class).to eq(Hash) + expect(@crowdin.options).to include(:headers) + end + + it 'should have a Config instance', :default do + expect(@crowdin.config.class).to eq(Crowdin::Configuration) + end + + it 'should have a disabled Logger by default', :default do + expect(@crowdin.logger_enabled?).to be_falsey + expect(@crowdin.logger).to be_nil + end + + it 'should have a disabled Enterprise mode by default', :default do + expect(@crowdin.enterprise_mode?).to be_falsey + end + + it 'should have a default target api url', :default do + expect(@crowdin.config.target_api_url).to eq('/api/v2') + end + it 'should have a default base url', :default do + expect(@crowdin.config.base_url).to eq('https://api.crowdin.com') + end + end + + describe 'Crowdin Client with enabled Logger' do + before do + @crowdin = Crowdin::Client.new do |config| + config.enable_logger = true + end + end + + it 'should have a Logger instance' do + expect(@crowdin.logger_enabled?).to be_truthy + expect(@crowdin.logger.class).to eq(Logger) + end + end + + describe 'Crowdin Client with enabled Enterprise mode' do + before do + @crowdin = Crowdin::Client.new do |config| + config.organization_domain = organization_domain + end + end + + it 'should have a generated Enterprise url' do + expect(@crowdin.enterprise_mode?).to be_truthy + expect(@crowdin.config.base_url).to eq("https://#{organization_domain}.api.crowdin.com") + end + end + + describe 'Crowdin Client with enabled Enterprise mode and specified full organization domain' do + before do + @crowdin = Crowdin::Client.new do |config| + config.organization_domain = full_organization_domain + end + end + + it 'should have a generated Enterprise url' do + expect(@crowdin.enterprise_mode?).to be_truthy + expect(@crowdin.config.base_url).to eq("https://#{full_organization_domain}") + end + end +end