diff --git a/.gitignore b/.gitignore index 639f196..98bc812 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,12 @@ -# Ruby logs +# Ignore Ruby logs .ruby-version .idea -# MacOS logs +# Ignore MacOS logs .DS_Store -# Versioning +# Ignore versioning file Gemfile.lock + +# Ignore tests coverage folder +coverage diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2ea13fd..32267ad 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-11-29 03:21:36 UTC using RuboCop version 1.23.0. +# on 2021-12-13 20:26:23 UTC using RuboCop version 1.23.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -21,6 +21,13 @@ Gemspec/RequiredRubyVersion: Exclude: - 'crowdin-api.gemspec' +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment. +Layout/ExtraSpacing: + Exclude: + - 'bin/crowdin-console' + # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator. @@ -29,20 +36,21 @@ Layout/SpaceAroundOperators: Exclude: - 'crowdin-api.gemspec' -# Offense count: 1 -Lint/MissingSuper: - Exclude: - - 'lib/crowdin-api/core/errors.rb' - # Offense count: 2 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: - Max: 20 + Max: 23 + +# Offense count: 1 +# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +# IgnoredMethods: refine +Metrics/BlockLength: + Max: 54 # Offense count: 4 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. Metrics/MethodLength: - Max: 14 + Max: 18 # Offense count: 2 # Configuration parameters: CountComments, CountAsOne. @@ -69,6 +77,13 @@ Naming/FileName: - 'spec/core/config-instance_spec.rb' - 'spec/crowdin-api_spec.rb' +# Offense count: 3 +# Configuration parameters: EnforcedStyleForLeadingUnderscores. +# SupportedStylesForLeadingUnderscores: disallowed, required, optional +Naming/MemoizedInstanceVariableName: + Exclude: + - 'lib/crowdin-api/client/client.rb' + # Offense count: 3 # Cop supports --auto-correct. # Configuration parameters: PreferredName. @@ -85,13 +100,13 @@ Style/AccessorGrouping: - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' -# Offense count: 1 +# Offense count: 2 # Cop supports --auto-correct. Style/CollectionCompact: Exclude: - - 'lib/crowdin-api/core/payload.rb' + - 'lib/crowdin-api/core/request.rb' -# Offense count: 11 +# Offense count: 12 # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: @@ -100,13 +115,14 @@ Style/Documentation: - 'lib/crowdin-api/api-resources/languages.rb' - 'lib/crowdin-api/api-resources/projects.rb' - 'lib/crowdin-api/api-resources/source_files.rb' + - 'lib/crowdin-api/api-resources/source_strings.rb' - 'lib/crowdin-api/api-resources/storages.rb' - 'lib/crowdin-api/api-resources/translation_status.rb' - 'lib/crowdin-api/api-resources/translations.rb' + - 'lib/crowdin-api/api-resources/workflows.rb' - 'lib/crowdin-api/client/client.rb' - 'lib/crowdin-api/client/configuration.rb' - - 'lib/crowdin-api/core/errors.rb' - - 'lib/crowdin-api/core/payload.rb' + - 'lib/crowdin-api/core/api_errors_raiser.rb' - 'lib/crowdin-api/core/request.rb' # Offense count: 1 @@ -116,3 +132,8 @@ Style/Documentation: Style/FrozenStringLiteralComment: Exclude: - 'bin/crowdin-console' + +# Offense count: 1 +Style/MixinUsage: + Exclude: + - 'bin/crowdin-console' diff --git a/README.md b/README.md index 395b8cd..1bb7b94 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ For more about Crowdin API v2 see the documentation: [![Gem](https://img.shields.io/gem/dtv/crowdin-api?cacheSeconds=1800)](https://rubygems.org/gems/crowdin-api) [![Test and Lint](https://github.com/crowdin/crowdin-api-client-ruby/actions/workflows/test-and-lint.yml/badge.svg)](https://github.com/crowdin/crowdin-api-client-ruby/actions/workflows/test-and-lint.yml) -[![codecov](https://codecov.io/gh/crowdin/crowdin-api-client-ruby/branch/master/graph/badge.svg?token=OJsyJwQbFM)](https://codecov.io/gh/crowdin/crowdin-api-client-ruby) +[![codecov](https://codecov.io/gh/crowdin/crowdin-api-client-ruby/branch/main/graph/badge.svg?token=OJsyJwQbFM)](https://codecov.io/gh/crowdin/crowdin-api-client-ruby) [![GitHub issues](https://img.shields.io/github/issues/crowdin/crowdin-api-client-ruby?cacheSeconds=1800)](https://github.com/crowdin/crowdin-api-client-ruby/issues) [![GitHub](https://img.shields.io/github/license/crowdin/crowdin-api-client-ruby?cacheSeconds=1800)](https://github.com/crowdin/crowdin-api-client-ruby/blob/main/LICENSE) @@ -36,7 +36,7 @@ For more about Crowdin API v2 see the documentation: Add this line to your application's Gemfile: ```gemfile -gem 'crowdin-api', '~> 1.0.0' +gem 'crowdin-api', '~> 1.1.0' ``` And then execute: @@ -65,25 +65,31 @@ gem install crowdin-api ```ruby require 'crowdin-api' -# Create a new Crowdin Client object. +# Initialize a new Client instance with config options crowdin = Crowdin::Client.new do |config| config.api_token = 'YourApiToken' end -# or you can create Enterprise instance by specify your organization_domain + +# Or you can intialize Enterprise Client instance by specifying your organization_domain in config options crowdin = Crowdin::Client.new do |config| config.api_token = 'YourEnterpriseApiToken' config.organization_domain = 'YourOrganizationDomain' end +# Note: we use full specified organization domain if that includes .com +# config.organization_domain = your_domain -> https://your_domain.api.crowdin.com +# config.organization_domain = your_domain.com -> your_domain.com -# Also you can specify project_id to handle it in methods - -# All Crowdin Client config options: +# All supported Crowdin Client config options now: crowdin = Crowdin::Client.new do |config| config.api_token = 'YourApiToken' # [String] required config.organization_domain = 'YourOrganizationDomain' # [String] optional config.project_id = 'YourProjectId' # [Integer] nil by default config.enable_logger = true # [Boolean] false by default end +# Note: Client will initialize default Logger instance if you have specify enable_logger to true, +# you can change it by crowdin.logger = YourLogger + +# Also you can specify proxy by adding it to ENV['http_proxy'] before Client initialization ``` To generate a new token in Crowdin, follow these steps: @@ -94,25 +100,25 @@ To generate a new token in Crowdin Enterprise, follow these steps: - Go to *Account Settings* > *Access tokens* tab and click *New token*. - Specify *Token Name*, select *Scopes* and *Projects*, click *Create*. -### How to call methods +### Usage ```ruby # Create Project project = crowdin.add_project(name: your_project_name, sourceLanguageId: your_language_id) # Get list of Projects -projects = client.list_projects - -# Get specified project -project = client.get_project(your_project_id) +projects = crowdin.list_projects # Get list of Projects with offset and limit -projects = client.list_projects(offset: 10, limit: 20) +projects = crowdin.list_projects(offset: 10, limit: 20) + +# Get specified project +project = crowdin.get_project(your_project_id) # Add Storage -adding_storage_response = crowdin.add_storage(File.open('YourFilename.extension')) +storage = crowdin.add_storage(File.open('YourFilename.extension', 'r')) # or you can specify only filename -adding_storage_response = crowdin.add_storage('YourFilename.extension') +storage = crowdin.add_storage('YourFilename.extension') # Download file filename = crowdin.download_file(your_destination, your_file_id, your_project_id) @@ -120,8 +126,10 @@ filename = crowdin.download_file(your_destination, your_file_id, your_project_id # project_id is optional, as it can be initialized with a Crowdin Client # File revisions -file_revisions = crowdin.list_file_revisions(your_file_id, { limit: 10, project_id: your_project_id }) -# project_id is optional, as it can be initialized with a Crowdin Client +# with initialized project_id in your Client +file_revisions = crowdin.list_file_revisions(your_file_id, limit: 10) +# or you can specify your project_id +file_revisions = crowdin.list_file_revisions(your_file_id, { limit: 10 }, your_project_id) ``` ### Command-Line Client @@ -129,14 +137,15 @@ file_revisions = crowdin.list_file_revisions(your_file_id, { limit: 10, project_ The Crowdin Ruby client support crowdin-console, where you can test endpoints easier ```console -bundle exec crowdin-console --enable-logger --api-token API_TOKEN --project-id PROJECT_ID +$ bundle exec crowdin-console --enable-logger --api-token API_TOKEN --project-id PROJECT_ID ``` Or Crowdin Enterprise ```console -bundle exec crowdin-console --enable-logger --enterprise --api-token API_TOKEN --organization-domain YOUR_DOMAIN +$ bundle exec crowdin-console --enable-logger --enterprise --api-token API_TOKEN --organization-domain YOUR_DOMAIN --project-id PROJECT_ID ``` +Note: you can specify full organization domain by adding .com When execute you'll have IRB console with configured *@crowdin* instance diff --git a/bin/crowdin-console b/bin/crowdin-console index 0f713ba..0020aff 100755 --- a/bin/crowdin-console +++ b/bin/crowdin-console @@ -1,20 +1,23 @@ #!/usr/bin/env ruby -require 'bundler/setup' require 'crowdin-api' require 'optparse' -# You can specify your arguments when execute this script # -# Type bundle exec crowdin-console --help for more available arguments +# You can specify your options while executing this script +# +# Type bundle exec crowdin-console --help for more available options # # Example: # # bundle exec crowdin-console --enable-logger --api-token YOUR_API_TOKEN --project-id YOUR_PROJECT_ID # -# or Enterpise API +# or Enterpise API: # # bundle exec crowdin-console --enterprise --api-token YOUR_ENTERPRISE_API_TOKEN --organization-domain YOUR_DOMAIN +# + +include Crowdin::Errors::ApiErrorsRaiser options = {} @@ -24,14 +27,14 @@ OptionParser.new do |opts| opts.on('--enterprise', 'Enterprise API') { |v| options[:enterprise] = v } opts.on('--api-token TOKEN', 'API Token') { |v| options[:api_token] = v } opts.on('--organization-domain DOMAIN', 'Organization Domain') { |v| options[:organization_domain] = v } - opts.on('--prohject-id ID', 'Project ID') { |v| options[:project_id] = v } + opts.on('--project-id ID', 'Project ID') { |v| options[:project_id] = v } opts.on('--enable-logger', 'Enable logger') { |v| options[:enable_logger] = v } end.parse! -options[:api_token] || raise(ArgumentError, '--api-token argument is required') +options[:api_token] || raise_api_token_is_required_error if options[:enterprise] - options[:organization_domain] || raise(ArgumentError, '--organization-domain option is required for Enterprise mode') + options[:organization_domain] || raise_organization_domain_is_required_error @crowdin = Crowdin::Client.new do |config| config.api_token = options[:api_token] diff --git a/crowdin-api.gemspec b/crowdin-api.gemspec index 20a6bc9..f352fce 100644 --- a/crowdin-api.gemspec +++ b/crowdin-api.gemspec @@ -18,10 +18,13 @@ Gem::Specification.new do |gem| gem.bindir = 'bin' gem.executables << 'crowdin-console' + gem.required_ruby_version = '>= 2.4' + gem.add_runtime_dependency 'open-uri', '>= 0.1.0', '< 0.2.0' gem.add_runtime_dependency 'rest-client', '>= 2.0.0', '< 2.1.0' gem.add_development_dependency 'bundler', '~> 2.2', '>= 2.2.32' + gem.add_development_dependency 'codecov', '~> 0.6.0' gem.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6' gem.add_development_dependency 'rspec', '~> 3.10' gem.add_development_dependency 'rubocop', '~> 1.23' diff --git a/lib/crowdin-api.rb b/lib/crowdin-api.rb index 7130b5b..4b1eb63 100644 --- a/lib/crowdin-api.rb +++ b/lib/crowdin-api.rb @@ -2,14 +2,13 @@ # Libs require 'json' -require 'logger' require 'open-uri' require 'rest-client' # Core modules -require 'crowdin-api/core/payload' -require 'crowdin-api/core/request' require 'crowdin-api/core/errors' +require 'crowdin-api/core/api_errors_raiser' +require 'crowdin-api/core/request' # Api modules require 'crowdin-api/api-resources/languages' @@ -18,6 +17,8 @@ require 'crowdin-api/api-resources/storages' require 'crowdin-api/api-resources/translation_status' require 'crowdin-api/api-resources/translations' +require 'crowdin-api/api-resources/workflows' +require 'crowdin-api/api-resources/source_strings' # Client require 'crowdin-api/client/version' diff --git a/lib/crowdin-api/api-resources/languages.rb b/lib/crowdin-api/api-resources/languages.rb index 4349e76..f910371 100644 --- a/lib/crowdin-api/api-resources/languages.rb +++ b/lib/crowdin-api/api-resources/languages.rb @@ -41,7 +41,7 @@ def add_custom_language(query = {}) end def get_language(language_id = nil) - language_id || raise(ArgumentError, ':language_id is required') + language_id || raise_parameter_is_required_error(:language_id) request = Web::Request.new( self, @@ -53,7 +53,7 @@ def get_language(language_id = nil) end def delete_custom_language(language_id = nil) - language_id || raise(ArgumentError, ':language_id is required') + language_id || raise_parameter_is_required_error(:language_id) request = Web::Request.new( self, @@ -65,7 +65,7 @@ def delete_custom_language(language_id = nil) end def edit_custom_language(language_id = nil) - language_id || raise(ArgumentError, ':language_id is required') + language_id || raise_parameter_is_required_error(:language_id) request = Web::Request.new( self, diff --git a/lib/crowdin-api/api-resources/projects.rb b/lib/crowdin-api/api-resources/projects.rb index 2a83812..e004192 100644 --- a/lib/crowdin-api/api-resources/projects.rb +++ b/lib/crowdin-api/api-resources/projects.rb @@ -26,7 +26,7 @@ def add_project(query = {}) end def get_project(project_id = nil) - project_id || raise(ArgumentError, ':project_id is required') + project_id || raise_parameter_is_required_error(:project_id) request = Web::Request.new( self, @@ -38,7 +38,7 @@ def get_project(project_id = nil) end def delete_project(project_id = nil) - project_id || raise(ArgumentError, ':project_id is required') + project_id || raise_parameter_is_required_error(:project_id) request = Web::Request.new( self, @@ -50,7 +50,7 @@ def delete_project(project_id = nil) end def edit_project(project_id = nil, query = {}) - project_id || raise(ArgumentError, ':project_id is required') + project_id || raise_parameter_is_required_error(:project_id) request = Web::Request.new( self, @@ -62,9 +62,11 @@ def edit_project(project_id = nil, query = {}) request.perform end - # For Enterprise API only + # For Enterprise mode only def list_groups(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + request = Web::Request.new( self, :get, @@ -76,6 +78,8 @@ def list_groups(query = {}) end def add_group(query = {}) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + request = Web::Request.new( self, :post, @@ -87,7 +91,8 @@ def add_group(query = {}) end def get_group(group_id = nil) - group_id || raise(ArgumentError, ':group_id is required') + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) request = Web::Request.new( self, @@ -99,7 +104,8 @@ def get_group(group_id = nil) end def delete_group(group_id = nil) - group_id || raise(ArgumentError, ':group_id is required') + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) request = Web::Request.new( self, @@ -111,7 +117,8 @@ def delete_group(group_id = nil) end def edit_group(group_id = nil, query = {}) - group_id || raise(ArgumentError, ':group_id is required') + config.enterprise_mode? || raise_only_for_enterprise_mode_error + group_id || raise_parameter_is_required_error(:group_id) request = Web::Request.new( self, diff --git a/lib/crowdin-api/api-resources/source_files.rb b/lib/crowdin-api/api-resources/source_files.rb index 7d42421..9618f54 100644 --- a/lib/crowdin-api/api-resources/source_files.rb +++ b/lib/crowdin-api/api-resources/source_files.rb @@ -4,7 +4,7 @@ module Crowdin module ApiResources module SourceFiles def list_branches(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -17,7 +17,7 @@ def list_branches(query = {}, project_id = config.project_id) end def add_branch(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -30,8 +30,8 @@ def add_branch(query = {}, project_id = config.project_id) end def get_branch(branch_id = nil, project_id = config.project_id) - branch_id || raise(ArgumentError, ':branch_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + branch_id || raise_parameter_is_required_error(:branch_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -43,8 +43,8 @@ def get_branch(branch_id = nil, project_id = config.project_id) end def delete_branch(branch_id = nil, project_id = config.project_id) - branch_id || raise(ArgumentError, ':branch_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + branch_id || raise_parameter_is_required_error(:branch_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -55,11 +55,9 @@ def delete_branch(branch_id = nil, project_id = config.project_id) request.perform end - def edit_branch(branch_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - branch_id || raise(ArgumentError, ':branch_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def edit_branch(branch_id = nil, query = {}, project_id = config.project_id) + branch_id || raise_parameter_is_required_error(:branch_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -72,7 +70,7 @@ def edit_branch(branch_id = nil, query = {}) end def list_directories(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -85,7 +83,7 @@ def list_directories(query = {}, project_id = config.project_id) end def add_directory(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -98,8 +96,8 @@ def add_directory(query = {}, project_id = config.project_id) end def get_directory(directory_id = nil, project_id = config.project_id) - directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + directory_id || raise_parameter_is_required_error(:directory_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -111,8 +109,8 @@ def get_directory(directory_id = nil, project_id = config.project_id) end def delete_directory(directory_id = nil, project_id = config.project_id) - directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + directory_id || raise_parameter_is_required_error(:directory_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -123,11 +121,9 @@ def delete_directory(directory_id = nil, project_id = config.project_id) request.perform end - def edit_directory(directory_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def edit_directory(directory_id = nil, query = {}, project_id = config.project_id) + directory_id || raise_parameter_is_required_error(:directory_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -140,7 +136,7 @@ def edit_directory(directory_id = nil, query = {}) end def list_files(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -166,12 +162,12 @@ def list_files(query = {}, project_id = config.project_id) # # crowdin.add_file(storage_id: your_storage_id, name: 'your_filename') # - # or you can use the specified project_id + # or you can specify project_id # - # crowdin.add_file({}, your_project_id) + # 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(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -184,8 +180,8 @@ def add_file(query = {}, project_id = config.project_id) end def get_file(file_id = nil, project_id = config.project_id) - file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + file_id || raise_parameter_is_required_error(:file_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -196,11 +192,9 @@ def get_file(file_id = nil, project_id = config.project_id) request.perform end - def update_or_restore_file(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def update_or_restore_file(file_id = nil, query = {}, project_id = config.project_id) + file_id || raise_parameter_is_required_error(:file_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -213,8 +207,8 @@ def update_or_restore_file(file_id = nil, query = {}) end def delete_file(file_id = nil, project_id = config.project_id) - file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + file_id || raise_parameter_is_required_error(:file_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -225,11 +219,9 @@ def delete_file(file_id = nil, project_id = config.project_id) request.perform end - def edit_file(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def edit_file(file_id = nil, query = {}, project_id = config.project_id) + file_id || raise_parameter_is_required_error(:file_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -242,9 +234,9 @@ def edit_file(file_id = nil, query = {}) end def download_file(destination = nil, file_id = nil, project_id = config.project_id) - destination || raise(ArgumentError, ':destination is required for downlaods') - file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + destination || raise_parameter_is_required_error(:destination) + file_id || raise_parameter_is_required_error(:file_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -273,17 +265,15 @@ def download_file(destination = nil, file_id = nil, project_id = config.project_ # # when you're initialized Crowdin Client with a project_id # - # crowdin.list_file_revisions(your_file_id, { limit: your_value }) + # crowdin.list_file_revisions(your_file_id, limit: your_value) # - # or you can use the specified project_id + # or you can specify project_id # - # crowdin.list_file_revisions(your_file_id, { project_id: your_project_id ..}) + # crowdin.list_file_revisions(your_file_id, { limit: your_value }, your_project_id) # - def list_file_revisions(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + 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, @@ -296,9 +286,9 @@ def list_file_revisions(file_id = nil, query = {}) end def get_file_revision(file_id = nil, revision_id = nil, project_id = config.project_id) - file_id || raise(ArgumentError, ':file_id is required') - revision_id || raise(ArgumentError, ':revision_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + file_id || raise_parameter_is_required_error(:file_id) + revision_id || raise_parameter_is_required_error(:revision_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, diff --git a/lib/crowdin-api/api-resources/source_strings.rb b/lib/crowdin-api/api-resources/source_strings.rb new file mode 100644 index 0000000..ce3f100 --- /dev/null +++ b/lib/crowdin-api/api-resources/source_strings.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module SourceStrings + def list_strings(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/strings", + query + ) + + request.perform + end + + def add_string(query = {}, project_id = config.project_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :post, + "/projects/#{project_id}/strings", + query + ) + + request.perform + end + + def get_string(string_id = nil, query = {}, project_id = config.project_id) + string_id || raise_parameter_is_required_error(:string_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/strings/#{string_id}", + query + ) + + request.perform + end + + def delete_string(string_id = nil, project_id = config.project_id) + string_id || raise_parameter_is_required_error(:string_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :delete, + "/projects/#{project_id}/strings/#{string_id}" + ) + + request.perform + end + + def edit_string(string_id = nil, query = {}, project_id = config.project_id) + string_id || raise_parameter_is_required_error(:string_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :patch, + "/projects/#{project_id}/strings/#{string_id}", + query + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/api-resources/storages.rb b/lib/crowdin-api/api-resources/storages.rb index d00b7ee..e9840c4 100644 --- a/lib/crowdin-api/api-resources/storages.rb +++ b/lib/crowdin-api/api-resources/storages.rb @@ -34,14 +34,14 @@ def list_storages(query = {}) # # === Example # - # crowdin.add_storage(File.open('your_filename.extension')) + # crowdin.add_storage(File.open('your_filename.extension', 'r')) # or # crowdin.add_storage('your_filename.extension') # def add_storage(file = nil) - file || raise(ArgumentError, ':file is required') + file || raise_parameter_is_required_error(:file) - file = file.is_a?(File) ? file : File.open(file) + file = file.is_a?(File) ? file : File.open(file, 'r') request = Web::Request.new( self, @@ -65,7 +65,7 @@ def add_storage(file = nil) # crowdin.get_storage(your_storage_id) # def get_storage(storage_id = nil) - storage_id || raise(ArgumentError, ':storage_id is required') + storage_id || raise_parameter_is_required_error(:storage_id) request = Web::Request.new( self, @@ -87,7 +87,7 @@ def get_storage(storage_id = nil) # crowdin.delete_storage(your_storage_id) # def delete_storage(storage_id = nil) - storage_id || raise(ArgumentError, ':storage_id is required') + storage_id || raise_parameter_is_required_error(:storage_id) request = Web::Request.new( self, diff --git a/lib/crowdin-api/api-resources/translation_status.rb b/lib/crowdin-api/api-resources/translation_status.rb index 26b2fef..05264c0 100644 --- a/lib/crowdin-api/api-resources/translation_status.rb +++ b/lib/crowdin-api/api-resources/translation_status.rb @@ -3,11 +3,9 @@ module Crowdin module ApiResources module TranslationStatus - def get_branch_progress(branch_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - branch_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def get_branch_progress(branch_id = nil, query = {}, project_id = config.project_id) + branch_id || raise_parameter_is_required_error(:branch_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -19,11 +17,9 @@ def get_branch_progress(branch_id = nil, query = {}) request.perform end - def get_directory_progress(directory_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def get_directory_progress(directory_id = nil, query = {}, project_id = config.project_id) + directory_id || raise_parameter_is_required_error(:directory_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -35,11 +31,9 @@ def get_directory_progress(directory_id = nil, query = {}) request.perform end - def get_file_progress(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def get_file_progress(file_id = nil, query = {}, project_id = config.project_id) + file_id || raise_parameter_is_required_error(:file_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -51,11 +45,9 @@ def get_file_progress(file_id = nil, query = {}) request.perform end - def get_language_progress(language_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - language_id || raise(ArgumentError, ':language_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def get_language_progress(language_id = nil, query = {}, project_id = config.project_id) + language_id || raise_parameter_is_required_error(:language_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -68,7 +60,7 @@ def get_language_progress(language_id = nil, query = {}) end def get_project_progress(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -81,7 +73,7 @@ def get_project_progress(query = {}, project_id = config.project_id) end def get_qa_progress(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, diff --git a/lib/crowdin-api/api-resources/translations.rb b/lib/crowdin-api/api-resources/translations.rb index a68f188..0dcfd8b 100644 --- a/lib/crowdin-api/api-resources/translations.rb +++ b/lib/crowdin-api/api-resources/translations.rb @@ -4,8 +4,8 @@ module Crowdin module ApiResources module Translations def pre_translation_status(pre_translation_id = nil, project_id = config.project_id) - pre_translation_id || raise(ArgumentError, ':pre_translation_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + pre_translation_id || raise_parameter_is_required_error(:pre_translation_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -17,7 +17,7 @@ def pre_translation_status(pre_translation_id = nil, project_id = config.project end def apply_pre_translation(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -29,11 +29,9 @@ def apply_pre_translation(query = {}, project_id = config.project_id) request.perform end - def build_project_directory_translation(directory_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - directory_id || raise(ArgumentError, ':directory_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def build_project_directory_translation(directory_id = nil, query = {}, project_id = config.project_id) + directory_id || raise_parameter_is_required_error(:directory_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -45,11 +43,9 @@ def build_project_directory_translation(directory_id = nil, query = {}) request.perform end - def build_project_file_translation(file_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - file_id || raise(ArgumentError, ':file_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def build_project_file_translation(file_id = nil, query = {}, project_id = config.project_id) + file_id || raise_parameter_is_required_error(:file_id) + project_id || raise_project_id_is_required_error headers = query[:eTag] ? { 'If-None-Match' => query[:eTag] } : {} @@ -65,7 +61,7 @@ def build_project_file_translation(file_id = nil, query = {}) end def list_project_builds(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -78,7 +74,7 @@ def list_project_builds(query = {}, project_id = config.project_id) end def build_project_translation(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -90,11 +86,9 @@ def build_project_translation(query = {}, project_id = config.project_id) request.perform end - def upload_translations(language_id = nil, query = {}) - project_id = query[:project_id] || config.project_id - - language_id || raise(ArgumentError, ':language_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def upload_translations(language_id = nil, query = {}, project_id = config.project_id) + language_id || raise_parameter_is_required_error(:language_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -106,10 +100,10 @@ def upload_translations(language_id = nil, query = {}) request.perform end - def download_project_translations(destinaton = nil, build_id = nil, project_id = config.project_id) - destinaton || raise(ArgumentError, ':destination is required for downlaods') - build_id || raise(ArgumentError, ':build_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + def download_project_translations(destination = nil, build_id = nil, project_id = config.project_id) + destination || raise_parameter_is_required_error(:destination) + build_id || raise_parameter_is_required_error(:build_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -117,15 +111,15 @@ def download_project_translations(destinaton = nil, build_id = nil, project_id = "/projects/#{project_id}/translations/builds/#{build_id}/download", {}, {}, - destinaton + destination ) request.perform end def check_project_build_status(build_id = nil, project_id = config.project_id) - build_id || raise(ArgumentError, ':build_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + build_id || raise_parameter_is_required_error(:build_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -137,8 +131,8 @@ def check_project_build_status(build_id = nil, project_id = config.project_id) end def cancel_build(build_id = nil, project_id = config.project_id) - build_id || raise(ArgumentError, ':build_id is required') - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + build_id || raise_parameter_is_required_error(:build_id) + project_id || raise_project_id_is_required_error request = Web::Request.new( self, @@ -150,7 +144,7 @@ def cancel_build(build_id = nil, project_id = config.project_id) end def export_project_translation(query = {}, project_id = config.project_id) - project_id || raise(ArgumentError, ':project_id is required in parameters or when initialize Client') + project_id || raise_project_id_is_required_error request = Web::Request.new( self, diff --git a/lib/crowdin-api/api-resources/workflows.rb b/lib/crowdin-api/api-resources/workflows.rb new file mode 100644 index 0000000..e13ef55 --- /dev/null +++ b/lib/crowdin-api/api-resources/workflows.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module Crowdin + module ApiResources + module Workflows + def list_workflow_steps(query = {}, project_id = config.project_id) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/workflow-steps", + query + ) + + request.perform + end + + def get_workflow_step(step_id = nil, project_id = config.project_id) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + step_id || raise_parameter_is_required_error(:step_id) + project_id || raise_project_id_is_required_error + + request = Web::Request.new( + self, + :get, + "/projects/#{project_id}/workflow-steps/#{step_id}" + ) + + request.perform + end + + def list_workflow_templates(query = {}) + request = Web::Request.new( + self, + :get, + '/workflow-templates', + query + ) + + request.perform + end + + def get_workflow_template(template_id = nil) + config.enterprise_mode? || raise_only_for_enterprise_mode_error + template_id || raise_parameter_is_required_error(:template_id) + + request = Web::Request.new( + self, + :get, + "/workflow-templates/#{template_id}" + ) + + request.perform + end + end + end +end diff --git a/lib/crowdin-api/client/client.rb b/lib/crowdin-api/client/client.rb index 17ca3a6..090e652 100644 --- a/lib/crowdin-api/client/client.rb +++ b/lib/crowdin-api/client/client.rb @@ -1,14 +1,17 @@ # frozen_string_literal: true +# # The Crowdin::Client library is used for interactions with a crowdin.com website. # # == Example # -# require 'crowdin-api' +# require 'crowdin-api' +# +# crowdin = Crowdin::Client.new do |config| +# config.api_token = 'YOUR_API_TOKEN' +# end # -# crowdin = Crowdin::Client.new do |config| -# config.api_token = 'YOUR_API_TOKEN' -# end +# crowdin.list_projects # module Crowdin class Client @@ -24,59 +27,56 @@ class Client include ApiResources::Storages include ApiResources::TranslationStatus include ApiResources::Translations + include ApiResources::Workflows + include ApiResources::SourceStrings + + include Errors::ApiErrorsRaiser + + attr_accessor :logger attr_reader :config - attr_reader :options attr_reader :connection + attr_reader :options - def initialize - raise ArgumentError, 'block with configurations not given' unless block_given? - - @config = Crowdin::Configuration.new - yield config + def initialize(&block) + build_configuration(&block) check_logger + check_rest_client_proxy - set_rest_client_proxy! - - build_options build_connection end def log!(message) - return true unless config.logger_enabled? - - logger.debug(message) + !config.logger_enabled? || logger.debug(message) end - def logger=(logger) - @logger = logger - config.enable_logger = true + private + + def build_configuration + @config = Crowdin::Configuration.new + yield config if block_given? end - protected + def build_connection + @connection ||= ::RestClient::Resource.new(config.base_url, build_options) + end def build_options - @options = config.options - options[:headers] = config.headers + @options ||= config.options.merge(headers: config.headers) end - def build_connection - @connection = ::RestClient::Resource.new(config.base_url, options) + def set_default_logger + require 'logger' + @logger ||= Logger.new($stderr) end - private - - def set_rest_client_proxy! + def check_rest_client_proxy ENV['http_proxy'] ? ::RestClient.proxy = ENV['http_proxy'] : false end def check_logger - config.enable_logger ||= false - end - - def logger - @logger ||= Logger.new($stderr) + 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 44b46b6..064b0b7 100644 --- a/lib/crowdin-api/client/configuration.rb +++ b/lib/crowdin-api/client/configuration.rb @@ -9,6 +9,9 @@ class Configuration attr_reader :target_api_url + alias logger_enabled? enable_logger + alias enterprise_mode? organization_domain + def initialize @target_api_url = '/api/v2' end @@ -31,11 +34,11 @@ def headers end def base_url - organization_domain ? "https://#{organization_domain}.api.crowdin.com" : 'https://api.crowdin.com' - end - - def logger_enabled? - enable_logger + if enterprise_mode? + organization_domain.include?('.com') ? organization_domain : "https://#{organization_domain}.api.crowdin.com" + else + 'https://api.crowdin.com' + end end end end diff --git a/lib/crowdin-api/client/version.rb b/lib/crowdin-api/client/version.rb index bd5d830..331aab9 100644 --- a/lib/crowdin-api/client/version.rb +++ b/lib/crowdin-api/client/version.rb @@ -2,6 +2,6 @@ module Crowdin class Client - VERSION = '1.0.0' + VERSION = '1.1.0' end end diff --git a/lib/crowdin-api/core/api_errors_raiser.rb b/lib/crowdin-api/core/api_errors_raiser.rb new file mode 100644 index 0000000..9deec6d --- /dev/null +++ b/lib/crowdin-api/core/api_errors_raiser.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Crowdin + module Errors + module ApiErrorsRaiser + def raise_only_for_enterprise_mode_error + raise(OnlyForEnterpriseMode, 'This method can be called only for Enterprise mode') + end + + def raise_project_id_is_required_error + raise(ArgumentError, ':project_id is required in parameters or while Client initialization') + end + + def raise_parameter_is_required_error(parameter) + raise(ArgumentError, ":#{parameter} is required") + end + + # crowdin-console errors + + def raise_api_token_is_required_error + raise(ArgumentError, '--api-token option is required') + end + + def raise_organization_domain_is_required_error + raise(ArgumentError, '--organization-domain option is required for Enterprise mode') + end + end + end +end diff --git a/lib/crowdin-api/core/errors.rb b/lib/crowdin-api/core/errors.rb index d795f66..3aa75c5 100644 --- a/lib/crowdin-api/core/errors.rb +++ b/lib/crowdin-api/core/errors.rb @@ -2,19 +2,6 @@ module Crowdin module Errors - class Error < StandardError - attr_reader :key, :error_code, :error_message - - def initialize(key, error_code, error_message) - @key = key - @error_code = error_code.to_i - @error_message = error_message - @message = "#{key} => #{error_code}: #{error_message}" - end - - def to_s - @message - end - end + class OnlyForEnterpriseMode < StandardError; end end end diff --git a/lib/crowdin-api/core/payload.rb b/lib/crowdin-api/core/payload.rb deleted file mode 100644 index e32acac..0000000 --- a/lib/crowdin-api/core/payload.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -module Crowdin - module Web - class Payload - attr_reader :method, :query - - def initialize(method, query) - @method = method - @query = query - end - - def perform - return @query if @query.is_a?(File) - - @method.eql?(:get) ? { params: fetch_cleared_query } : fetch_cleared_query.to_json - end - - private - - def fetch_cleared_query - @query.reject { |_, value| value.nil? } - end - end - end -end diff --git a/lib/crowdin-api/core/request.rb b/lib/crowdin-api/core/request.rb index ff9123e..4da79e1 100644 --- a/lib/crowdin-api/core/request.rb +++ b/lib/crowdin-api/core/request.rb @@ -9,7 +9,7 @@ def initialize(client, method, path, query = {}, headers = {}, destination = nil @client = client @method = method @full_path = client.config.target_api_url + path - @payload = Payload.new(method, query).perform + @payload = perform_payload(query) @headers = headers @destination = destination @errors = [] @@ -20,13 +20,15 @@ def perform process_response! end + private + def process_request! return @response = client.connection[@full_path].delete if delete_request? return @response = client.connection[@full_path].get(@payload) if get_request? client.connection[@full_path].send(@method, @payload, @headers) { |response, _, _| @response = response } rescue StandardError => error - client.log! error.class + client.log! error @errors << "Something went wrong while proccessing request. Details - #{error.class}" end @@ -36,14 +38,19 @@ def process_response! begin if @response - doc = JSON.parse(@response.body) - client.log! "args: #{@response.request.args}" - client.log! "body: #{doc}" - data = fetch_response_data(doc) + if @response.body.empty? + @response.code + else + doc = JSON.parse(@response.body) + + client.log! "body: #{doc}" - @errors.any? ? fetch_errors : data + data = fetch_response_data(doc) + + @errors.any? ? fetch_errors : data + end end rescue StandardError => error client.log! error @@ -54,10 +61,10 @@ def process_response! end end - private + def perform_payload(query) + return query if query.is_a?(File) - def fetch_errors - @errors.join(';') + get_request? ? { params: fetch_cleared_query(query) } : fetch_cleared_query(query).to_json end def download_file(url) @@ -71,14 +78,31 @@ def download_file(url) @errors << "Something went wrong while downloading file. Details - #{error.class}" end + def fetch_errors + @errors.join(';') + end + def fetch_response_data(doc) - if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].scan(/response-content-disposition/) + if doc['data'].is_a?(Hash) && doc['data']['url'] && doc['data']['url'].include?('response-content-disposition') download_file(doc['data']['url']) else doc end end + def fetch_cleared_query(query) + 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 + end + end + def get_request? @method.eql?(:get) end diff --git a/spec/core/config-instance_spec.rb b/spec/core/config-instance_spec.rb index 98ed008..7719f87 100644 --- a/spec/core/config-instance_spec.rb +++ b/spec/core/config-instance_spec.rb @@ -1,35 +1,72 @@ # frozen_string_literal: true describe 'Config instance' do - it 'should have a project_id' do + before do @crowdin = Crowdin::Client.new do |config| + config.api_token = 'api_token' config.project_id = 1 end + end + it 'show have a #project_id' do expect(@crowdin.config.project_id).to_not be_nil end - it 'should have a api_token' do - @crowdin = Crowdin::Client.new do |config| - config.api_token = 'api_token' + 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 be false by default' do + expect(@crowdin.config.logger_enabled?).to be_falsey end - expect(@crowdin.config.api_token).to_not be_nil + 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 - it 'should have a base_url' do - @crowdin = Crowdin::Client.new do |config| - config.api_token = 'api_token' + describe '#enterprise_mode?' do + it 'should be false by default' do + expect(@crowdin.config.enterprise_mode?).to be_falsey end - expect(@crowdin.config.base_url).to_not be_nil + it 'should equal specified arguments' do + @crowdin = Crowdin::Client.new do |config| + config.organization_domain = 'organization_domain' + end + + expect(@crowdin.config.enterprise_mode?).to be_truthy + end end - it 'should have a enable_logger set to false by default' do - @crowdin = Crowdin::Client.new do |config| - config.api_token = 'api_token' + describe '#base_url' do + it 'should equal https://api.crowdin.com by default' do + expect(@crowdin.config.base_url).to eq('https://api.crowdin.com') end - expect(@crowdin.config.enable_logger).eql? false + 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 aa77b54..294cbbc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,9 @@ # frozen_string_literal: true +require 'simplecov' +SimpleCov.start + +require 'codecov' +SimpleCov.formatter = SimpleCov::Formatter::Codecov + require 'crowdin-api'