translator-text 0.2.0 → 0.3.1

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
2
  SHA256:
3
- metadata.gz: beb931c696d3d7ccbcafa3ff1fdfbfad976be0f2c31b54d87f71d61e980ddaa4
4
- data.tar.gz: 8cd04286399ee904a25a81af6ebc941c8b187c894848cef65b9ec489678db68c
3
+ metadata.gz: 402064d43803ff2cdf50582b924de32b0a2252fa537a9bde2a9d9aff1eafc206
4
+ data.tar.gz: f297a6cf08cbeddd475a971c94e2564e03bddb7b5d2d53a338b1853ecb46014f
5
5
  SHA512:
6
- metadata.gz: 9f8573a6969215b1bfb8fe325d4cd818f99a84d8e4d85fc84bfbef6d8f88f5350ae8097b0d3ff9c6c0727f76cf6ef129b426f808cd7a92e7778defda69b01638
7
- data.tar.gz: d91a8916221f79605592e75dbf6e72cce5e0f9e0122e4038e6daf79be144eb0001a9d5b9fa0a065e411915083b68fa7dbe570817ab065a80557c208362770764
6
+ metadata.gz: 2133346bf4f959758e2ace99cfb2ef4c78aa057c248115ea82725d4b5e885019c4e36b380af3514e3e32467179261b28b5fe8f4829900e09318c462f2a5a7a7d
7
+ data.tar.gz: a315e3b0e877ddfe1ad399761e3ba5cec4b1d329ef5fd1554306733aa42c27bf5b1cd03e47b8cb68e5d09945b6a966e30b8df7505aad656e67dc6b9d9ba44d9a
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/CHANGELOG.md CHANGED
@@ -4,12 +4,24 @@ 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.2.0 (2023-04-19)
7
+ ## [0.3.1] - 2024-03-05
8
+ ### Fixes
9
+ - Better support of errors
10
+
11
+ ## [0.3.0] - 2023-11-15
12
+ ### Changed
13
+ - Add support of the COGNITIVE_SUBSCRIPTION_REGION
14
+ - Target ruby 3.1+
15
+
16
+ ### Fixed
17
+ - Upgrade Client array queries normalization
18
+
19
+ ## [0.2.0] - 2023-04-19
8
20
  ### Changed
9
21
  - Use dry-struct 1+
10
22
  - Switch to ruby 3.1 as default ruby version
11
23
 
12
- ## 0.1.0 (2018-08-28)
24
+ ## [0.1.0] - 2018-08-28
13
25
  ### Added
14
26
  - Initial release
15
27
  - Support of <translation> service
data/Gemfile CHANGED
@@ -1,12 +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
7
+ group :test, :development do
6
8
  gem 'dotenv'
7
9
  gem 'rake', '~> 12'
8
10
  gem 'rspec', '~> 3'
9
11
  gem 'rubocop'
12
+ gem 'rubocop-rspec'
10
13
  gem 'simplecov', require: false
11
14
  gem 'webmock'
12
15
  gem 'yard'
data/Gemfile.lock CHANGED
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- translator-text (0.2.0)
4
+ translator-text (0.3.1)
5
5
  dry-struct (~> 1)
6
6
  httparty (~> 0.21)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- addressable (2.8.4)
11
+ addressable (2.8.5)
12
12
  public_suffix (>= 2.0.2, < 6.0)
13
13
  ast (2.4.2)
14
14
  concurrent-ruby (1.2.2)
@@ -17,7 +17,7 @@ GEM
17
17
  diff-lcs (1.5.0)
18
18
  docile (1.4.0)
19
19
  dotenv (2.8.1)
20
- dry-core (1.0.0)
20
+ dry-core (1.0.1)
21
21
  concurrent-ruby (~> 1.0)
22
22
  zeitwerk (~> 2.6)
23
23
  dry-inflector (1.0.0)
@@ -42,41 +42,53 @@ GEM
42
42
  multi_xml (>= 0.5.2)
43
43
  ice_nine (0.11.2)
44
44
  json (2.6.3)
45
- mini_mime (1.1.2)
45
+ language_server-protocol (3.17.0.3)
46
+ mini_mime (1.1.5)
46
47
  multi_xml (0.6.0)
47
48
  parallel (1.23.0)
48
- parser (3.2.2.0)
49
+ parser (3.2.2.4)
49
50
  ast (~> 2.4.1)
50
- public_suffix (5.0.1)
51
+ racc
52
+ public_suffix (5.0.3)
53
+ racc (1.7.3)
51
54
  rainbow (3.1.1)
52
55
  rake (12.3.3)
53
- regexp_parser (2.8.0)
54
- rexml (3.2.5)
56
+ regexp_parser (2.8.2)
57
+ rexml (3.2.6)
55
58
  rspec (3.12.0)
56
59
  rspec-core (~> 3.12.0)
57
60
  rspec-expectations (~> 3.12.0)
58
61
  rspec-mocks (~> 3.12.0)
59
62
  rspec-core (3.12.2)
60
63
  rspec-support (~> 3.12.0)
61
- rspec-expectations (3.12.2)
64
+ rspec-expectations (3.12.3)
62
65
  diff-lcs (>= 1.2.0, < 2.0)
63
66
  rspec-support (~> 3.12.0)
64
- rspec-mocks (3.12.5)
67
+ rspec-mocks (3.12.6)
65
68
  diff-lcs (>= 1.2.0, < 2.0)
66
69
  rspec-support (~> 3.12.0)
67
- rspec-support (3.12.0)
68
- rubocop (1.50.2)
70
+ rspec-support (3.12.1)
71
+ rubocop (1.57.2)
69
72
  json (~> 2.3)
73
+ language_server-protocol (>= 3.17.0)
70
74
  parallel (~> 1.10)
71
- parser (>= 3.2.0.0)
75
+ parser (>= 3.2.2.4)
72
76
  rainbow (>= 2.2.2, < 4.0)
73
77
  regexp_parser (>= 1.8, < 3.0)
74
78
  rexml (>= 3.2.5, < 4.0)
75
- rubocop-ast (>= 1.28.0, < 2.0)
79
+ rubocop-ast (>= 1.28.1, < 2.0)
76
80
  ruby-progressbar (~> 1.7)
77
81
  unicode-display_width (>= 2.4.0, < 3.0)
78
- rubocop-ast (1.28.0)
82
+ rubocop-ast (1.30.0)
79
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)
80
92
  ruby-progressbar (1.13.0)
81
93
  simplecov (0.22.0)
82
94
  docile (~> 1.1)
@@ -84,13 +96,13 @@ GEM
84
96
  simplecov_json_formatter (~> 0.1)
85
97
  simplecov-html (0.12.3)
86
98
  simplecov_json_formatter (0.1.4)
87
- unicode-display_width (2.4.2)
88
- webmock (3.18.1)
99
+ unicode-display_width (2.5.0)
100
+ webmock (3.19.1)
89
101
  addressable (>= 2.8.0)
90
102
  crack (>= 0.3.2)
91
103
  hashdiff (>= 0.4.0, < 2.0.0)
92
104
  yard (0.9.34)
93
- zeitwerk (2.6.7)
105
+ zeitwerk (2.6.12)
94
106
 
95
107
  PLATFORMS
96
108
  ruby
@@ -100,6 +112,7 @@ DEPENDENCIES
100
112
  rake (~> 12)
101
113
  rspec (~> 3)
102
114
  rubocop
115
+ rubocop-rspec
103
116
  simplecov
104
117
  translator-text!
105
118
  webmock
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
 
@@ -83,30 +93,28 @@ module TranslatorText
83
93
  # If success, return the response body
84
94
  # If failure, raise an error
85
95
  def handle_response(response)
86
- case response.code
87
- when 200..299
88
- response
89
- else
90
- if response.request.format == :json
91
- raise ServiceError.new(
92
- code: response['error']['code'],
93
- message: response['error']['message']
94
- )
95
- else
96
- raise NetError.new(
97
- code: response.code,
98
- message: response.response.message
99
- )
100
- end
96
+ return response if response.code.between?(200, 299)
97
+
98
+ if response.request.format == :json && response['error']
99
+ raise ServiceError.new(
100
+ code: response['error']['code'],
101
+ message: response['error']['message']
102
+ )
101
103
  end
104
+
105
+ raise NetError.new(
106
+ code: response.code,
107
+ message: response.response.message
108
+ )
102
109
  end
103
110
 
104
111
  def headers
105
112
  {
106
113
  'Ocp-Apim-Subscription-Key' => @api_key,
114
+ 'Ocp-Apim-Subscription-Region' => @api_region,
107
115
  'X-ClientTraceId' => SecureRandom.uuid,
108
116
  'Content-type' => 'application/json'
109
- }
117
+ }.compact
110
118
  end
111
119
  end
112
120
  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
@@ -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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'dry-struct'
2
4
 
3
5
  module TranslatorText
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TranslatorText
2
- VERSION = '0.2.0'.freeze
4
+ VERSION = '0.3.1'
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
 
26
+ spec.required_ruby_version = '>= 3.1'
24
27
  spec.add_dependency 'dry-struct', '~> 1'
25
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.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aymeric Brisse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-19 00:00:00.000000000 Z
11
+ date: 2024-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-struct
@@ -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,7 +83,7 @@ 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
  - - ">="