bing_translator 6.1.0 → 6.2.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 +16 -10
  3. metadata +2 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f88718168ee707a700ed926c13ff7539155d5e94535145c912672520a166f72
4
- data.tar.gz: 0d5afc1c573bb1c613a7ca77fc7cc8ae2373477677845b3b21f4e425b6ea2fa1
3
+ metadata.gz: 7135526b9017a66474ba77d994758c3f9bed0133527133f8d1abc5bf9461bad1
4
+ data.tar.gz: 4fce05ccdbb038f514dd80e7420951bad1e70e9d7b13be2d653fd7df71c5b347
5
5
  SHA512:
6
- metadata.gz: 61a293de90f373bbe12a79989c32a416949b557761dcfb3a773b7fb5c2f9a3bb1657bc084661dee065aa603c0497ada6bf3e6fbc66ea17c99a4f2bfb200a7b6f
7
- data.tar.gz: 0d9afa36690c92b84987392f5b5ca1d970e5b057144f355fe8733e304a4acc444522159fd0fc8bbc5510c6a2417872972b84aa77124dbfd2d45899e9e2a5a6d1
6
+ metadata.gz: 17393a3b1cf0e224f57cfefa1a0f9aac02cf235d415415b68c0a12c0740cb56051f4aa324c58cb822f892255ff086edd337a31c3c41efe1511fa9add84e5eb9f
7
+ data.tar.gz: 5368fa7751bebf485142e5b6a7e6821c25544c7a9a6e8524ef033f6cc65de7e71616935d51a8ed92a2a442634e6eff57be4bbdb6b53a3da0ed13879174766bc6
@@ -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 Exception.new("Unsuccessful API call: Code: #{response.code}") unless response.code == '200'
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
- if response.code != '200'
94
- raise Exception.new('Invalid credentials')
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 Exception.new('Not supported since 3.0.0')
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 Exception.new('Must provide :to.') if params[:to].nil?
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'] == params[:to].to_s }
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.1.0
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
- rubyforge_project:
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