translator-text 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2a23c9076d2e91225a46124d809eb0acf0b3513b
4
- data.tar.gz: ef82b1270172a0ef04f5b83b5e15317b5de61383
2
+ SHA256:
3
+ metadata.gz: 8632db69acab569b667be95a710ddfb4063475a820160008248099d36d0b42fc
4
+ data.tar.gz: d0bc41f634efd36ed604f439036188e70f3a1952d79952f650449a448c0cb535
5
5
  SHA512:
6
- metadata.gz: 4552b5d7a357a5f013a0a3a36aaf9ee9d63b54a42ce3f92451ddc312e6a8a52ed239250f4289c2070b346c11a1fe43e747fe5887c08e43113352e51ad7417d6a
7
- data.tar.gz: 44ff8cb26e0d4ff5f99a9f52276847cc7fbd7e31c40cb1e6a604f996227e607d8b385c0f0bd2948d90c0be5455d72e8a24d13cf9d8478ca343d03a0139e160ae
6
+ metadata.gz: 2286a74057def86c7fae4ed2f6e8e9b4ebb15e3d23a38a6dbf74533ac6e079fc023764270cb8f3930e2803d8d17e4b9505a7396d3c02ac78c6a843f2241e759b
7
+ data.tar.gz: 5edd026d712c585b93e170644424f73da279c1dad6314250a99a153a4fcca9d1ea2c7216c02bed6f068d7c6864400c4ad164035586d46ad47d79efd4682259e0
data/.rubocop.yml CHANGED
@@ -1,3 +1,7 @@
1
+ ---
2
+ require:
3
+ - rubocop-rspec
4
+
1
5
  LineLength:
2
6
  Max: 130
3
7
 
@@ -8,7 +12,8 @@ Metrics/ParameterLists:
8
12
  CountKeywordArgs: false
9
13
 
10
14
  AllCops:
11
- TargetRubyVersion: 2.2
15
+ TargetRubyVersion: 3.1
16
+ NewCops: enable
12
17
 
13
18
  EndAlignment:
14
19
  EnforcedStyleAlignWith: variable
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.4.4
5
- before_install: gem install bundler -v 1.16.2
4
+ - 3.1.4
5
+ before_install: gem install bundler -v 2.3.26
data/CHANGELOG.md CHANGED
@@ -4,7 +4,20 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
- ## 0.1.0 (2018-08-28)
7
+ ## [0.3.0] - 2023-11-15
8
+ ### Changed
9
+ - Add support of the COGNITIVE_SUBSCRIPTION_REGION
10
+ - Target ruby 3.1+
11
+
12
+ ### Fixed
13
+ - Upgrade Client array queries normalization
14
+
15
+ ## [0.2.0] - 2023-04-19
16
+ ### Changed
17
+ - Use dry-struct 1+
18
+ - Switch to ruby 3.1 as default ruby version
19
+
20
+ ## [0.1.0] - 2018-08-28
8
21
  ### Added
9
22
  - Initial release
10
23
  - Support of <translation> service
data/Gemfile CHANGED
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # git_source(:github) {|repo_name| 'https://github.com/#{repo_name}' }
4
6
 
5
- group :test do
6
- gem 'bundler', '~> 1.16'
7
+ group :test, :development do
7
8
  gem 'dotenv'
8
- gem 'rake', '~> 10.0'
9
- gem 'rspec', '~> 3.0'
9
+ gem 'rake', '~> 12'
10
+ gem 'rspec', '~> 3'
10
11
  gem 'rubocop'
12
+ gem 'rubocop-rspec'
11
13
  gem 'simplecov', require: false
12
14
  gem 'webmock'
13
15
  gem 'yard'
data/Gemfile.lock CHANGED
@@ -1,109 +1,122 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- translator-text (0.1.0)
5
- dry-struct (~> 0.5.0)
6
- httparty (~> 0.15)
4
+ translator-text (0.3.0)
5
+ dry-struct (~> 1)
6
+ httparty (~> 0.21)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- addressable (2.5.2)
12
- public_suffix (>= 2.0.2, < 4.0)
13
- ast (2.4.0)
14
- concurrent-ruby (1.0.5)
15
- crack (0.4.3)
16
- safe_yaml (~> 1.0.0)
17
- diff-lcs (1.3)
18
- docile (1.3.1)
19
- dotenv (2.5.0)
20
- dry-configurable (0.7.0)
11
+ addressable (2.8.5)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ concurrent-ruby (1.2.2)
15
+ crack (0.4.5)
16
+ rexml
17
+ diff-lcs (1.5.0)
18
+ docile (1.4.0)
19
+ dotenv (2.8.1)
20
+ dry-core (1.0.1)
21
21
  concurrent-ruby (~> 1.0)
22
- dry-container (0.6.0)
22
+ zeitwerk (~> 2.6)
23
+ dry-inflector (1.0.0)
24
+ dry-logic (1.5.0)
23
25
  concurrent-ruby (~> 1.0)
24
- dry-configurable (~> 0.1, >= 0.1.3)
25
- dry-core (0.4.7)
26
- concurrent-ruby (~> 1.0)
27
- dry-equalizer (0.2.1)
28
- dry-inflector (0.1.2)
29
- dry-logic (0.4.2)
30
- dry-container (~> 0.2, >= 0.2.6)
31
- dry-core (~> 0.2)
32
- dry-equalizer (~> 0.2)
33
- dry-struct (0.5.1)
34
- dry-core (~> 0.4, >= 0.4.3)
35
- dry-equalizer (~> 0.2)
36
- dry-types (~> 0.13)
26
+ dry-core (~> 1.0, < 2)
27
+ zeitwerk (~> 2.6)
28
+ dry-struct (1.6.0)
29
+ dry-core (~> 1.0, < 2)
30
+ dry-types (>= 1.7, < 2)
37
31
  ice_nine (~> 0.11)
38
- dry-types (0.13.2)
32
+ zeitwerk (~> 2.6)
33
+ dry-types (1.7.1)
39
34
  concurrent-ruby (~> 1.0)
40
- dry-container (~> 0.3)
41
- dry-core (~> 0.4, >= 0.4.4)
42
- dry-equalizer (~> 0.2)
43
- dry-inflector (~> 0.1, >= 0.1.2)
44
- dry-logic (~> 0.4, >= 0.4.2)
45
- hashdiff (0.3.7)
46
- httparty (0.16.2)
35
+ dry-core (~> 1.0)
36
+ dry-inflector (~> 1.0)
37
+ dry-logic (~> 1.4)
38
+ zeitwerk (~> 2.6)
39
+ hashdiff (1.0.1)
40
+ httparty (0.21.0)
41
+ mini_mime (>= 1.0.0)
47
42
  multi_xml (>= 0.5.2)
48
43
  ice_nine (0.11.2)
49
- jaro_winkler (1.5.1)
50
- json (2.1.0)
44
+ json (2.6.3)
45
+ language_server-protocol (3.17.0.3)
46
+ mini_mime (1.1.5)
51
47
  multi_xml (0.6.0)
52
- parallel (1.12.1)
53
- parser (2.5.1.2)
54
- ast (~> 2.4.0)
55
- powerpack (0.1.2)
56
- public_suffix (3.0.3)
57
- rainbow (3.0.0)
58
- rake (10.5.0)
59
- rspec (3.8.0)
60
- rspec-core (~> 3.8.0)
61
- rspec-expectations (~> 3.8.0)
62
- rspec-mocks (~> 3.8.0)
63
- rspec-core (3.8.0)
64
- rspec-support (~> 3.8.0)
65
- rspec-expectations (3.8.1)
48
+ parallel (1.23.0)
49
+ parser (3.2.2.4)
50
+ ast (~> 2.4.1)
51
+ racc
52
+ public_suffix (5.0.3)
53
+ racc (1.7.3)
54
+ rainbow (3.1.1)
55
+ rake (12.3.3)
56
+ regexp_parser (2.8.2)
57
+ rexml (3.2.6)
58
+ rspec (3.12.0)
59
+ rspec-core (~> 3.12.0)
60
+ rspec-expectations (~> 3.12.0)
61
+ rspec-mocks (~> 3.12.0)
62
+ rspec-core (3.12.2)
63
+ rspec-support (~> 3.12.0)
64
+ rspec-expectations (3.12.3)
66
65
  diff-lcs (>= 1.2.0, < 2.0)
