ms_translate 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/README.md +76 -0
- data/Rakefile +11 -0
- data/lib/ms_translate.rb +394 -0
- data/lib/ms_translate/version.rb +4 -0
- data/ms_translate.gemspec +24 -0
- data/spec/lib/ms_translate/api_spec.rb +140 -0
- data/spec/spec_helper.rb +29 -0
- metadata +110 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# MsTranslate
|
2
|
+
The library is a simple API in Ruby for Microsoft Translator V2
|
3
|
+
|
4
|
+
The Microsoft Translator services can be used in web or client
|
5
|
+
applications to perform language translation operations. The services
|
6
|
+
support users who are not familiar with the default language of a page
|
7
|
+
or application, or those desiring to communicate with people of a
|
8
|
+
different language group.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'ms_translate'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install ms_translate
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
first step set your appId
|
27
|
+
|
28
|
+
$ MsTranslate::Api.appId = 'MyRealAppId'
|
29
|
+
|
30
|
+
traNslate method:
|
31
|
+
|
32
|
+
$ MsTranslate::Api.translate('Hello World!)
|
33
|
+
$ => Ciao, Mondo!
|
34
|
+
|
35
|
+
## Method not implemented
|
36
|
+
|
37
|
+
1. Microsoft.Translator.AddTranslation Method
|
38
|
+
2. Microsoft.Translator.AddTranslationArray Method
|
39
|
+
|
40
|
+
## Test
|
41
|
+
|
42
|
+
You need to insert your appId into
|
43
|
+
|
44
|
+
# File 'spec/lib/ms_translate/api_spec.rb'
|
45
|
+
@api_real = 'INSERT_YOUR_APPID'
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
54
|
+
|
55
|
+
|
56
|
+
## Author
|
57
|
+
|
58
|
+
[![endorse](http://api.coderwall.com/waydotnet/endorsecount.png)](http://coderwall.com/waydotnet)
|
59
|
+
|
60
|
+
WaYdotNET, you can follow me on twitter [@WaYdotNET](http://twitter.com/WaYdotNET) or take a look at my site [waydotnet.com](http://www.waydotnet.com)
|
61
|
+
|
62
|
+
## Copyright
|
63
|
+
|
64
|
+
Copyright (C) 2012 Carlo Bertini - [@WaYdotNET](http://twitter.com/WaYdotNET)
|
65
|
+
|
66
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
67
|
+
associated documentation files (the “Software”), to deal in the Software without restriction, including without
|
68
|
+
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
69
|
+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
70
|
+
|
71
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
72
|
+
|
73
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
74
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM,
|
75
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
76
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/ms_translate.rb
ADDED
@@ -0,0 +1,394 @@
|
|
1
|
+
require 'rubygems' unless defined?(Gem)
|
2
|
+
require 'httparty'
|
3
|
+
require 'ms_translate/version'
|
4
|
+
|
5
|
+
##
|
6
|
+
# Wrapper for Microsoft Translator V2
|
7
|
+
#
|
8
|
+
# @author Carlo Bertini
|
9
|
+
#
|
10
|
+
module MsTranslate
|
11
|
+
##
|
12
|
+
# monkey patch to covert method name to camelize
|
13
|
+
#
|
14
|
+
# @author Yehuda Katz
|
15
|
+
#
|
16
|
+
# see
|
17
|
+
# http://yehudakatz.com/2010/11/30/ruby-2-0-refinements-in-practice/
|
18
|
+
#
|
19
|
+
class ::String
|
20
|
+
##
|
21
|
+
# camlize method
|
22
|
+
#
|
23
|
+
def camelize()
|
24
|
+
self.dup.split(/_/).map{ |word| word.capitalize }.join('')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Wrapper for Microsoft Translator V2
|
30
|
+
#
|
31
|
+
# The library is a simple API in Ruby for Microsoft Translator V2
|
32
|
+
#
|
33
|
+
# The Microsoft Translator services can be used in web or client
|
34
|
+
# applications to perform language translation operations. The
|
35
|
+
# services support users who are not familiar with the default
|
36
|
+
# language of a page or application, or those desiring to
|
37
|
+
# communicate with people of a different language group.
|
38
|
+
#
|
39
|
+
#
|
40
|
+
# @author Carlo Bertini
|
41
|
+
#
|
42
|
+
class Api
|
43
|
+
include HTTParty
|
44
|
+
base_uri 'http://api.microsofttranslator.com/V2/Http.svc//'
|
45
|
+
FROM, TO = :en, :it
|
46
|
+
|
47
|
+
class << self
|
48
|
+
# The AppId to work with Microsoft Translator
|
49
|
+
attr_accessor :appId
|
50
|
+
|
51
|
+
# The language code to translate the text from
|
52
|
+
attr_accessor :from
|
53
|
+
|
54
|
+
# The language code to translate the text into.
|
55
|
+
attr_accessor :to
|
56
|
+
|
57
|
+
# init val
|
58
|
+
MsTranslate::Api.from ||= FROM
|
59
|
+
MsTranslate::Api.to ||= TO
|
60
|
+
available_langauge ||= nil
|
61
|
+
end
|
62
|
+
|
63
|
+
# Invalid language
|
64
|
+
#
|
65
|
+
class InvalidLanguage < StandardError; end
|
66
|
+
|
67
|
+
# Invalid AppId
|
68
|
+
#
|
69
|
+
class InvalidAppId < StandardError; end
|
70
|
+
|
71
|
+
# Generic ArgumentException
|
72
|
+
#
|
73
|
+
class ArgumentException < StandardError; end
|
74
|
+
|
75
|
+
# Generic ArgumentException
|
76
|
+
#
|
77
|
+
class HTTPMethodNotAllowed < StandardError; end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Translate Method
|
81
|
+
#
|
82
|
+
# Translates a text string from one language to another
|
83
|
+
#
|
84
|
+
# @param [.appId] appId
|
85
|
+
# The AppID to work with Microsoft Translator
|
86
|
+
#
|
87
|
+
# @param [.from] from
|
88
|
+
# The language code to translate the text from
|
89
|
+
#
|
90
|
+
# @param [.to] to
|
91
|
+
# The language code to translate the text to
|
92
|
+
#
|
93
|
+
# @param [String] text
|
94
|
+
# the text to translate
|
95
|
+
#
|
96
|
+
# @return [String]
|
97
|
+
# the translated text
|
98
|
+
#
|
99
|
+
def self.translate(text, query = {})
|
100
|
+
query[:from] = @from if query[:from].nil?
|
101
|
+
query[:to] = @to if query[:to].nil?
|
102
|
+
query[:text] = text
|
103
|
+
|
104
|
+
wrapper( __method__.to_s.camelize, query)['string']
|
105
|
+
end
|
106
|
+
|
107
|
+
##
|
108
|
+
# Detect Method
|
109
|
+
#
|
110
|
+
# Use the Detect Method to identify the language of a selected
|
111
|
+
# piece of text
|
112
|
+
#
|
113
|
+
# @param [.appId] appId
|
114
|
+
# The AppID to work with Microsoft Translator
|
115
|
+
#
|
116
|
+
# @param [String] text
|
117
|
+
# some text whose language is to be identified
|
118
|
+
#
|
119
|
+
# @return [String]
|
120
|
+
# two-character Language code for the given text
|
121
|
+
#
|
122
|
+
def self.detect(text)
|
123
|
+
wrapper( __method__.to_s.camelize, { :text => text })['string']
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# DetectArray Method
|
128
|
+
#
|
129
|
+
# Use the DetectArray Method to identify the language of an array
|
130
|
+
# of string at once. Performs independent detection of each
|
131
|
+
# individual array element and returns a result for each row of
|
132
|
+
# the array
|
133
|
+
#
|
134
|
+
# @param [.appId] appId
|
135
|
+
# The AppID to work with Microsoft Translator
|
136
|
+
#
|
137
|
+
# @param [Array] texts
|
138
|
+
# the text from an unknown language
|
139
|
+
#
|
140
|
+
# @return [String]
|
141
|
+
# two-character Language codes for each row of the input array
|
142
|
+
#
|
143
|
+
def self.detect_array(texts)
|
144
|
+
wrapper( __method__.to_s.camelize, { :text => texts })
|
145
|
+
end
|
146
|
+
|
147
|
+
##
|
148
|
+
# GetLanguageNames Method
|
149
|
+
#
|
150
|
+
# Retrieves friendly names for the languages passed in as the
|
151
|
+
# parameter languageCodes, and localized using the passed locale
|
152
|
+
# language
|
153
|
+
#
|
154
|
+
# @param [.appId] appId
|
155
|
+
# The AppID to work with Microsoft Translator
|
156
|
+
#
|
157
|
+
# @param [String] locale
|
158
|
+
# ISO 639 two-letter lowercase culture code
|
159
|
+
#
|
160
|
+
# @return [Array]
|
161
|
+
# languages names supported by the Translator Service, localized
|
162
|
+
# into the requested language
|
163
|
+
#
|
164
|
+
def self.get_language_names(locale, v1 = false)
|
165
|
+
wrapper( __method__.to_s.camelize, { :locale => locale.to_s, :v1 => v1 })
|
166
|
+
end
|
167
|
+
|
168
|
+
##
|
169
|
+
# GetLanguagesForSpeak Method
|
170
|
+
#
|
171
|
+
# Retrieves the languages available for speech synthesis
|
172
|
+
#
|
173
|
+
# @param [.appId] appId
|
174
|
+
# The AppID to work with Microsoft Translator
|
175
|
+
#
|
176
|
+
# return [Array]
|
177
|
+
# Tthe language codes supported for speech synthesis by the
|
178
|
+
# Translator Service
|
179
|
+
#
|
180
|
+
def self.get_languages_for_speak
|
181
|
+
wrapper( __method__.to_s.camelize )['ArrayOfstring']['string']
|
182
|
+
end
|
183
|
+
|
184
|
+
##
|
185
|
+
# GetLanguagesForTranslate Method
|
186
|
+
#
|
187
|
+
# Obtain a list of language codes representing languages that are
|
188
|
+
# supported by the Translation Service. Translate() and
|
189
|
+
# TranslateArray() can translate between any two of these
|
190
|
+
# languages
|
191
|
+
#
|
192
|
+
# @param [.appId] appId
|
193
|
+
# The AppID to work with Microsoft Translator
|
194
|
+
#
|
195
|
+
# @return [Array]
|
196
|
+
# The language codes supported by the Translator Services
|
197
|
+
#
|
198
|
+
def self.get_languages_for_translate
|
199
|
+
@available_language ||= wrapper( __method__.to_s.camelize )['ArrayOfstring']['string']
|
200
|
+
end
|
201
|
+
|
202
|
+
##
|
203
|
+
# GetTranslations Method
|
204
|
+
#
|
205
|
+
# Retrieves an array of translations for a given language pair
|
206
|
+
# from the store and the MT engine. GetTranslations differs from
|
207
|
+
# Translate as it returns all available translations.
|
208
|
+
#
|
209
|
+
# @param [.appId] appId
|
210
|
+
# The AppID to work with Microsoft Translator
|
211
|
+
#
|
212
|
+
# @param [.from] from
|
213
|
+
# The language code to translate the text from
|
214
|
+
#
|
215
|
+
# @param [.to] to
|
216
|
+
# The language code to translate the text to
|
217
|
+
#
|
218
|
+
# @param [String] text
|
219
|
+
# The text to translate.
|
220
|
+
#
|
221
|
+
# @param [Integer] maxTranslations
|
222
|
+
# the maximum number of translations to return
|
223
|
+
#
|
224
|
+
#
|
225
|
+
def self.get_translations(text, max_translations)
|
226
|
+
wrapper( __method__.to_s.camelize, {:max_translations => max_translations, :text => text})
|
227
|
+
end
|
228
|
+
|
229
|
+
##
|
230
|
+
# GetTranslationsArray Method
|
231
|
+
#
|
232
|
+
# Use the GetTranslationsArray method to retrieve multiple
|
233
|
+
# translation candidates for multiple source texts.
|
234
|
+
#
|
235
|
+
# @param [.appId] appId
|
236
|
+
# The AppID to work with Microsoft Translator
|
237
|
+
#
|
238
|
+
# @param [.from] from
|
239
|
+
# The language code to translate the text from
|
240
|
+
#
|
241
|
+
# @param [.to] to
|
242
|
+
# The language code to translate the text to
|
243
|
+
#
|
244
|
+
# @param [Array] texts
|
245
|
+
# An array containing the texts for translation. All strings
|
246
|
+
# should be of the same language.
|
247
|
+
#
|
248
|
+
# @param [Integer] maxTranslations
|
249
|
+
# the maximum number of translations to return
|
250
|
+
#
|
251
|
+
# @return [Array]
|
252
|
+
# Returns a GetTranslationsResponse array
|
253
|
+
#
|
254
|
+
def self.get_translations_array(texts, max_translations)
|
255
|
+
wrapper( __method__.to_s.camelize, {:max_translations => max_translations, :texts => texts})
|
256
|
+
end
|
257
|
+
|
258
|
+
##
|
259
|
+
# Speak Method
|
260
|
+
#
|
261
|
+
# Returns a wave or mp3 stream of the passed-in text being spoken in the desired language
|
262
|
+
#
|
263
|
+
# @param [.appId] appId
|
264
|
+
# The AppID to work with Microsoft Translator
|
265
|
+
#
|
266
|
+
# @param [String] text
|
267
|
+
# A sentence or sentences of the specified language to be spoken for the wave stream
|
268
|
+
#
|
269
|
+
# @param [String] language
|
270
|
+
# The supported language code to speak the text in. (see get_languages_for_speak)
|
271
|
+
#
|
272
|
+
def self.speak(text, language)
|
273
|
+
wrapper( __method__.to_s.camelize, {:text => text, :language => language})
|
274
|
+
end
|
275
|
+
|
276
|
+
##
|
277
|
+
# TranslateArray Method
|
278
|
+
#
|
279
|
+
# Use the TranslateArray method to retrieve translations for
|
280
|
+
# multiple source texts.
|
281
|
+
#
|
282
|
+
# @param [.appId] appId
|
283
|
+
# The AppID to work with Microsoft Translator
|
284
|
+
#
|
285
|
+
# @param [.from] from
|
286
|
+
# The language code to translate the text from
|
287
|
+
#
|
288
|
+
# @param [.to] to
|
289
|
+
# The language code to translate the text to
|
290
|
+
#
|
291
|
+
# @param [Array] texts
|
292
|
+
# An array containing the texts for translation. All strings
|
293
|
+
# should be of the same language.
|
294
|
+
#
|
295
|
+
# @return [Array]
|
296
|
+
# Returns a TranslateArrayResponse array
|
297
|
+
def self.translate_array(texts)
|
298
|
+
wrapper( __method__.to_s.camelize, {:texts => texts})
|
299
|
+
end
|
300
|
+
|
301
|
+
##
|
302
|
+
# BreakSentences Method
|
303
|
+
#
|
304
|
+
# Breaks a piece of text into sentences and returns an array
|
305
|
+
# containing the lengths in each sentence.
|
306
|
+
#
|
307
|
+
# @param [.appId] appId
|
308
|
+
# The AppID to work with Microsoft Translator
|
309
|
+
#
|
310
|
+
# @param [String] text
|
311
|
+
# The text to split into sentences.
|
312
|
+
#
|
313
|
+
# @param [String] language
|
314
|
+
# The language code of input text.
|
315
|
+
#
|
316
|
+
# @return [Array]
|
317
|
+
# An array of integers representing the lengths of the
|
318
|
+
# sentences. The length of the array is the number of sentences,
|
319
|
+
# and the values are the length of each sentence.
|
320
|
+
#
|
321
|
+
def self.break_sentences(text, language)
|
322
|
+
wrapper( __method__.to_s.camelize, {:text => text, :language => language})['ArrayOfint']['int']
|
323
|
+
end
|
324
|
+
|
325
|
+
##
|
326
|
+
# reset attributes to default value
|
327
|
+
#
|
328
|
+
def self.reset!
|
329
|
+
@from = FROM
|
330
|
+
@to = TO
|
331
|
+
@appId = nil
|
332
|
+
@available_language = nil
|
333
|
+
end
|
334
|
+
|
335
|
+
##
|
336
|
+
# The Wrapper !!!
|
337
|
+
#
|
338
|
+
# The real method to invoce call function from HTTParty
|
339
|
+
#
|
340
|
+
# @param [String] method
|
341
|
+
# The method name to call
|
342
|
+
#
|
343
|
+
# @param [Hash] query
|
344
|
+
# the params to send
|
345
|
+
#
|
346
|
+
#
|
347
|
+
# @api private
|
348
|
+
def self.wrapper(method, query = {})
|
349
|
+
|
350
|
+
query[:appId] = @appId if query[:appId].nil?
|
351
|
+
raise InvalidAppId if query[:appId].nil? || query[:appId].length < 16
|
352
|
+
# check if you wan V1
|
353
|
+
base_uri.gsub!('V2', 'V1') if query.delete(:v1)
|
354
|
+
valid_language?( query[:from], query[:to] ) unless query[:from].nil? || query[:to].nil?
|
355
|
+
|
356
|
+
response = get(method, :query => query)
|
357
|
+
base_uri.gsub!('V1', 'V2')
|
358
|
+
case response.code
|
359
|
+
when 200
|
360
|
+
response
|
361
|
+
when 404
|
362
|
+
raise ServiceNotFound
|
363
|
+
when 405
|
364
|
+
raise HTTPMethodNotAllowed
|
365
|
+
when 500...600
|
366
|
+
raise ServiceError, "ZOMG ERROR #{response.code}"
|
367
|
+
when 400
|
368
|
+
raise ArgumentException
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
##
|
373
|
+
# ValidLanguage? Method
|
374
|
+
#
|
375
|
+
# Check if the language is supported from Microsoft Translator
|
376
|
+
#
|
377
|
+
# @param [Array] langs
|
378
|
+
# the language to check
|
379
|
+
#
|
380
|
+
# @return [Boolean]
|
381
|
+
# check resutl :D
|
382
|
+
#
|
383
|
+
# @api private
|
384
|
+
def self.valid_language?(*langs)
|
385
|
+
langs.each { |arg_item| raise InvalidLanguage unless get_languages_for_translate.include?(arg_item.to_s) } if langs
|
386
|
+
end
|
387
|
+
|
388
|
+
|
389
|
+
private_class_method :valid_language?
|
390
|
+
private_class_method :wrapper
|
391
|
+
|
392
|
+
end
|
393
|
+
|
394
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path('../lib/ms_translate/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Carlo Bertini"]
|
5
|
+
gem.email = ["waydotnet@gmail.com"]
|
6
|
+
gem.description = "MsTranslate is a wrapper for the Microsoft Translator API"
|
7
|
+
gem.summary = "Microsoft Translator API Wrapper for Ruby"
|
8
|
+
gem.homepage = "http://www.waydotnet.com"
|
9
|
+
|
10
|
+
gem.add_dependency('httparty')
|
11
|
+
|
12
|
+
gem.add_development_dependency 'webmock'
|
13
|
+
gem.add_development_dependency 'vcr'
|
14
|
+
gem.add_development_dependency 'turn'
|
15
|
+
gem.add_development_dependency 'rake'
|
16
|
+
|
17
|
+
|
18
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
gem.files = `git ls-files`.split("\n")
|
20
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
gem.name = "ms_translate"
|
22
|
+
gem.require_paths = ["lib"]
|
23
|
+
gem.version = MsTranslate::VERSION
|
24
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# require_relative '../../spec_helper'
|
2
|
+
# For Ruby < 1.9.3, use this instead of require_relative
|
3
|
+
require (File.expand_path('./../../../spec_helper', __FILE__))
|
4
|
+
|
5
|
+
describe MsTranslate::Api do
|
6
|
+
|
7
|
+
describe "default attributes" do
|
8
|
+
before do
|
9
|
+
MsTranslate::Api.appId = nil
|
10
|
+
@api_real = 'INSERT_YOUR_APPID'
|
11
|
+
end
|
12
|
+
it "must include httparty methods" do
|
13
|
+
MsTranslate::Api.must_include HTTParty
|
14
|
+
end
|
15
|
+
|
16
|
+
it "must have the base url set to MS Translator API endpoint" do
|
17
|
+
MsTranslate::Api.base_uri.must_equal 'http://api.microsofttranslator.com/V2/Http.svc/'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "must get/set appId attribute" do
|
21
|
+
MsTranslate::Api.appId.must_be_nil
|
22
|
+
MsTranslate::Api.appId = @api_real
|
23
|
+
MsTranslate::Api.appId.must_equal @api_real
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "GET MS translator" do
|
29
|
+
before do
|
30
|
+
VCR.insert_cassette __name__ , :record => :new_episodes
|
31
|
+
@api_real = 'INSERT_YOUR_APPID'
|
32
|
+
MsTranslate::Api.appId = @api_real
|
33
|
+
|
34
|
+
# make HTTP request in before
|
35
|
+
end
|
36
|
+
|
37
|
+
after do
|
38
|
+
# make HTTP request in after
|
39
|
+
MsTranslate::Api.reset!
|
40
|
+
VCR.eject_cassette
|
41
|
+
end
|
42
|
+
|
43
|
+
it "load list language available without appId code" do
|
44
|
+
MsTranslate::Api.appId = nil
|
45
|
+
MsTranslate::Api.get('GetLanguagesForTranslate')
|
46
|
+
end
|
47
|
+
|
48
|
+
it "load list language available with appId code" do
|
49
|
+
MsTranslate::Api.appId.must_equal @api_real
|
50
|
+
MsTranslate::Api.get('GetLanguagesForTranslate', :query => {
|
51
|
+
:appId => MsTranslate::Api.appId
|
52
|
+
})
|
53
|
+
end
|
54
|
+
it 'translate Ciao Mondo! from en to it' do
|
55
|
+
translate = MsTranslate::Api.translate('Hello World!', {:from => :en, :to => :it })
|
56
|
+
translate.must_equal 'Salve, mondo!'
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'translate Ciao Mondo! from it to fr' do
|
61
|
+
translate = MsTranslate::Api.translate('Ciao Mondo!',{:from => :it, :to => :fr})
|
62
|
+
translate.must_equal 'Salut tout le monde!'
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'translate Hello World! with default language' do
|
66
|
+
translate = MsTranslate::Api.translate('Ciao Mondo!')
|
67
|
+
translate.must_equal 'Ciao Mondo!'
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'must have a list language available' do
|
71
|
+
MsTranslate::Api.get_languages_for_translate
|
72
|
+
MsTranslate::Api.get_languages_for_translate.must_be_instance_of Array
|
73
|
+
MsTranslate::Api.get_languages_for_translate.must_equal ["ar", "bg", "ca", "zh-CHS", "zh-CHT", "cs", "da", "nl", "en", "et", "fi", "fr", "de", "el", "ht", "he", "hi", "mww", "hu", "id", "it", "ja", "ko", "lv", "lt", "no", "pl", "pt", "ro", "ru", "sk", "sl", "es", "sv", "th", "tr", "uk", "vi"]
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'translate Hello World! with invalid language' do
|
77
|
+
MsTranslate::Api.from = :ge
|
78
|
+
MsTranslate::Api.to = :xu
|
79
|
+
lambda { MsTranslate::Api.translate('Hello World') }.must_raise MsTranslate::Api::InvalidLanguage
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'translate Hello World! with invalid and short appId' do
|
83
|
+
MsTranslate::Api.appId = "invalid_appId"
|
84
|
+
lambda { MsTranslate::Api.translate('Hello World') }.must_raise MsTranslate::Api::InvalidAppId
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'translate Hello World! with invalid appId' do
|
88
|
+
MsTranslate::Api.appId = "invalid_appId_very_long_but_wrong"
|
89
|
+
lambda { MsTranslate::Api.translate('Hello World') }.must_raise MsTranslate::Api::ArgumentException
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'DETECT the language of selection of text' do
|
93
|
+
MsTranslate::Api.detect('Hello World!').must_equal 'en'
|
94
|
+
MsTranslate::Api.detect('Ciao Mondo!').must_equal 'it'
|
95
|
+
MsTranslate::Api.detect('Salut tout le monde!').must_equal 'fr'
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'Detects the language of an array of strings' do
|
99
|
+
lambda { MsTranslate::Api.detect_array(['Hello', 'World!"'] ) }.must_raise MsTranslate::Api::HTTPMethodNotAllowed
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'Get a list of the languages supported by the Translator Service *' do
|
103
|
+
lambda { MsTranslate::Api.get_language_names(:it ) }.must_raise MsTranslate::Api::HTTPMethodNotAllowed
|
104
|
+
MsTranslate::Api.get_language_names(:it, true).must_include 'Italiano'
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'Get a list of the language codes supported by the Translator Service for speech synthesis.' do
|
108
|
+
MsTranslate::Api.get_languages_for_speak.must_be_instance_of Array
|
109
|
+
MsTranslate::Api.get_languages_for_speak.include?('it').must_equal true
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'Get a list of the language codes supported by the Translator Service.' do
|
113
|
+
MsTranslate::Api.get_languages_for_translate.must_be_instance_of Array
|
114
|
+
MsTranslate::Api.get_languages_for_translate.include?('it').must_equal true
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'Get an array of alternative translations of the given text. ' do
|
118
|
+
lambda { MsTranslate::Api.get_translations("Hello World!", 10 ) }.must_raise MsTranslate::Api::HTTPMethodNotAllowed
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'Get am array of alternative translations of the passed array of text' do
|
122
|
+
lambda { MsTranslate::Api.get_translations_array(['Hello','world!'], 10) }.must_raise MsTranslate::Api::HTTPMethodNotAllowed
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'Get a string with a URL to a wave stream of the passed in text in desired language. ' do
|
126
|
+
MsTranslate::Api.speak('Hello World!', 'it').bytesize.must_equal 14006
|
127
|
+
MsTranslate::Api.speak('Hello World!', 'it').size.must_equal 14006
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'Translates an array of texts into another language.' do
|
131
|
+
lambda{ MsTranslate::Api.translate_array(['Hello','World!']) }.must_raise MsTranslate::Api::HTTPMethodNotAllowed
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'Get an array of sentence lengths for each sentence of the given text' do
|
135
|
+
MsTranslate::Api.break_sentences('Hello World!', 'en').must_equal '12'
|
136
|
+
MsTranslate::Api.break_sentences('Ciao Mondo! Visto che bella giornata?', 'it').must_equal ['12', '25']
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#we need the actual library file
|
2
|
+
# require_relative '../lib/ms_translate'
|
3
|
+
# For Ruby < 1.9.3, use this instead of require_relative
|
4
|
+
require(File.expand_path('../../lib/ms_translate', __FILE__))
|
5
|
+
|
6
|
+
#dependencies
|
7
|
+
require 'minitest/autorun'
|
8
|
+
require 'webmock/minitest'
|
9
|
+
require 'vcr'
|
10
|
+
require 'turn'
|
11
|
+
|
12
|
+
Turn.config do |c|
|
13
|
+
# :outline - turn's original case/test outline mode [default]
|
14
|
+
c.format = :outline
|
15
|
+
# turn on invoke/execute tracing, enable full backtrace
|
16
|
+
# c.trace = true
|
17
|
+
# use humanized test names (works only with :outline format)
|
18
|
+
c.natural = true
|
19
|
+
end
|
20
|
+
|
21
|
+
# VCR config
|
22
|
+
VCR.configure do |c|
|
23
|
+
c.cassette_library_dir = 'spec/fixtures/ms_translate_cassettes'
|
24
|
+
c.hook_into :webmock # or :fakeweb
|
25
|
+
c.allow_http_connections_when_no_cassette = false
|
26
|
+
c.default_cassette_options = {
|
27
|
+
:record => :once
|
28
|
+
}
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ms_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Carlo Bertini
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: &19578260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *19578260
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: webmock
|
27
|
+
requirement: &19577840 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *19577840
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: vcr
|
38
|
+
requirement: &19577380 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *19577380
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: turn
|
49
|
+
requirement: &19576940 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *19576940
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rake
|
60
|
+
requirement: &19576440 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *19576440
|
69
|
+
description: MsTranslate is a wrapper for the Microsoft Translator API
|
70
|
+
email:
|
71
|
+
- waydotnet@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- lib/ms_translate.rb
|
81
|
+
- lib/ms_translate/version.rb
|
82
|
+
- ms_translate.gemspec
|
83
|
+
- spec/lib/ms_translate/api_spec.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
homepage: http://www.waydotnet.com
|
86
|
+
licenses: []
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 1.8.15
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: Microsoft Translator API Wrapper for Ruby
|
109
|
+
test_files: []
|
110
|
+
has_rdoc:
|