easy_translate 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/easy_translate.rb +7 -7
- data/lib/easy_translate/detection.rb +1 -1
- data/lib/easy_translate/translation.rb +39 -6
- data/lib/easy_translate/translation_target.rb +1 -1
- data/lib/easy_translate/version.rb +1 -1
- data/spec/spec_helper.rb +2 -2
- metadata +39 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 422e8ca1c75f8125002cba2383871ec8988d14c4
|
4
|
+
data.tar.gz: c9fca5ee01af683ea3d7d3decc7afd6423cc8909
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e8ba638b958afb464830b30f4b558ccb61d1f7e7d951fcea48c5cf1fc58651878bd9d645de4937ada57ce4ebfcf8eaf0ca1b1b5fe343df16048d9cdb9a6096d
|
7
|
+
data.tar.gz: dc2254431130de0d5f30a4e5c85f0e29b7289ffb6b17cc2920fadf5ffbffc1b5b1d9bb9e7c5282315738f842bb3b1a6e5464a1a4b372d663b12bd9067544d1dc
|
data/lib/easy_translate.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'easy_translate/detection'
|
2
|
+
require 'easy_translate/translation'
|
3
|
+
require 'easy_translate/translation_target'
|
4
4
|
|
5
5
|
module EasyTranslate
|
6
6
|
|
7
|
-
autoload :EasyTranslateException,
|
8
|
-
autoload :Request,
|
7
|
+
autoload :EasyTranslateException, 'easy_translate/easy_translate_exception'
|
8
|
+
autoload :Request, 'easy_translate/request'
|
9
9
|
|
10
|
-
autoload :LANGUAGES,
|
11
|
-
autoload :VERSION,
|
10
|
+
autoload :LANGUAGES, 'easy_translate/languages'
|
11
|
+
autoload :VERSION, 'easy_translate/version'
|
12
12
|
|
13
13
|
extend Detection # Language Detection
|
14
14
|
extend Translation # Language Translation
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'cgi'
|
3
|
-
require
|
3
|
+
require 'thread/pool'
|
4
|
+
require 'thread_safe'
|
5
|
+
require 'easy_translate/request'
|
4
6
|
|
5
7
|
module EasyTranslate
|
6
8
|
|
@@ -8,19 +10,49 @@ module EasyTranslate
|
|
8
10
|
|
9
11
|
# Translate text
|
10
12
|
# @param [String, Array] texts - A single string or set of strings to translate
|
13
|
+
# @option options [Fixnum] :batch_size - Maximum keys per request (optional, default 100)
|
14
|
+
# @option options [Fixnum] :concurrency - Maximum concurrent requests (optional, default 4)
|
11
15
|
# @option options [String, Symbol] :source - The source language (optional)
|
12
16
|
# @option options [String, Symbol] :target - The target language (required)
|
13
17
|
# @option options [Boolean] :html - Whether or not the supplied string is HTML (optional)
|
14
18
|
# @return [String, Array] Translated text or texts
|
15
19
|
def translate(texts, options = {}, http_options = {})
|
20
|
+
options = options.dup
|
21
|
+
batch_size = options.delete(:batch_size) || 100
|
22
|
+
concurrency = options.delete(:concurrency) || 4
|
23
|
+
batches = Array(texts).each_slice(batch_size).to_a
|
24
|
+
if concurrency > 1 && batches.size > 1
|
25
|
+
pool = Thread::Pool.new([concurrency, 1 + (texts.length - 1) / batch_size].min)
|
26
|
+
batch_results = ThreadSafe::Array.new
|
27
|
+
batches.each_with_index do |texts_batch, i|
|
28
|
+
pool.process { batch_results[i] = request_translations(texts_batch, options, http_options) }
|
29
|
+
end
|
30
|
+
pool.shutdown
|
31
|
+
results = batch_results.reduce(:+)
|
32
|
+
else
|
33
|
+
results = batches.map { |texts_batch| request_translations(texts_batch, options, http_options) }.reduce(:+)
|
34
|
+
end
|
35
|
+
# if they only asked for one, only give one back
|
36
|
+
texts.is_a?(String) ? results[0] : results
|
37
|
+
ensure
|
38
|
+
pool.shutdown! if pool && !pool.shutdown?
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
# Perform a single request to translate texts
|
44
|
+
# @param [Array] texts - Texts to translate
|
45
|
+
# @option options [String, Symbol] :source - The source language (optional)
|
46
|
+
# @option options [String, Symbol] :target - The target language (required)
|
47
|
+
# @option options [Boolean] :html - Whether or not the supplied string is HTML (optional)
|
48
|
+
# @return [String, Array] Translated text or texts
|
49
|
+
def request_translations(texts, options = {}, http_options = {})
|
16
50
|
request = TranslationRequest.new(texts, options, http_options)
|
17
51
|
# Turn the response into an array of translations
|
18
52
|
raw = request.perform_raw
|
19
|
-
|
53
|
+
JSON.parse(raw)['data']['translations'].map do |res|
|
20
54
|
res['translatedText']
|
21
55
|
end
|
22
|
-
# And then return, if they only asked for one, only give one back
|
23
|
-
request.multi? ? translations : translations.first
|
24
56
|
end
|
25
57
|
|
26
58
|
# A convenience class for wrapping a translation request
|
@@ -30,11 +62,12 @@ module EasyTranslate
|
|
30
62
|
# @param [String, Array] texts - the text (or texts) to translate
|
31
63
|
# @param [Hash] options - Options to override or pass along with the request
|
32
64
|
def initialize(texts, options, http_options = {})
|
65
|
+
options = options.dup
|
33
66
|
self.texts = texts
|
34
67
|
self.html = options.delete(:html)
|
35
68
|
@source = options.delete(:from)
|
36
69
|
@target = options.delete(:to)
|
37
|
-
raise ArgumentError.new('No target language
|
70
|
+
raise ArgumentError.new('No target language provided') unless @target
|
38
71
|
raise ArgumentError.new('Support for multiple targets dropped in V2') if @target.is_a?(Array)
|
39
72
|
@http_options = http_options
|
40
73
|
if options
|
@@ -48,7 +81,7 @@ module EasyTranslate
|
|
48
81
|
# The params for this request
|
49
82
|
# @return [Hash] the params for the request
|
50
83
|
def params
|
51
|
-
params
|
84
|
+
params = super || {}
|
52
85
|
params[:source] = lang(@source) unless @source.nil?
|
53
86
|
params[:target] = lang(@target) unless @target.nil?
|
54
87
|
params[:format] = @format unless @format.nil?
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,46 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.4.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- John Crepezzi
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-12-17 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: json
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thread
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: thread_safe
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
44
67
|
- !ruby/object:Gem::Version
|
45
68
|
version: '0'
|
46
69
|
description: easy_translate is a wrapper for the google translate API that makes sense
|
@@ -61,27 +84,26 @@ files:
|
|
61
84
|
- spec/spec_helper.rb
|
62
85
|
homepage: https://github.com/seejohnrun/easy_translate
|
63
86
|
licenses: []
|
87
|
+
metadata: {}
|
64
88
|
post_install_message:
|
65
89
|
rdoc_options: []
|
66
90
|
require_paths:
|
67
91
|
- lib
|
68
92
|
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
-
none: false
|
70
93
|
requirements:
|
71
|
-
- -
|
94
|
+
- - '>='
|
72
95
|
- !ruby/object:Gem::Version
|
73
96
|
version: '0'
|
74
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
-
none: false
|
76
98
|
requirements:
|
77
|
-
- -
|
99
|
+
- - '>='
|
78
100
|
- !ruby/object:Gem::Version
|
79
101
|
version: '0'
|
80
102
|
requirements: []
|
81
103
|
rubyforge_project:
|
82
|
-
rubygems_version:
|
104
|
+
rubygems_version: 2.0.6
|
83
105
|
signing_key:
|
84
|
-
specification_version:
|
106
|
+
specification_version: 4
|
85
107
|
summary: Google Translate API Wrapper for Ruby
|
86
108
|
test_files:
|
87
109
|
- spec/spec_helper.rb
|