67
- rspec-support (~> 3.8.0)
68
- rspec-mocks (3.8.0)
66
+ rspec-support (~> 3.12.0)
67
+ rspec-mocks (3.12.6)
69
68
  diff-lcs (>= 1.2.0, < 2.0)
70
- rspec-support (~> 3.8.0)
71
- rspec-support (3.8.0)
72
- rubocop (0.58.2)
73
- jaro_winkler (~> 1.5.1)
69
+ rspec-support (~> 3.12.0)
70
+ rspec-support (3.12.1)
71
+ rubocop (1.57.2)
72
+ json (~> 2.3)
73
+ language_server-protocol (>= 3.17.0)
74
74
  parallel (~> 1.10)
75
- parser (>= 2.5, != 2.5.1.1)
76
- powerpack (~> 0.1)
75
+ parser (>= 3.2.2.4)
77
76
  rainbow (>= 2.2.2, < 4.0)
77
+ regexp_parser (>= 1.8, < 3.0)
78
+ rexml (>= 3.2.5, < 4.0)
79
+ rubocop-ast (>= 1.28.1, < 2.0)
78
80
  ruby-progressbar (~> 1.7)
79
- unicode-display_width (~> 1.0, >= 1.0.1)
80
- ruby-progressbar (1.10.0)
81
- safe_yaml (1.0.4)
82
- simplecov (0.16.1)
81
+ unicode-display_width (>= 2.4.0, < 3.0)
82
+ rubocop-ast (1.30.0)
83
+ parser (>= 3.2.1.0)
84
+ rubocop-capybara (2.19.0)
85
+ rubocop (~> 1.41)
86
+ rubocop-factory_bot (2.24.0)
87
+ rubocop (~> 1.33)
88
+ rubocop-rspec (2.25.0)
89
+ rubocop (~> 1.40)
90
+ rubocop-capybara (~> 2.17)
91
+ rubocop-factory_bot (~> 2.22)
92
+ ruby-progressbar (1.13.0)
93
+ simplecov (0.22.0)
83
94
  docile (~> 1.1)
84
- json (>= 1.8, < 3)
85
- simplecov-html (~> 0.10.0)
86
- simplecov-html (0.10.2)
87
- unicode-display_width (1.4.0)
88
- webmock (3.4.2)
89
- addressable (>= 2.3.6)
95
+ simplecov-html (~> 0.11)
96
+ simplecov_json_formatter (~> 0.1)
97
+ simplecov-html (0.12.3)
98
+ simplecov_json_formatter (0.1.4)
99
+ unicode-display_width (2.5.0)
100
+ webmock (3.19.1)
101
+ addressable (>= 2.8.0)
90
102
  crack (>= 0.3.2)
91
- hashdiff
92
- yard (0.9.16)
103
+ hashdiff (>= 0.4.0, < 2.0.0)
104
+ yard (0.9.34)
105
+ zeitwerk (2.6.12)
93
106
 
94
107
  PLATFORMS
95
108
  ruby
96
109
 
97
110
  DEPENDENCIES
98
- bundler (~> 1.16)
99
111
  dotenv
100
- rake (~> 10.0)
101
- rspec (~> 3.0)
112
+ rake (~> 12)
113
+ rspec (~> 3)
102
114
  rubocop
115
+ rubocop-rspec
103
116
  simplecov
104
117
  translator-text!
105
118
  webmock
106
119
  yard
107
120
 
108
121
  BUNDLED WITH
109
- 1.16.2
122
+ 2.3.26
data/README.md CHANGED
@@ -29,15 +29,34 @@ Or install it yourself as:
29
29
 
30
30
  ## Usage
31
31
 
