Class: BingTranslator
- Inherits:
-
Object
show all
- Defined in:
- lib/bing_translator.rb
Defined Under Namespace
Classes: AuthenticationException, Exception
Constant Summary
collapse
- WSDL_URI =
'http://api.microsofttranslator.com/V2/soap.svc?wsdl'
- NAMESPACE_URI =
'http://api.microsofttranslator.com/V2'
- ACCESS_TOKEN_URI =
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
- DATASETS_URI =
"https://api.datamarket.azure.com/Services/My/Datasets?$format=json"
Instance Method Summary
collapse
Constructor Details
#initialize(client_id, client_secret, skip_ssl_verify = false, account_key = nil) ⇒ BingTranslator
Returns a new instance of BingTranslator.
23
24
25
26
27
28
29
30
31
|
# File 'lib/bing_translator.rb', line 23
def initialize(client_id, client_secret, skip_ssl_verify = false, account_key = nil)
@client_id = client_id
@client_secret = client_secret
@account_key = account_key
@skip_ssl_verify = skip_ssl_verify
@access_token_uri = URI.parse ACCESS_TOKEN_URI
@datasets_uri = URI.parse DATASETS_URI
end
|
Instance Method Details
#balance ⇒ Object
130
131
132
133
134
|
# File 'lib/bing_translator.rb', line 130
def balance
datasets["d"]["results"].each do |result|
return result["ResourceBalance"] if result["ProviderName"] == "Microsoft Translator"
end
end
|
#detect(text) ⇒ Object
79
80
81
82
83
84
85
86
|
# File 'lib/bing_translator.rb', line 79
def detect(text)
params = {
'text' => text.to_s,
'language' => '',
}
result(:detect, params).to_sym
end
|
#get_access_token ⇒ hash
Get a new access token and set it internally as @access_token
Microsoft changed up how you get access to the Translate API. This gets a new token if it’s required. We call this internally before any request we make to the Translate API.
Returns existing @access_token if we don’t need a new token yet, or returns the one just obtained.
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/bing_translator.rb', line 145
def get_access_token
return @access_token if @access_token and
Time.now < @access_token['expires_at']
params = {
'client_id' => CGI.escape(@client_id),
'client_secret' => CGI.escape(@client_secret),
'scope' => CGI.escape('http://api.microsofttranslator.com'),
'grant_type' => 'client_credentials'
}
http = Net::HTTP.new(@access_token_uri.host, @access_token_uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_ssl_verify
response = http.post(@access_token_uri.path, prepare_param_string(params))
@access_token = JSON.parse(response.body)
raise AuthenticationException, @access_token['error'] if @access_token["error"]
@access_token['expires_at'] = Time.now + @access_token['expires_in'].to_i
@access_token
end
|
#language_names(codes, locale = 'en') ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/bing_translator.rb', line 122
def language_names(codes, locale = 'en')
response = result(:get_language_names, locale: locale, languageCodes: {'a:string' => codes}) do
attributes 'xmlns:a' => 'http://schemas.microsoft.com/2003/10/Serialization/Arrays'
end
response[:string]
end
|
#speak(text, params = {}) ⇒ Object
format: ‘audio/wav’ [default] or ‘audio/mp3’ language: valid translator language code options: ‘MinSize’ [default] or ‘MaxQuality’
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/bing_translator.rb', line 91
def speak(text, params = {})
raise "Must provide :language" if params[:language].nil?
params = {
'text' => text.to_s,
'language' => params[:language].to_s,
'format' => params[:format] || 'audio/wav',
'options' => params[:options] || 'MinSize',
}
uri = URI.parse(result(:speak, params))
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_ssl_verify
end
results = http.get(uri.to_s, {'Authorization' => "Bearer #{get_access_token['access_token']}"})
if results.response.code.to_i == 200
results.body
else
html = Nokogiri::HTML(results.body)
raise Exception, html.xpath("//text()").remove.map(&:to_s).join(' ')
end
end
|
#supported_language_codes ⇒ Object
118
119
120
|
# File 'lib/bing_translator.rb', line 118
def supported_language_codes
result(:get_languages_for_translate)[:string]
end
|
#translate(text, params = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/bing_translator.rb', line 33
def translate(text, params = {})
raise "Must provide :to." if params[:to].nil?
params = {
'text' => text.to_s,
'from' => params[:from].to_s,
'to' => params[:to].to_s,
'category' => 'general',
'contentType' => params[:content_type] || 'text/plain'
}
result(:translate, params)
end
|
#translate_array(texts, params = {}) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/bing_translator.rb', line 48
def translate_array(texts, params = {})
raise "Must provide :to." if params[:to].nil?
params = {
'texts' => { 'arr:string' => texts },
'from' => params[:from].to_s,
'to' => params[:to].to_s,
'category' => 'general',
'contentType' => params[:content_type] || 'text/plain'
}
array_wrap(result(:translate_array, params)[:translate_array_response]).map{|r| r[:translated_text]}
end
|
#translate_array2(texts, params = {}) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/bing_translator.rb', line 63
def translate_array2(texts, params = {})
raise "Must provide :to." if params[:to].nil?
params = {
'texts' => { 'arr:string' => texts },
'from' => params[:from].to_s,
'to' => params[:to].to_s,
'category' => 'general',
'contentType' => params[:content_type] || 'text/plain'
}
array_wrap(result(:translate_array2, params)[:translate_array2_response]).map{|r| [r[:translated_text], r[:alignment]]}
end
|