et-wsm 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/easy_translate.rb +21 -0
- data/lib/easy_translate/detection.rb +82 -0
- data/lib/easy_translate/easy_translate_exception.rb +6 -0
- data/lib/easy_translate/languages.rb +58 -0
- data/lib/easy_translate/request.rb +105 -0
- data/lib/easy_translate/translation.rb +111 -0
- data/lib/easy_translate/translation_target.rb +47 -0
- data/lib/easy_translate/version.rb +5 -0
- data/spec/spec_helper.rb +11 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7220f40d497b857957df43eb0375f5ba6601b4f8
|
4
|
+
data.tar.gz: af7c20b017e370ff66e96149f93b78252e8adb84
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 162c831567d8f5bb24a2db513d595d711b0824dce6e67d69b84dcd4add420a0bde6e162af2055288e1e7aaa8ab993c6b4f0085895f13c9402c2b2536ee015beb
|
7
|
+
data.tar.gz: 4308eb1d2d1689facd6dc20a3abfad614a3db705a17048d1b84a16303b7763d0f2c7f31d96fd00caa2cfb025af1cc9547f938f3fedefa788cb02e983634dc62f
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/easy_translate/detection'
|
2
|
+
require File.dirname(__FILE__) + '/easy_translate/translation'
|
3
|
+
require File.dirname(__FILE__) + '/easy_translate/translation_target'
|
4
|
+
|
5
|
+
module EasyTranslate
|
6
|
+
|
7
|
+
autoload :EasyTranslateException, File.dirname(__FILE__) + '/easy_translate/easy_translate_exception'
|
8
|
+
autoload :Request, File.dirname(__FILE__) + '/easy_translate/request'
|
9
|
+
|
10
|
+
autoload :LANGUAGES, File.dirname(__FILE__) + '/easy_translate/languages'
|
11
|
+
autoload :VERSION, File.dirname(__FILE__) + '/easy_translate/version'
|
12
|
+
|
13
|
+
extend Detection # Language Detection
|
14
|
+
extend Translation # Language Translation
|
15
|
+
extend TranslationTarget # Language Translation Targets
|
16
|
+
|
17
|
+
class << self
|
18
|
+
attr_accessor :api_key
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'cgi'
|
3
|
+
require File.dirname(__FILE__) + '/request'
|
4
|
+
|
5
|
+
module EasyTranslate
|
6
|
+
|
7
|
+
module Detection
|
8
|
+
|
9
|
+
# Detect language
|
10
|
+
# @param [String, Array] texts - A single string or set of strings to detect for
|
11
|
+
# @param [Hash] options - Extra options to pass along with the request
|
12
|
+
# @return [String, Array] The resultant language or languages
|
13
|
+
def detect(texts, options = {}, http_options = {})
|
14
|
+
request = DetectionRequest.new(texts, options, http_options)
|
15
|
+
# Turn the response into an array of detections
|
16
|
+
raw = request.perform_raw
|
17
|
+
detections = JSON.parse(raw)['data']['detections'].map do |res|
|
18
|
+
res.empty? ? nil : res.first['language']
|
19
|
+
end
|
20
|
+
# And then return, if they only asked for one, only give one back
|
21
|
+
request.multi? ? detections : detections.first
|
22
|
+
end
|
23
|
+
|
24
|
+
# A convenience class for wrapping a detection request
|
25
|
+
class DetectionRequest < EasyTranslate::Request
|
26
|
+
|
27
|
+
# Set the texts and options
|
28
|
+
# @param [String, Array] texts - The text (or texts) to translate
|
29
|
+
# @param [Hash] options - Options to override or pass along with the request
|
30
|
+
def initialize(texts, options = {}, http_options = {})
|
31
|
+
super(options, http_options)
|
32
|
+
if replacement_api_key = @options.delete(:api_key)
|
33
|
+
@options[:key] = replacement_api_key
|
34
|
+
end
|
35
|
+
self.texts = texts
|
36
|
+
end
|
37
|
+
|
38
|
+
# The params for this request
|
39
|
+
# @return [Hash] the params for the request
|
40
|
+
def params
|
41
|
+
params = super || {}
|
42
|
+
params.merge! @options if @options
|
43
|
+
params
|
44
|
+
end
|
45
|
+
|
46
|
+
# The path for the request
|
47
|
+
# @return [String] The path for the request
|
48
|
+
def path
|
49
|
+
'/language/translate/v2/detect'
|
50
|
+
end
|
51
|
+
|
52
|
+
# The body for the request
|
53
|
+
# @return [String] the body for the request, URL escaped
|
54
|
+
def body
|
55
|
+
@texts.map { |t| "q=#{CGI::escape(t)}" }.join '&'
|
56
|
+
end
|
57
|
+
|
58
|
+
# Whether or not this was a request for multiple texts
|
59
|
+
# @return [Boolean]
|
60
|
+
def multi?
|
61
|
+
@multi
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# Set the texts for this request
|
67
|
+
# @param [String, Array] texts - The text or texts for this request
|
68
|
+
def texts=(texts)
|
69
|
+
if texts.is_a?(String)
|
70
|
+
@multi = false
|
71
|
+
@texts = [texts]
|
72
|
+
else
|
73
|
+
@multi = true
|
74
|
+
@texts = texts
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module EasyTranslate
|
2
|
+
|
3
|
+
LANGUAGES = {
|
4
|
+
'af' => 'afrikaans',
|
5
|
+
'sq' => 'albanian',
|
6
|
+
'ar' => 'arabic',
|
7
|
+
'be' => 'belarusian',
|
8
|
+
'bg' => 'bulgarian',
|
9
|
+
'ca' => 'catalan',
|
10
|
+
'zh-CN' => 'chinese_simplified',
|
11
|
+
'zh-TW' => 'chinese_traditional',
|
12
|
+
'hr' => 'croatian',
|
13
|
+
'cs' => 'czech',
|
14
|
+
'da' => 'danish',
|
15
|
+
'nl' => 'dutch',
|
16
|
+
'en' => 'english',
|
17
|
+
'et' => 'estonian',
|
18
|
+
'tl' => 'filipino',
|
19
|
+
'fi' => 'finnish',
|
20
|
+
'fr' => 'french',
|
21
|
+
'gl' => 'galician',
|
22
|
+
'de' => 'german',
|
23
|
+
'el' => 'greek',
|
24
|
+
'iw' => 'hebrew',
|
25
|
+
'hi' => 'hindi',
|
26
|
+
'hu' => 'hungarian',
|
27
|
+
'is' => 'icelandic',
|
28
|
+
'id' => 'indonesian',
|
29
|
+
'ga' => 'irish',
|
30
|
+
'it' => 'italian',
|
31
|
+
'ja' => 'japanese',
|
32
|
+
'ko' => 'korean',
|
33
|
+
'lv' => 'latvian',
|
34
|
+
'lt' => 'lithuanian',
|
35
|
+
'mk' => 'macedonian',
|
36
|
+
'ms' => 'malay',
|
37
|
+
'mt' => 'maltese',
|
38
|
+
'no' => 'norwegian',
|
39
|
+
'fa' => 'persian',
|
40
|
+
'pl' => 'polish',
|
41
|
+
'pt' => 'portuguese',
|
42
|
+
'ro' => 'romanian',
|
43
|
+
'ru' => 'russian',
|
44
|
+
'sr' => 'serbian',
|
45
|
+
'sk' => 'slovak',
|
46
|
+
'sl' => 'slovenian',
|
47
|
+
'es' => 'spanish',
|
48
|
+
'sw' => 'swahili',
|
49
|
+
'sv' => 'swedish',
|
50
|
+
'th' => 'thai',
|
51
|
+
'tr' => 'turkish',
|
52
|
+
'uk' => 'ukrainian',
|
53
|
+
'vi' => 'vietnamese',
|
54
|
+
'cy' => 'welsh',
|
55
|
+
'yi' => 'yiddish'
|
56
|
+
}
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module EasyTranslate
|
6
|
+
|
7
|
+
class Request
|
8
|
+
attr_accessor :http_options
|
9
|
+
|
10
|
+
def initialize(options = {}, http_options = {})
|
11
|
+
@options = options
|
12
|
+
@http_options = http_options
|
13
|
+
end
|
14
|
+
|
15
|
+
# Body, blank by default
|
16
|
+
# @return [String] The body for this request
|
17
|
+
def body
|
18
|
+
''
|
19
|
+
end
|
20
|
+
|
21
|
+
# The path for the request
|
22
|
+
# @return [String] The path for this request
|
23
|
+
def path
|
24
|
+
raise NotImplementedError.new('path is not implemented')
|
25
|
+
end
|
26
|
+
|
27
|
+
# The base params for a request
|
28
|
+
# @return [Hash] a hash of the base parameters for any request
|
29
|
+
def params
|
30
|
+
params = {}
|
31
|
+
params[:key] = EasyTranslate.api_key if EasyTranslate.api_key
|
32
|
+
params[:prettyPrint] = 'false' # eliminate unnecessary overhead
|
33
|
+
params
|
34
|
+
end
|
35
|
+
|
36
|
+
# Perform the given request
|
37
|
+
# @return [String] The response String
|
38
|
+
def perform_raw
|
39
|
+
# Construct the request
|
40
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
41
|
+
request.add_field('X-HTTP-Method-Override', 'GET')
|
42
|
+
request.body = body
|
43
|
+
# Fire and return
|
44
|
+
response = http.request(request)
|
45
|
+
unless response.code == '200'
|
46
|
+
err = JSON.parse(response.body)['error']['errors'].first['message']
|
47
|
+
raise EasyTranslateException.new(err)
|
48
|
+
end
|
49
|
+
response.body
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def uri
|
55
|
+
@uri ||= URI.parse("https://www.googleapis.com#{path}?#{param_s}")
|
56
|
+
end
|
57
|
+
|
58
|
+
def http
|
59
|
+
@http ||= Net::HTTP.new(uri.host, uri.port).tap do |http|
|
60
|
+
configure_timeouts(http)
|
61
|
+
configure_ssl(http)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def configure_timeouts(http)
|
66
|
+
http.read_timeout = http.open_timeout = http_options[:timeout] if http_options[:timeout]
|
67
|
+
http.open_timeout = http_options[:open_timeout] if http_options[:open_timeout]
|
68
|
+
end
|
69
|
+
|
70
|
+
def configure_ssl(http)
|
71
|
+
http.use_ssl = true
|
72
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
73
|
+
http.cert_store = ssl_cert_store
|
74
|
+
|
75
|
+
http.cert = ssl_options[:client_cert] if ssl_options[:client_cert]
|
76
|
+
http.key = ssl_options[:client_key] if ssl_options[:client_key]
|
77
|
+
http.ca_file = ssl_options[:ca_file] if ssl_options[:ca_file]
|
78
|
+
http.ca_path = ssl_options[:ca_path] if ssl_options[:ca_path]
|
79
|
+
http.verify_depth = ssl_options[:verify_depth] if ssl_options[:verify_depth]
|
80
|
+
http.ssl_version = ssl_options[:version] if ssl_options[:version]
|
81
|
+
end
|
82
|
+
|
83
|
+
def ssl_cert_store
|
84
|
+
return ssl_options[:cert_store] if ssl_options[:cert_store]
|
85
|
+
# Use the default cert store by default, i.e. system ca certs
|
86
|
+
cert_store = OpenSSL::X509::Store.new
|
87
|
+
cert_store.set_default_paths
|
88
|
+
cert_store
|
89
|
+
end
|
90
|
+
|
91
|
+
def ssl_options
|
92
|
+
http_options[:ssl] || {}
|
93
|
+
end
|
94
|
+
|
95
|
+
# Stringify the params
|
96
|
+
# @return [String] The params as a string
|
97
|
+
def param_s
|
98
|
+
params.map do |k, v|
|
99
|
+
"#{k}=#{v}" unless v.nil?
|
100
|
+
end.compact.join('&')
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'cgi'
|
3
|
+
require File.dirname(__FILE__) + '/request'
|
4
|
+
|
5
|
+
module EasyTranslate
|
6
|
+
|
7
|
+
module Translation
|
8
|
+
|
9
|
+
# Translate text
|
10
|
+
# @param [String, Array] texts - A single string or set of strings to translate
|
11
|
+
# @option options [String, Symbol] :source - The source language (optional)
|
12
|
+
# @option options [String, Symbol] :target - The target language (required)
|
13
|
+
# @option options [Boolean] :html - Whether or not the supplied string is HTML (optional)
|
14
|
+
# @return [String, Array] Translated text or texts
|
15
|
+
def translate(texts, options = {}, http_options = {})
|
16
|
+
request = TranslationRequest.new(texts, options, http_options)
|
17
|
+
# Turn the response into an array of translations
|
18
|
+
raw = request.perform_raw
|
19
|
+
translations = JSON.parse(raw)['data']['translations'].map do |res|
|
20
|
+
res['translatedText']
|
21
|
+
end
|
22
|
+
# And then return, if they only asked for one, only give one back
|
23
|
+
request.multi? ? translations : translations.first
|
24
|
+
end
|
25
|
+
|
26
|
+
# A convenience class for wrapping a translation request
|
27
|
+
class TranslationRequest < EasyTranslate::Request
|
28
|
+
|
29
|
+
# Set the texts and options
|
30
|
+
# @param [String, Array] texts - the text (or texts) to translate
|
31
|
+
# @param [Hash] options - Options to override or pass along with the request
|
32
|
+
def initialize(texts, options, http_options = {})
|
33
|
+
self.texts = texts
|
34
|
+
self.html = options.delete(:html)
|
35
|
+
@source = options.delete(:from)
|
36
|
+
@target = options.delete(:to)
|
37
|
+
raise ArgumentError.new('No target language provided') unless @target
|
38
|
+
raise ArgumentError.new('Support for multiple targets dropped in V2') if @target.is_a?(Array)
|
39
|
+
@http_options = http_options
|
40
|
+
if options
|
41
|
+
@options = options
|
42
|
+
if replacement_api_key = @options.delete(:api_key)
|
43
|
+
@options[:key] = replacement_api_key
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# The params for this request
|
49
|
+
# @return [Hash] the params for the request
|
50
|
+
def params
|
51
|
+
params = super || {}
|
52
|
+
params[:source] = lang(@source) unless @source.nil?
|
53
|
+
params[:target] = lang(@target) unless @target.nil?
|
54
|
+
params[:format] = @format unless @format.nil?
|
55
|
+
params.merge! @options if @options
|
56
|
+
params
|
57
|
+
end
|
58
|
+
|
59
|
+
# The path for the request
|
60
|
+
# @return [String] The path for the request
|
61
|
+
def path
|
62
|
+
'/language/translate/v2'
|
63
|
+
end
|
64
|
+
|
65
|
+
# The body for the request
|
66
|
+
# @return [String] the body for the request, URL escaped
|
67
|
+
def body
|
68
|
+
@texts.map { |t| "q=#{CGI::escape(t)}" }.join '&'
|
69
|
+
end
|
70
|
+
|
71
|
+
# Whether or not this was a request for multiple texts
|
72
|
+
# @return [Boolean]
|
73
|
+
def multi?
|
74
|
+
@multi
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
# Look up a language in the table (if needed)
|
80
|
+
def lang(orig)
|
81
|
+
look = orig.is_a?(String) ? orig : orig.to_s
|
82
|
+
return look if LANGUAGES[look] # shortcut iteration
|
83
|
+
if val = LANGUAGES.detect { |k, v| v == look }
|
84
|
+
return val.first
|
85
|
+
end
|
86
|
+
look
|
87
|
+
end
|
88
|
+
|
89
|
+
# Set the HTML attribute, if true add a format
|
90
|
+
# @param [Boolean] b - Whether or not the text supplied iS HTML
|
91
|
+
def html=(b)
|
92
|
+
@format = b ? 'html' : nil
|
93
|
+
end
|
94
|
+
|
95
|
+
# Set the texts for this request
|
96
|
+
# @param [String, Array] texts - The text or texts for this request
|
97
|
+
def texts=(texts)
|
98
|
+
if texts.is_a?(String)
|
99
|
+
@multi = false
|
100
|
+
@texts = [texts]
|
101
|
+
else
|
102
|
+
@multi = true
|
103
|
+
@texts = texts
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'json'
|
2
|
+
require File.dirname(__FILE__) + '/request'
|
3
|
+
|
4
|
+
module EasyTranslate
|
5
|
+
|
6
|
+
module TranslationTarget
|
7
|
+
|
8
|
+
# Determine what translations are available
|
9
|
+
# @param [String] source - The source language (optional)
|
10
|
+
# @param [Hash] options - extra options
|
11
|
+
# @return [Array] an array of strings representing languages
|
12
|
+
def translations_available(target = nil, options = {})
|
13
|
+
request = TranslationTargetRequest.new(target, options)
|
14
|
+
raw = request.perform_raw
|
15
|
+
JSON.parse(raw)['data']['languages'].map do |res|
|
16
|
+
res['language']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class TranslationTargetRequest < EasyTranslate::Request
|
21
|
+
|
22
|
+
def initialize(target = nil, options = nil)
|
23
|
+
super(options)
|
24
|
+
@target = target
|
25
|
+
if @options
|
26
|
+
if replacement_api_key = @options.delete(:api_key)
|
27
|
+
@options[:key] = replacement_api_key
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def params
|
33
|
+
params = super || {}
|
34
|
+
params[:target] = @target unless @target.nil?
|
35
|
+
params.merge! @options if @options
|
36
|
+
params
|
37
|
+
end
|
38
|
+
|
39
|
+
def path
|
40
|
+
'/language/translate/v2/languages'
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: et-wsm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Test Only
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: easy_translate is a wrapper for the google translate API that makes sense
|
42
|
+
programatically, and implements API keys
|
43
|
+
email: test@test_fake.edu
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/easy_translate.rb
|
49
|
+
- lib/easy_translate/detection.rb
|
50
|
+
- lib/easy_translate/easy_translate_exception.rb
|
51
|
+
- lib/easy_translate/languages.rb
|
52
|
+
- lib/easy_translate/request.rb
|
53
|
+
- lib/easy_translate/translation.rb
|
54
|
+
- lib/easy_translate/translation_target.rb
|
55
|
+
- lib/easy_translate/version.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
homepage: https://www.google.com
|
58
|
+
licenses: []
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.6.4
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Google Translate API Wrapper for Ruby
|
80
|
+
test_files:
|
81
|
+
- spec/spec_helper.rb
|