Class: BingTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/bing_translator.rb

Defined Under Namespace

Classes: Exception

Constant Summary collapse

WSDL_URI =
'http://api.microsofttranslator.com/V2/soap.svc?wsdl'
NAMESPACE_URI =
'http://api.microsofttranslator.com/V2'
COGNITIVE_ACCESS_TOKEN_URI =
URI.parse('https://api.cognitive.microsoft.com/sts/v1.0/issueToken').freeze

Instance Method Summary collapse

Constructor Details

#initialize(subscription_key, options = {}) ⇒ BingTranslator

Returns a new instance of BingTranslator.



21
22
23
24
# File 'lib/bing_translator.rb', line 21

def initialize(subscription_key, options = {})
  @skip_ssl_verify = options.fetch(:skip_ssl_verify, false)
  @subscription_key = subscription_key
end

Instance Method Details

#detect(text) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/bing_translator.rb', line 71

def detect(text)
  params = {
    'text'     => text.to_s,
    'language' => '',
  }

  if lang = result(:detect, params)
    lang.to_sym
  end
end

#language_names(codes, locale = 'en') ⇒ Object



116
117
118
119
120
121
122
# File 'lib/bing_translator.rb', line 116

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’



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bing_translator.rb', line 85

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_codesObject



112
113
114
# File 'lib/bing_translator.rb', line 112

def supported_language_codes
  result(:get_languages_for_translate)[:string]
end

#translate(text, params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bing_translator.rb', line 26

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bing_translator.rb', line 41

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

#translate_array2(texts, params = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bing_translator.rb', line 56

def translate_array2(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_array2, params)[:translate_array2_response]).map{|r| [r[:translated_text], r[:alignment]]}
end