32
- ### Translation
32
+ ### Client
33
+
34
+ You can instanciate your own client by passing the [Azure authentication](https://learn.microsoft.com/en-us/azure/ai-services/translator/reference/v3-0-reference#authentication) parameters.
33
35
 
34
36
  ```ruby
35
37
  require 'translator-text'
36
38
 
37
- api_key = ENV.fetch('COGNITIVE_SUBSCRIPTION_KEY')
38
- client = TranslatorText::Client.new(api_key)
39
+ client = TranslatorText::Client.new(api_key, api_region)
40
+ ```
41
+
42
+ Or use the built-in that uses the following environment variables:
43
+
44
+ * `COGNITIVE_SUBSCRIPTION_KEY`: The value is the Azure secret key for your subscription to Translator.
45
+ * `COGNITIVE_SUBSCRIPTION_REGION`: The value is the region of the multi-service or regional translator resource. This value is optional when using a global translator resource.
39
46
 
40
- results = client.translate(
47
+ ```ruby
48
+ require 'translator-text'
49
+
50
+ client = TranslatorText.client
51
+ ```
52
+
53
+
54
+ ### Translation
55
+
56
+ ```ruby
57
+ require 'translator-text'
58
+
59
+ results = TranslatorText.client.translate(
41
60
  ['something to translate', 'another something to translate'],
42
61
  to: [:fr, :es]
43
62
  )
@@ -58,10 +77,7 @@ results = client.translate(
58
77
  ```ruby
59
78
  require 'translator-text'
60
79
 
61
- api_key = ENV.fetch('COGNITIVE_SUBSCRIPTION_KEY')
62
- client = TranslatorText::Client.new(api_key)
63
-
64
- results = client.detect(['some unknown language', 'quelque chose à détecter'])
80
+ results = TranslatorText.client.detect(['some unknown language', 'quelque chose à détecter'])
65
81
 
66
82
  # Returns
67
83
  # [
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
  require 'yard'
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bundler/setup'
4
5
  require 'translator-text'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'translator_text/version'
2
4
  require_relative 'translator_text/exceptions'
3
5
  require_relative 'translator_text/client'
@@ -14,7 +16,10 @@ module TranslatorText
14
16
  private
15
17
 
16
18
  def build_client
17
- TranslatorText::Client.new(ENV.fetch('COGNITIVE_SUBSCRIPTION_KEY'))
19
+ TranslatorText::Client.new(
20
+ ENV.fetch('COGNITIVE_SUBSCRIPTION_KEY'),
21
+ ENV.fetch('COGNITIVE_SUBSCRIPTION_REGION', nil)
22
+ )
18
23
  end
19
24
  end
20
25
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'securerandom'
3
5
  require 'httparty'
@@ -7,15 +9,23 @@ module TranslatorText
7
9
  include HTTParty
8
10
  format :json
9
11
 
10
- base_uri 'https://api.cognitive.microsofttranslator.com'.freeze
11
- API_VERSION = '3.0'.freeze
12
+ base_uri 'https://api.cognitive.microsofttranslator.com'
13
+ API_VERSION = '3.0'
14
+
15
+ query_string_normalizer proc { |query|
16
+ query.map do |key, value|
17
+ Array(value).map { |v| "#{key}=#{v}" }
18
+ end.join('&')
19
+ }
12
20
 
13
21
  # Initialize the client
14
22
  # @since 1.0.0
15
23
  #
16
24
  # @param api_key [String] the Cognitive Services API Key
17
- def initialize(api_key)
25
+ # @param api_region [String] the Cognitive Services API Region
26
+ def initialize(api_key, api_region = nil)
18
27
  @api_key = api_key
28
+ @api_region = api_region
19
29
  end
20
30
 
21
31
  # Translate a group of sentences.
@@ -33,7 +43,7 @@ module TranslatorText
33
43
  results = post(
34
44
  '/translate',
35
45
  body: build_sentences(sentences).to_json,
36
- query: Hash[to: to, **options]
46
+ query: { to:, **options }
37
47
  )
38
48
 
39
49
  results.map { |r| Types::TranslationResult.new(r) }
@@ -68,7 +78,7 @@ module TranslatorText
68
78
 
69
79
  def post(path, params)
70
80
  options = {
71
- headers: headers,
81
+ headers:,
72
82
  query: {}
73
83
  }.merge(params)
74
84
 
@@ -104,9 +114,10 @@ module TranslatorText
104
114
  def headers
105
115
  {
106
116
  'Ocp-Apim-Subscription-Key' => @api_key,
117
+ 'Ocp-Apim-Subscription-Region' => @api_region,
107
118
  'X-ClientTraceId' => SecureRandom.uuid,
108
119
  'Content-type' => 'application/json'
109
- }
120
+ }.compact
110
121
  end
111
122
  end
112
123
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TranslatorText
2
4
  class Error < StandardError
3
5
  attr_reader :code, :message, :errors
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TranslatorText
2
4
  module Types
3
5
  class Alternative < Dry::Struct
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TranslatorText
2
4
  module Types
3
5
  class DetectionResult < Dry::Struct
@@ -9,8 +11,6 @@ module TranslatorText
9
11
  attribute :score, Types::Float
10
12
  attribute :isTranslationSupported, Types::Bool
11
13
  attribute :isTransliterationSupported, Types::Bool
12
-
13
- attribute :alternatives, Types::Array.of(Alternative)
14
14
  end
15
15
  end
16
16
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TranslatorText
2
4
  module Types
3
5
  class Sentence < Dry::Struct
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TranslatorText
2
4
  module Types
3
5
  class Translation < Dry::Struct
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TranslatorText
2
4
  module Types
3
5
  class TranslationResult < Dry::Struct
@@ -12,9 +14,9 @@ module TranslatorText
12
14
  #
13
15
  # @return [Symbol] the language
14
16
  def detected_language
15
- if h = detectedLanguage['language']
16
- h.to_sym
17
- end
17
+ return unless (h = detectedLanguage['language'])
18
+
19
+ h.to_sym
18
20
  end
19
21
  end
20
22
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'dry-struct'
2
4
 
3
5
  module TranslatorText
4
6
  # Includes all the types for the exchanges with the API
5
7
  module Types
6
- include Dry::Types.module
8
+ include Dry.Types
7
9
 
8
10
  Symbol = Types::Symbol.constructor do |str|
9
11
  str ? str.to_sym : str
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TranslatorText
2
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.3.0'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  lib = File.expand_path('lib', __dir__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
5
  require 'translator_text/version'
@@ -21,6 +23,8 @@ Gem::Specification.new do |spec|
21
23
 
22
24
  spec.require_paths = ['lib']
23
25
 
24
- spec.add_dependency 'dry-struct', '~> 0.5.0'
25
- spec.add_dependency 'httparty', '~> 0.15'
26
+ spec.required_ruby_version = '>= 3.1'
27
+ spec.add_dependency 'dry-struct', '~> 1'
28
+ spec.add_dependency 'httparty', '~> 0.21'
29
+ spec.metadata['rubygems_mfa_required'] = 'true'
26
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translator-text
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aymeric Brisse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-28 00:00:00.000000000 Z
11
+ date: 2023-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-struct
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.5.0
19
+ version: '1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.5.0
26
+ version: '1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: httparty
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.15'
33
+ version: '0.21'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.15'
40
+ version: '0.21'
41
41
  description: API Wrapper for the Microsoft Translator Text API (Cognitive Services)
42
42
  email:
43
43
  - aymeric.brisse@perfect-memory.com
@@ -48,6 +48,7 @@ files:
48
48
  - ".gitignore"
49
49
  - ".rspec"
50
50
  - ".rubocop.yml"
51
+ - ".ruby-version"
51
52
  - ".travis.yml"
52
53
  - CHANGELOG.md
53
54
  - CODE_OF_CONDUCT.md
@@ -72,7 +73,8 @@ files:
72
73
  homepage: https://github.com/PerfectMemory/translator-text
73
74
  licenses:
74
75
  - MIT
75
- metadata: {}
76
+ metadata:
77
+ rubygems_mfa_required: 'true'
76
78
  post_install_message:
77
79
  rdoc_options: []
78
80
  require_paths:
@@ -81,15 +83,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
83
  requirements:
82
84
  - - ">="
83
85
  - !ruby/object:Gem::Version
84
- version: '0'
86
+ version: '3.1'
85
87
  required_rubygems_version: !ruby/object:Gem::Requirement
86
88
  requirements:
87
89
  - - ">="
88
90
  - !ruby/object:Gem::Version
89
91
  version: '0'
90
92
  requirements: []
91
- rubyforge_project:
92
- rubygems_version: 2.6.14.1
93
+ rubygems_version: 3.3.26
93
94
  signing_key:
94
95
  specification_version: 4
95
96
  summary: API Wrapper for the Microsoft Translator Text API