bing_translator 4.6.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bing_translator.rb +27 -64
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83055c8b2f4de7db9d0ce2d41f9a1b092290ff94
4
- data.tar.gz: 62d0896420ce97c3afa5abdb079dd8d706e1e11a
3
+ metadata.gz: a4a89ca33b028fba8416d74bc1cde262de6e2f14
4
+ data.tar.gz: 5d8067fe8c7bd8af39de751c3eb04a70b8e0b120
5
5
  SHA512:
6
- metadata.gz: 4f18af208585d0b875606628cdd0abff184312fad19f973d5fd0f7e47d7e95df19fe7f05c5d0b225257692c2c31948465165495a7928b4aef4a116fc7461e4d1
7
- data.tar.gz: 12fc8ea90925e854acad94aee9a08b04b0506a5cd57c155815660c40a32800e36a11686f3cbda69f1f131fdeccfae8667c1bc23225d7cbee1c36efce1efddb2c
6
+ metadata.gz: eb7563e99e2252f49716087d03dd100c3e4dc680f9c244bc2c65c9a04fbb7bf8572450e011584eee32aeb51f929f4eb5bd2c6513a3c291dddb974d1d19e2765a
7
+ data.tar.gz: 5176196c0ff71f703a94148ae7bdddd81cb1a912631b93754641c42e347a2e0a80ac735d562f9154797e8909671e1a7288e966ab53414e1a9e8818533618a0f0
@@ -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
- ACCESS_TOKEN_URI = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
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(client_id, client_secret, skip_ssl_verify = false, account_key = nil)
24
- @client_id = client_id
25
- @client_secret = client_secret
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).to_sym
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
- def balance
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
- return @access_token if @access_token and
147
- Time.now < @access_token['expires_at']
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(@access_token_uri.host, @access_token_uri.port)
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(@access_token_uri.path, prepare_param_string(params))
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
- JSON.parse response.body
142
+ @access_token = {
143
+ 'access_token' => response.body,
144
+ 'expires_at' => Time.now + 480
145
+ }
179
146
  end
180
147
 
181
- def prepare_param_string(params)
182
- params.map { |key, value| "#{key}=#{value}" }.join '&'
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
- # Public: performs actual request to Bing Translator SOAP API
186
- def result(action, params = {}, &block)
187
- # Specify SOAP namespace in tag names (see https://github.com/savonrb/savon/issues/340 )
188
- params = Hash[params.map{|k,v| ["v2:#{k}", v]}]
189
- begin
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.6.0
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: 2016-04-22 00:00:00.000000000 Z
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.4.8
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: