bing_translator 6.1.0 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bing_translator.rb +16 -10
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7135526b9017a66474ba77d994758c3f9bed0133527133f8d1abc5bf9461bad1
|
4
|
+
data.tar.gz: 4fce05ccdbb038f514dd80e7420951bad1e70e9d7b13be2d653fd7df71c5b347
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17393a3b1cf0e224f57cfefa1a0f9aac02cf235d415415b68c0a12c0740cb56051f4aa324c58cb822f892255ff086edd337a31c3c41efe1511fa9add84e5eb9f
|
7
|
+
data.tar.gz: 5368fa7751bebf485142e5b6a7e6821c25544c7a9a6e8524ef033f6cc65de7e71616935d51a8ed92a2a442634e6eff57be4bbdb6b53a3da0ed13879174766bc6
|
data/lib/bing_translator.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
# (c) 2011-present. Ricky Elrod <ricky@elrod.me>
|
4
|
-
# Released under the MIT license.
|
2
|
+
# Released under the MIT license, see LICENSE.
|
5
3
|
require 'rubygems'
|
6
4
|
require 'cgi'
|
7
5
|
require 'uri'
|
@@ -11,6 +9,10 @@ require 'json'
|
|
11
9
|
|
12
10
|
class BingTranslator
|
13
11
|
class Exception < StandardError; end
|
12
|
+
class ApiException < BingTranslator::Exception; end
|
13
|
+
class UsageException < BingTranslator::Exception; end
|
14
|
+
class UnavailableException < BingTranslator::Exception; end
|
15
|
+
class AuthenticationException < BingTranslator::Exception; end
|
14
16
|
|
15
17
|
class ApiClient
|
16
18
|
API_HOST = 'https://api.cognitive.microsofttranslator.com'.freeze
|
@@ -53,7 +55,7 @@ class BingTranslator
|
|
53
55
|
|
54
56
|
response = http.request(request)
|
55
57
|
|
56
|
-
raise
|
58
|
+
raise ApiException.new("Unsuccessful API call: Code: #{response.code}") unless response.code == '200'
|
57
59
|
JSON.parse(response.body)
|
58
60
|
end
|
59
61
|
|
@@ -90,11 +92,14 @@ class BingTranslator
|
|
90
92
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_ssl_verify
|
91
93
|
|
92
94
|
response = http.post(COGNITIVE_ACCESS_TOKEN_URI.path, '', headers)
|
93
|
-
|
94
|
-
|
95
|
-
else
|
95
|
+
case response
|
96
|
+
when Net::HTTPSuccess
|
96
97
|
@access_token_expiration_time = Time.now + 480
|
97
98
|
response.body
|
99
|
+
when Net::HTTPServerError
|
100
|
+
raise UnavailableException.new("#{response.code}: Credentials server unavailable")
|
101
|
+
else
|
102
|
+
raise AuthenticationException.new("Unsuccessful Access Token call: Code: #{response.code} (Invalid credentials?)")
|
98
103
|
end
|
99
104
|
end
|
100
105
|
end
|
@@ -133,7 +138,7 @@ class BingTranslator
|
|
133
138
|
end
|
134
139
|
|
135
140
|
def speak(text, params = {})
|
136
|
-
raise
|
141
|
+
raise UsageException.new('Not supported since 3.0.0')
|
137
142
|
end
|
138
143
|
|
139
144
|
def supported_language_codes
|
@@ -159,13 +164,14 @@ class BingTranslator
|
|
159
164
|
attr_reader :api_client
|
160
165
|
|
161
166
|
def translation_request(texts, params)
|
162
|
-
raise
|
167
|
+
raise UsageException.new('Must provide :to.') if params[:to].nil?
|
163
168
|
|
164
169
|
data = texts.map { |text| { 'Text' => text } }.to_json
|
165
170
|
response_json = api_client.post('/translate', params: params, data: data)
|
171
|
+
to_lang = params[:to].to_s
|
166
172
|
response_json.map do |translation|
|
167
173
|
# There should be just one translation, but who knows...
|
168
|
-
translation['translations'].find { |result| result['to']
|
174
|
+
translation['translations'].find { |result| result['to'].casecmp(to_lang).zero? }
|
169
175
|
end
|
170
176
|
end
|
171
177
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bing_translator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.
|
4
|
+
version: 6.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricky Elrod
|
@@ -51,8 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '0'
|
53
53
|
requirements: []
|
54
|
-
|
55
|
-
rubygems_version: 2.7.6.2
|
54
|
+
rubygems_version: 3.2.22
|
56
55
|
signing_key:
|
57
56
|
specification_version: 4
|
58
57
|
summary: Translate using the Bing HTTP API
|