bing_translator 4.6.0 → 5.0.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 +27 -64
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4a89ca33b028fba8416d74bc1cde262de6e2f14
|
4
|
+
data.tar.gz: 5d8067fe8c7bd8af39de751c3eb04a70b8e0b120
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb7563e99e2252f49716087d03dd100c3e4dc680f9c244bc2c65c9a04fbb7bf8572450e011584eee32aeb51f929f4eb5bd2c6513a3c291dddb974d1d19e2765a
|
7
|
+
data.tar.gz: 5176196c0ff71f703a94148ae7bdddd81cb1a912631b93754641c42e347a2e0a80ac735d562f9154797e8909671e1a7288e966ab53414e1a9e8818533618a0f0
|
data/lib/bing_translator.rb
CHANGED
@@ -14,20 +14,13 @@ require 'savon'
|
|
14
14
|
class BingTranslator
|
15
15
|
WSDL_URI = 'http://api.microsofttranslator.com/V2/soap.svc?wsdl'
|
16
16
|
NAMESPACE_URI = 'http://api.microsofttranslator.com/V2'
|
17
|
-
|
18
|
-
DATASETS_URI = "https://api.datamarket.azure.com/Services/My/Datasets?$format=json"
|
17
|
+
COGNITIVE_ACCESS_TOKEN_URI = URI.parse('https://api.cognitive.microsoft.com/sts/v1.0/issueToken').freeze
|
19
18
|
|
20
19
|
class Exception < StandardError; end
|
21
|
-
class AuthenticationException < StandardError; end
|
22
20
|
|
23
|
-
def initialize(
|
24
|
-
@
|
25
|
-
@
|
26
|
-
@account_key = account_key
|
27
|
-
@skip_ssl_verify = skip_ssl_verify
|
28
|
-
|
29
|
-
@access_token_uri = URI.parse ACCESS_TOKEN_URI
|
30
|
-
@datasets_uri = URI.parse DATASETS_URI
|
21
|
+
def initialize(subscription_key, options = {})
|
22
|
+
@skip_ssl_verify = options.fetch(:skip_ssl_verify, false)
|
23
|
+
@subscription_key = subscription_key
|
31
24
|
end
|
32
25
|
|
33
26
|
def translate(text, params = {})
|
@@ -75,14 +68,15 @@ class BingTranslator
|
|
75
68
|
array_wrap(result(:translate_array2, params)[:translate_array2_response]).map{|r| [r[:translated_text], r[:alignment]]}
|
76
69
|
end
|
77
70
|
|
78
|
-
|
79
71
|
def detect(text)
|
80
72
|
params = {
|
81
73
|
'text' => text.to_s,
|
82
74
|
'language' => '',
|
83
75
|
}
|
84
76
|
|
85
|
-
result(:detect, params)
|
77
|
+
if lang = result(:detect, params)
|
78
|
+
lang.to_sym
|
79
|
+
end
|
86
80
|
end
|
87
81
|
|
88
82
|
# format: 'audio/wav' [default] or 'audio/mp3'
|
@@ -127,73 +121,42 @@ class BingTranslator
|
|
127
121
|
response[:string]
|
128
122
|
end
|
129
123
|
|
130
|
-
|
131
|
-
datasets["d"]["results"].each do |result|
|
132
|
-
return result["ResourceBalance"] if result["ProviderName"] == "Microsoft Translator"
|
133
|
-
end
|
134
|
-
end
|
124
|
+
private
|
135
125
|
|
136
126
|
# Get a new access token and set it internally as @access_token
|
137
127
|
#
|
138
|
-
# Microsoft changed up how you get access to the Translate API.
|
139
|
-
# This gets a new token if it's required. We call this internally
|
140
|
-
# before any request we make to the Translate API.
|
141
|
-
#
|
142
128
|
# @return {hash}
|
143
129
|
# Returns existing @access_token if we don't need a new token yet,
|
144
130
|
# or returns the one just obtained.
|
145
131
|
def get_access_token
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
params = {
|
150
|
-
'client_id' => CGI.escape(@client_id),
|
151
|
-
'client_secret' => CGI.escape(@client_secret),
|
152
|
-
'scope' => CGI.escape('http://api.microsofttranslator.com'),
|
153
|
-
'grant_type' => 'client_credentials'
|
132
|
+
headers = {
|
133
|
+
'Ocp-Apim-Subscription-Key' => @subscription_key
|
154
134
|
}
|
155
135
|
|
156
|
-
http = Net::HTTP.new(
|
136
|
+
http = Net::HTTP.new(COGNITIVE_ACCESS_TOKEN_URI.host, COGNITIVE_ACCESS_TOKEN_URI.port)
|
157
137
|
http.use_ssl = true
|
158
138
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_ssl_verify
|
159
139
|
|
160
|
-
response = http.post(
|
161
|
-
@access_token = JSON.parse(response.body)
|
162
|
-
raise AuthenticationException, @access_token['error'] if @access_token["error"]
|
163
|
-
@access_token['expires_at'] = Time.now + @access_token['expires_in'].to_i
|
164
|
-
@access_token
|
165
|
-
end
|
166
|
-
|
167
|
-
private
|
168
|
-
def datasets
|
169
|
-
raise AuthenticationException, "Must provide account key" if @account_key.nil?
|
170
|
-
|
171
|
-
http = Net::HTTP.new(@datasets_uri.host, @datasets_uri.port)
|
172
|
-
http.use_ssl = true
|
173
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
174
|
-
request = Net::HTTP::Get.new(@datasets_uri.request_uri)
|
175
|
-
request.basic_auth("", @account_key)
|
176
|
-
response = http.request(request)
|
140
|
+
response = http.post(COGNITIVE_ACCESS_TOKEN_URI.path, "", headers)
|
177
141
|
|
178
|
-
|
142
|
+
@access_token = {
|
143
|
+
'access_token' => response.body,
|
144
|
+
'expires_at' => Time.now + 480
|
145
|
+
}
|
179
146
|
end
|
180
147
|
|
181
|
-
|
182
|
-
|
148
|
+
# Performs actual request to Bing Translator SOAP API
|
149
|
+
def result(action, params = {}, &block)
|
150
|
+
soap_client.call(action, message: build_soap_message(params), &block).body[:"#{action}_response"][:"#{action}_result"]
|
151
|
+
rescue Savon::SOAPFault => e
|
152
|
+
raise Exception, e.message
|
183
153
|
end
|
184
154
|
|
185
|
-
#
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
soap_client.call(action, message: params, &block).body[:"#{action}_response"][:"#{action}_result"]
|
191
|
-
rescue AuthenticationException
|
192
|
-
raise
|
193
|
-
rescue StandardError => e
|
194
|
-
# Keep old behaviour: raise only internal Exception class
|
195
|
-
raise Exception, e.message
|
196
|
-
end
|
155
|
+
# Specify SOAP namespace in tag names (see https://github.com/savonrb/savon/issues/340 )
|
156
|
+
#
|
157
|
+
# @return [Hash]
|
158
|
+
def build_soap_message(params)
|
159
|
+
Hash[params.map{|k,v| ["v2:#{k}", v]}]
|
197
160
|
end
|
198
161
|
|
199
162
|
# Private: Constructs SOAP client
|
@@ -202,7 +165,7 @@ private
|
|
202
165
|
# Return stored client while access token is fresh.
|
203
166
|
# Construct and store new client when token have been expired.
|
204
167
|
def soap_client
|
205
|
-
return @client if @client and @access_token and
|
168
|
+
return @client if @client and @access_token and @access_token['expires_at'] and
|
206
169
|
Time.now < @access_token['expires_at']
|
207
170
|
|
208
171
|
@client = Savon.client(
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bing_translator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricky Elrod
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -80,9 +80,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
80
|
version: '0'
|
81
81
|
requirements: []
|
82
82
|
rubyforge_project:
|
83
|
-
rubygems_version: 2.
|
83
|
+
rubygems_version: 2.6.8
|
84
84
|
signing_key:
|
85
85
|
specification_version: 4
|
86
86
|
summary: Translate using the Bing HTTP API
|
87
87
|
test_files: []
|
88
|
-
has_rdoc:
|