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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bing_translator.rb +56 -43
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d761c38853882839ab672fd197ff094ffd5c3aa5
4
- data.tar.gz: 62d00b7d34edcbb894f05ec9da5f3d0519d593d2
3
+ metadata.gz: c47210ab956062ed129494ee00ddea3a5bef49b9
4
+ data.tar.gz: 4de26bfdc79fbbca80aaeea72ff5bc5a278604b5
5
5
  SHA512:
6
- metadata.gz: 4a8d0044389165ce6e3ff8c4337150a6d95d10554e6b454cb9dbe02c23cd8750e6595ff94ed448506033a1371a42f431fa13371a66c1c14cdcb6497ab7ad916e
7
- data.tar.gz: e62ad7ff517d12cf2c7f92871da6a9fa71f1714f6ddeee960226d3583771ca72b8378f043649d932e18ab5354f47e59206b4638ff168068f8ab30e20b722d257
6
+ metadata.gz: 9b58e734f08077b607401d5b5552340ab7d96ae43a4bf731d3241fb6f941486e9257e2b446adc256a0035f8b490ef1b53ec3e6854898055d4184620df4feffde
7
+ data.tar.gz: b4a3ed05526f260e858144f471797324f6858e471b2b6b042e7ebd0935307633807dde5f70b464da21ae60437cd9cd195387ddf94f7388b6ee072b71da2d273f
@@ -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
- TRANSLATE_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Translate'
15
- DETECT_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Detect'
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
- from = CGI.escape params[:from].to_s
36
+ # Important notice: param order makes sense in SOAP. Do not reorder or delete!
42
37
  params = {
43
- 'to' => CGI.escape(params[:to].to_s),
44
- 'text' => CGI.escape(text.to_s),
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
- Nokogiri.parse(result.body).at_xpath("//xmlns:string").content
45
+ result(:translate, params).body[:translate_response][:translate_result]
52
46
  end
53
47
 
54
48
  def detect(text)
55
49
  params = {
56
- 'text' => CGI.escape(text.to_s),
57
- 'category' => 'general',
58
- 'contentType' => 'text/plain'
50
+ 'text' => text.to_s,
51
+ 'language' => '',
59
52
  }
60
- result = result @detect_uri, params
61
53
 
62
- Nokogiri.parse(result.body).at_xpath("//xmlns:string").content.to_sym
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
- 'format' => CGI.escape(params[:format].to_s),
73
- 'text' => CGI.escape(text.to_s),
74
- 'language' => params[:language].to_s
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
- result = result(@speak_uri, params, { "Content-Type" => params[:format].to_s })
70
+ uri = URI.parse(result(:speak, params).body[:speak_response][:speak_result])
78
71
 
79
- result.body
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 = result @list_codes_uri
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
- def result(uri, params={}, headers={})
113
- get_access_token
114
- http = Net::HTTP.new(uri.host, uri.port)
115
- if uri.scheme == "https"
116
- http.use_ssl = true
117
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_ssl_verify
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
- results = http.get(
121
- "#{uri.path}?#{prepare_param_string(params)}",
122
- {
123
- 'Authorization' => "Bearer #{@access_token['access_token']}"
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
- if results.response.code.to_i == 200
127
- results
128
- else
129
- html = Nokogiri::HTML(results.body)
130
- raise Exception, html.xpath("//text()").remove.map(&:to_s).join(' ')
131
- end
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.1.1
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-08-18 00:00:00.000000000 Z
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