Skip to content

Commit

Permalink
Merge pull request #14 from crowdin/client-updates
Browse files Browse the repository at this point in the history
Release '1.1.0'
  • Loading branch information
andrii-bodnar authored Dec 14, 2021
2 parents 115608b + 199ff59 commit fdd6706
Show file tree
Hide file tree
Showing 23 changed files with 484 additions and 268 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
47 changes: 34 additions & 13 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -116,3 +132,8 @@ Style/Documentation:
Style/FrozenStringLiteralComment:
Exclude:
- 'bin/crowdin-console'

# Offense count: 1
Style/MixinUsage:
Exclude:
- 'bin/crowdin-console'
47 changes: 28 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -94,49 +100,52 @@ 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)
# your_destination - filename or full path to file
# 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

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

Expand Down
17 changes: 10 additions & 7 deletions bin/crowdin-console
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions crowdin-api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 4 additions & 3 deletions lib/crowdin-api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions lib/crowdin-api/api-resources/languages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Loading

0 comments on commit fdd6706

Please sign in to comment.