bing_translator 4.1.1 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bing_translator.rb +56 -43
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c47210ab956062ed129494ee00ddea3a5bef49b9
|
4
|
+
data.tar.gz: 4de26bfdc79fbbca80aaeea72ff5bc5a278604b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b58e734f08077b607401d5b5552340ab7d96ae43a4bf731d3241fb6f941486e9257e2b446adc256a0035f8b490ef1b53ec3e6854898055d4184620df4feffde
|
7
|
+
data.tar.gz: b4a3ed05526f260e858144f471797324f6858e471b2b6b042e7ebd0935307633807dde5f70b464da21ae60437cd9cd195387ddf94f7388b6ee072b71da2d273f
|
data/lib/bing_translator.rb
CHANGED
@@ -9,13 +9,12 @@ require 'net/http'
|
|
9
9
|
require 'net/https'
|
10
10
|
require 'nokogiri'
|
11
11
|
require 'json'
|
12
|
+
require 'savon'
|
12
13
|
|
13
14
|
class BingTranslator
|
14
|
-
|
15
|
-
|
16
|
-
LANG_CODE_LIST_URI = 'http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate'
|
15
|
+
WSDL_URI = 'http://api.microsofttranslator.com/V2/soap.svc?wsdl'
|
16
|
+
NAMESPACE_URI = 'http://api.microsofttranslator.com/V2'
|
17
17
|
ACCESS_TOKEN_URI = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
|
18
|
-
SPEAK_URI = 'http://api.microsofttranslator.com/v2/Http.svc/Speak'
|
19
18
|
DATASETS_URI = "https://api.datamarket.azure.com/Services/My/Datasets?$format=json"
|
20
19
|
|
21
20
|
class Exception < StandardError; end
|
@@ -27,39 +26,32 @@ class BingTranslator
|
|
27
26
|
@account_key = account_key
|
28
27
|
@skip_ssl_verify = skip_ssl_verify
|
29
28
|
|
30
|
-
@translate_uri = URI.parse TRANSLATE_URI
|
31
|
-
@detect_uri = URI.parse DETECT_URI
|
32
|
-
@list_codes_uri = URI.parse LANG_CODE_LIST_URI
|
33
29
|
@access_token_uri = URI.parse ACCESS_TOKEN_URI
|
34
|
-
@speak_uri = URI.parse SPEAK_URI
|
35
30
|
@datasets_uri = URI.parse DATASETS_URI
|
36
31
|
end
|
37
32
|
|
38
33
|
def translate(text, params = {})
|
39
34
|
raise "Must provide :to." if params[:to].nil?
|
40
35
|
|
41
|
-
|
36
|
+
# Important notice: param order makes sense in SOAP. Do not reorder or delete!
|
42
37
|
params = {
|
43
|
-
'
|
44
|
-
'
|
38
|
+
'text' => text.to_s,
|
39
|
+
'from' => params[:from].to_s,
|
40
|
+
'to' => params[:to].to_s,
|
45
41
|
'category' => 'general',
|
46
42
|
'contentType' => params[:content_type] || 'text/plain'
|
47
43
|
}
|
48
|
-
params[:from] = from unless from.empty?
|
49
|
-
result = result @translate_uri, params
|
50
44
|
|
51
|
-
|
45
|
+
result(:translate, params).body[:translate_response][:translate_result]
|
52
46
|
end
|
53
47
|
|
54
48
|
def detect(text)
|
55
49
|
params = {
|
56
|
-
'text'
|
57
|
-
'
|
58
|
-
'contentType' => 'text/plain'
|
50
|
+
'text' => text.to_s,
|
51
|
+
'language' => '',
|
59
52
|
}
|
60
|
-
result = result @detect_uri, params
|
61
53
|
|
62
|
-
|
54
|
+
result = result(:detect, params).body[:detect_response][:detect_result].to_sym
|
63
55
|
end
|
64
56
|
|
65
57
|
# format: 'audio/wav' [default] or 'audio/mp3'
|
@@ -69,19 +61,31 @@ class BingTranslator
|
|
69
61
|
raise "Must provide :language" if params[:language].nil?
|
70
62
|
|
71
63
|
params = {
|
72
|
-
'
|
73
|
-
'
|
74
|
-
'
|
64
|
+
'text' => text.to_s,
|
65
|
+
'language' => params[:language].to_s,
|
66
|
+
'format' => params[:format] || 'audio/wav',
|
67
|
+
'options' => params[:options] || 'MinSize',
|
75
68
|
}
|
76
69
|
|
77
|
-
|
70
|
+
uri = URI.parse(result(:speak, params).body[:speak_response][:speak_result])
|
78
71
|
|
79
|
-
|
72
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
73
|
+
if uri.scheme == "https"
|
74
|
+
http.use_ssl = true
|
75
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_ssl_verify
|
76
|
+
end
|
77
|
+
results = http.get(uri, {'Authorization' => "Bearer #{get_access_token['access_token']}"})
|
78
|
+
|
79
|
+
if results.response.code.to_i == 200
|
80
|
+
results.body
|
81
|
+
else
|
82
|
+
html = Nokogiri::HTML(results.body)
|
83
|
+
raise Exception, html.xpath("//text()").remove.map(&:to_s).join(' ')
|
84
|
+
end
|
80
85
|
end
|
81
86
|
|
82
87
|
def supported_language_codes
|
83
|
-
result
|
84
|
-
Nokogiri.parse(result.body).xpath("//xmlns:string").map(&:content)
|
88
|
+
result(:get_languages_for_translate).body[:get_languages_for_translate_response][:get_languages_for_translate_result][:string]
|
85
89
|
end
|
86
90
|
|
87
91
|
|
@@ -109,26 +113,35 @@ private
|
|
109
113
|
params.map { |key, value| "#{key}=#{value}" }.join '&'
|
110
114
|
end
|
111
115
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
116
|
+
# Public: performs actual request to Bing Translator SOAP API
|
117
|
+
def result(action, params={})
|
118
|
+
# Specify SOAP namespace in tag names (see https://github.com/savonrb/savon/issues/340 )
|
119
|
+
params = Hash[params.map{|k,v| ["v2:#{k}", v]}]
|
120
|
+
begin
|
121
|
+
soap_client.call(action, message: params)
|
122
|
+
rescue AuthenticationException
|
123
|
+
raise
|
124
|
+
rescue StandardError => e
|
125
|
+
# Keep old behaviour: raise only internal Exception class
|
126
|
+
raise Exception, e.message
|
118
127
|
end
|
128
|
+
end
|
119
129
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
130
|
+
# Private: Constructs SOAP client
|
131
|
+
#
|
132
|
+
# Construct and store new client when called first time.
|
133
|
+
# Return stored client while access token is fresh.
|
134
|
+
# Construct and store new client when token have been expired.
|
135
|
+
def soap_client
|
136
|
+
return @client if @client and @access_token and
|
137
|
+
Time.now < @access_token['expires_at']
|
125
138
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
139
|
+
@client = Savon.client(
|
140
|
+
wsdl: WSDL_URI,
|
141
|
+
namespace: NAMESPACE_URI,
|
142
|
+
namespace_identifier: :v2,
|
143
|
+
headers: {'Authorization' => "Bearer #{get_access_token['access_token']}"},
|
144
|
+
)
|
132
145
|
end
|
133
146
|
|
134
147
|
# Private: Get a new access token
|
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.
|
4
|
+
version: 4.2.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: 2013-
|
11
|
+
date: 2013-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 1.8.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: savon
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
41
55
|
description: Translate strings using the Bing HTTP API. Requires that you have a Client
|
42
56
|
ID and Secret. See README.md for information.
|
43
57
|
email: ricky@elrod.me
|