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 +4 -4
- data/.rubocop.yml +6 -1
- data/.ruby-version +1 -0
- data/CHANGELOG.md +14 -2
- data/Gemfile +4 -1
- data/Gemfile.lock +31 -18
- data/README.md +24 -8
- data/Rakefile +2 -0
- data/bin/console +1 -0
- data/lib/translator-text.rb +6 -1
- data/lib/translator_text/client.rb +29 -21
- data/lib/translator_text/exceptions.rb +2 -0
- data/lib/translator_text/types/alternative.rb +2 -0
- data/lib/translator_text/types/detection_result.rb +2 -0
- data/lib/translator_text/types/sentence.rb +2 -0
- data/lib/translator_text/types/translation.rb +2 -0
- data/lib/translator_text/types/translation_result.rb +5 -3
- data/lib/translator_text/types.rb +2 -0
- data/lib/translator_text/version.rb +3 -1
- data/translator-text.gemspec +4 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 402064d43803ff2cdf50582b924de32b0a2252fa537a9bde2a9d9aff1eafc206
|
4
|
+
data.tar.gz: f297a6cf08cbeddd475a971c94e2564e03bddb7b5d2d53a338b1853ecb46014f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
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.
|
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
|
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.
|
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.
|
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.
|
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
|
-
|
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.
|
49
|
+
parser (3.2.2.4)
|
49
50
|
ast (~> 2.4.1)
|
50
|
-
|
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.
|
54
|
-
rexml (3.2.
|
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.
|
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.
|
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.
|
68
|
-
rubocop (1.
|
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.
|
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.
|
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.
|
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.
|
88
|
-
webmock (3.
|
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.
|
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
|
-
###
|
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
|
-
|
38
|
-
|
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
|
-
|
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
|
-
|
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
data/bin/console
CHANGED
data/lib/translator-text.rb
CHANGED
@@ -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(
|
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'
|
11
|
-
API_VERSION = '3.0'
|
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
|
-
|
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:
|
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
|
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
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
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
|
-
|
16
|
-
|
17
|
-
|
17
|
+
return unless (h = detectedLanguage['language'])
|
18
|
+
|
19
|
+
h.to_sym
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
data/translator-text.gemspec
CHANGED
@@ -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.
|
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:
|
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: '
|
86
|
+
version: '3.1'
|
85
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
88
|
requirements:
|
87
89
|
- - ">="
|