Class: BingTranslator
- Inherits:
-
Object
- Object
- BingTranslator
- 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
- #balance ⇒ Object
- #detect(text) ⇒ Object
-
#get_access_token ⇒ hash
Get a new access token and set it internally as @access_token.
-
#initialize(client_id, client_secret, skip_ssl_verify = false, account_key = nil) ⇒ BingTranslator
constructor
A new instance of BingTranslator.
- #language_names(codes, locale = 'en') ⇒ Object
-
#speak(text, params = {}) ⇒ Object
format: ‘audio/wav’ [default] or ‘audio/mp3’ language: valid translator language code options: ‘MinSize’ [default] or ‘MaxQuality’.
- #supported_language_codes ⇒ Object
- #translate(text, params = {}) ⇒ Object
- #translate_array(texts, params = {}) ⇒ Object
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
114 115 116 117 118 |
# File 'lib/bing_translator.rb', line 114 def balance datasets["d"]["results"].each do |result| return result["ResourceBalance"] if result["ProviderName"] == "Microsoft Translator" end end |
#detect(text) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/bing_translator.rb', line 63 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.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/bing_translator.rb', line 129 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
106 107 108 109 110 111 112 |
# File 'lib/bing_translator.rb', line 106 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’
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bing_translator.rb', line 75 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
102 103 104 |
# File 'lib/bing_translator.rb', line 102 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? # Important notice: param order makes sense in SOAP. Do not reorder or delete! 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? # Important notice: param order makes sense in SOAP. Do not reorder or delete! 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 |