yat 0.0.3 → 0.0.4
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 +4 -4
- data/bin/yat +80 -32
- data/lib/yat.rb +1 -1
- data/lib/yat/config.rb +19 -6
- data/lib/yat/errors.rb +2 -0
- data/lib/yat/generic_translator.rb +65 -0
- data/lib/yat/json_translator.rb +33 -0
- data/lib/yat/xml_translator.rb +59 -0
- metadata +6 -4
- data/lib/yat/translator.rb +0 -172
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4e9cfdb16b8ab275ceff26d62dfcfb194862c84
|
4
|
+
data.tar.gz: cb74a19f8be0afe8980dcafa991dab706b90ac4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a6350dd4f6341b648dbdfb2bee403f87050bcb97f42db42b26a3aeb3cb7b8f79c2514c6c74d2597492df58e431f8d087e31b6dd6906113c4500094793b040cd
|
7
|
+
data.tar.gz: 41abbaef831db0abe1f688807684703dc999e63c354fc67a2ea50c47dd28ecf90884c90bf57f1cad92a301307a236952136256f5bd9d5ecd6ebfd09d33fa3b52
|
data/bin/yat
CHANGED
@@ -1,44 +1,53 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# -*- encoding: utf-8 -*-
|
3
2
|
require_relative "../lib/yat.rb"
|
4
3
|
|
4
|
+
require 'yaml'
|
5
5
|
require 'getoptlong'
|
6
6
|
|
7
|
-
|
7
|
+
CHUNK_SIZE = 10000
|
8
8
|
|
9
9
|
$EXIT_OK = 0
|
10
|
-
$
|
11
|
-
$
|
12
|
-
|
13
|
-
raise "provide api key in lib/config.rb file" if YandexTranslator::Options[:key].nil?
|
10
|
+
$EXIT_ECMDLINE = 3
|
11
|
+
$EXIT_EFAIL = 4
|
14
12
|
|
15
13
|
def show_help
|
16
14
|
print File.basename(__FILE__, '.rb')
|
17
15
|
puts <<-HELP
|
18
|
-
[OPTIONS] file1 .. fileN
|
16
|
+
--to LANGUAGE [OPTIONS] [file1 .. fileN]
|
19
17
|
|
20
18
|
OPTIONS:
|
21
|
-
--help
|
19
|
+
--help print this out
|
22
20
|
-h
|
23
21
|
|
24
|
-
--
|
22
|
+
--quiet turns off additional output
|
23
|
+
|
24
|
+
--to LANGUAGE LANGUAGE to translate to (e.g. "ru")
|
25
|
+
|
26
|
+
--list-languages prints list of supported languages
|
27
|
+
-l --to can be specified to get languages translations
|
28
|
+
|
29
|
+
--key KEY yandex translator api key
|
30
|
+
-k KEY
|
31
|
+
|
32
|
+
--make-config [FILE] generate YAML config
|
33
|
+
-m [FILE] if FILE not given, but -c option provided, it's MissingArgument
|
34
|
+
used instead
|
25
35
|
|
26
|
-
--
|
27
|
-
-
|
36
|
+
--config FILE read in config from FILE
|
37
|
+
-c FILE
|
38
|
+
|
39
|
+
Files are optional: if none given text grabbed from standard input.
|
40
|
+
Text can be provided through piped input (e.g. `echo "test" | yat --to af`)
|
28
41
|
|
29
42
|
NOTICE:
|
30
43
|
LANGUAGE must be one of https://translate.yandex.ru supported languages
|
31
|
-
abbreviations.
|
44
|
+
abbreviations. See --list-languages option.
|
32
45
|
|
33
46
|
HELP
|
34
|
-
end
|
35
47
|
|
36
|
-
|
48
|
+
end
|
37
49
|
|
38
|
-
|
39
|
-
"list_languages: #{__FILE__}: #{__LINE__}" \
|
40
|
-
"wrong argument: #{translator}" \
|
41
|
-
unless translator.kind_of? YandexTranslator::GenericTranslator
|
50
|
+
def list_languages(translator, quiet, to_lang)
|
42
51
|
|
43
52
|
res = translator.getLangs(ui: to_lang || 'en')
|
44
53
|
|
@@ -61,18 +70,32 @@ begin
|
|
61
70
|
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
62
71
|
[ '--to', GetoptLong::REQUIRED_ARGUMENT],
|
63
72
|
[ '--list-languages', '-l', GetoptLong::NO_ARGUMENT],
|
64
|
-
[ '--quiet', '-q', GetoptLong::NO_ARGUMENT]
|
73
|
+
[ '--quiet', '-q', GetoptLong::NO_ARGUMENT],
|
74
|
+
[ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
|
75
|
+
[ '--make-config', '-m', GetoptLong::OPTIONAL_ARGUMENT],
|
76
|
+
[ '--config', '-c', GetoptLong::REQUIRED_ARGUMENT]
|
65
77
|
)
|
66
78
|
|
67
79
|
to_lang = nil
|
68
80
|
list_languages = false
|
69
81
|
quiet = false
|
82
|
+
key = nil
|
83
|
+
make_config = false
|
84
|
+
config_file = nil
|
85
|
+
new_config = nil
|
70
86
|
|
71
87
|
opts.each { |opt, arg|
|
72
88
|
case opt
|
73
89
|
when '--help'
|
74
90
|
show_help
|
75
91
|
exit($EXIT_OK)
|
92
|
+
when '--key'
|
93
|
+
key = arg
|
94
|
+
when '--make-config'
|
95
|
+
make_config = true
|
96
|
+
new_config = arg
|
97
|
+
when '--config'
|
98
|
+
config_file = arg
|
76
99
|
when '--list-languages'
|
77
100
|
list_languages = true
|
78
101
|
when '--to'
|
@@ -82,20 +105,36 @@ begin
|
|
82
105
|
end
|
83
106
|
}
|
84
107
|
|
85
|
-
|
86
|
-
|
108
|
+
unless config_file.nil?
|
109
|
+
YandexTranslator.configuration = YAML.load(File.read(config_file))
|
110
|
+
YandexTranslator.configuration.api_key = key unless key.nil?
|
111
|
+
else
|
112
|
+
YandexTranslator.configure.api_key = key
|
113
|
+
end
|
114
|
+
|
115
|
+
if make_config
|
116
|
+
Dir.mkdir(new_config_dir) unless new_config.nil? || Dir.exist?(new_config_dir = File.dirname(new_config))
|
117
|
+
new_config == '-' || new_config = config_file
|
118
|
+
File.open(new_config || config_file, "w") { |file|
|
119
|
+
file.write(YAML.dump(YandexTranslator.configuration))
|
120
|
+
}
|
87
121
|
end
|
88
122
|
|
89
123
|
translator = YandexTranslator::JSONTranslator.new
|
90
124
|
|
91
125
|
if list_languages
|
92
|
-
list_languages(translator, quiet)
|
126
|
+
list_languages(translator, quiet, to_lang)
|
93
127
|
end
|
94
128
|
|
129
|
+
if to_lang.nil?
|
130
|
+
raise GetoptLong::MissingArgument, "Please, specify language for output"
|
131
|
+
end
|
132
|
+
|
95
133
|
str = String.new
|
134
|
+
|
96
135
|
ARGF.each_line do |line|
|
97
|
-
while str.length + line.length >
|
98
|
-
str, line = str + line[0,
|
136
|
+
while str.length + line.length > CHUNK_SIZE
|
137
|
+
str, line = str + line[0, CHUNK_SIZE], line[CHUNK_SIZE - str.length, line.length]
|
99
138
|
$stdout << translator.translate(text: str, lang: to_lang)
|
100
139
|
str.clear
|
101
140
|
end
|
@@ -103,15 +142,24 @@ begin
|
|
103
142
|
end
|
104
143
|
$stdout << translator.translate(text: str, lang: to_lang) unless str.empty?
|
105
144
|
|
106
|
-
rescue GetoptLong::MissingArgument
|
107
|
-
|
108
|
-
puts exc.message
|
109
|
-
puts
|
145
|
+
rescue GetoptLong::MissingArgument, GetoptLong::InvalidOption
|
146
|
+
$stderr.puts
|
110
147
|
show_help
|
111
|
-
exit($
|
148
|
+
exit($EXIT_ECMDLINE)
|
112
149
|
|
113
150
|
rescue YandexTranslator::ReturnCodeException => exc
|
114
|
-
|
115
|
-
|
116
|
-
|
151
|
+
$stderr.puts "Error: #{exc.message}"
|
152
|
+
exit($EXIT_EFAIL)
|
153
|
+
|
154
|
+
rescue YandexTranslator::ApiFunctionNotSupported => exc
|
155
|
+
$stderr.puts "Error: #{exc.message}"
|
156
|
+
exit($EXIT_EFAIL)
|
157
|
+
|
158
|
+
rescue YandexTranslator::NoApiKey => exc
|
159
|
+
$stderr.puts "Error: #{exc.message}"
|
160
|
+
exit($EXIT_EFAIL)
|
161
|
+
|
162
|
+
rescue Errno::ENOENT => exc
|
163
|
+
$stderr.puts exc.message
|
164
|
+
exit($EXIT_ECMDLINE)
|
117
165
|
end
|
data/lib/yat.rb
CHANGED
data/lib/yat/config.rb
CHANGED
@@ -1,8 +1,21 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
1
|
module YandexTranslator
|
3
|
-
|
4
|
-
:
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
class << self
|
3
|
+
attr_accessor :configuration
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.configure
|
7
|
+
self.configuration ||= Configuration.new
|
8
|
+
yield(self.configuration) rescue self.configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
class Configuration
|
12
|
+
attr_accessor :api_key, :xml_path, :json_path
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@api_key = nil
|
16
|
+
@json_path = 'https://translate.yandex.net/api/v1.5/tr.json'
|
17
|
+
@xml_path = 'https://translate.yandex.net/api/v1.5/tr'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
8
21
|
end
|
data/lib/yat/errors.rb
CHANGED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module YandexTranslator
|
4
|
+
|
5
|
+
class GenericTranslator
|
6
|
+
attr_reader :response
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@response = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def method_missing(api_function, *args)
|
14
|
+
@response = make_request(api_function, *args)
|
15
|
+
|
16
|
+
if @response.code == '404'
|
17
|
+
raise YandexTranslator::ApiFunctionNotSupported, "#{api_function}"
|
18
|
+
else
|
19
|
+
GenericTranslator.send(:define_method, api_function) do |*args|
|
20
|
+
make_request(__method__, *args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
@response
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
def make_request(function, format, params = nil)
|
28
|
+
raise ArgumentError, "wrong format: #{format}" if YandexTranslator::configuration.instance_eval("#{format}_path").nil?
|
29
|
+
|
30
|
+
raise NoApiKey, "provide api key" if (key = YandexTranslator::configuration.api_key).nil?
|
31
|
+
|
32
|
+
params[:key] = key
|
33
|
+
|
34
|
+
uri = URI(YandexTranslator::configuration.instance_eval("#{format}_path"))
|
35
|
+
uri.path += "/#{function}"
|
36
|
+
|
37
|
+
Net::HTTP.start(uri.host, uri.port,
|
38
|
+
:use_ssl => uri.scheme == 'https') do |https|
|
39
|
+
request = Net::HTTP::Post.new(uri)
|
40
|
+
@response = https.request(request, URI.encode_www_form(params))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def error_check(return_code)
|
45
|
+
unless return_code.nil? || return_code == 200
|
46
|
+
raise YandexTranslator::ReturnCodeException, case return_code.to_i
|
47
|
+
when 401
|
48
|
+
"401: wrong api key"
|
49
|
+
when 402
|
50
|
+
"402: api key blocked"
|
51
|
+
when 403
|
52
|
+
"403: daily request limit exceeded"
|
53
|
+
when 404
|
54
|
+
"404: daily text volume limit exceeded"
|
55
|
+
when 413
|
56
|
+
"413: text size too big"
|
57
|
+
when 422
|
58
|
+
"422: unable to translate"
|
59
|
+
when 501
|
60
|
+
"501: translation direction is not supported"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end # class GenericTranslator
|
65
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module YandexTranslator
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class JSONTranslator < GenericTranslator
|
5
|
+
|
6
|
+
def getLangs(params = nil)
|
7
|
+
response = super(:json, params)
|
8
|
+
response = JSON.parse(response.body)
|
9
|
+
error_check(response["code"])
|
10
|
+
response
|
11
|
+
end
|
12
|
+
|
13
|
+
def detect(params)
|
14
|
+
raise ArgumentError, "text parameter not specified" unless params.has_key?(:text)
|
15
|
+
|
16
|
+
response = super(:json, params)
|
17
|
+
response = JSON.parse(response.body)
|
18
|
+
error_check(response["code"])
|
19
|
+
response["lang"]
|
20
|
+
end
|
21
|
+
|
22
|
+
def translate(params)
|
23
|
+
raise ArgumentError, "text parameter not specified" unless params.has_key?(:text)
|
24
|
+
raise ArgumentError, "lang parameter not specified" unless params.has_key?(:lang)
|
25
|
+
|
26
|
+
response = super(:json, params)
|
27
|
+
response = JSON.parse(response.body)
|
28
|
+
error_check(response["code"])
|
29
|
+
return response["text"].inject {|elm, sum| elm + sum} if response["text"].is_a? Array
|
30
|
+
response["text"]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module YandexTranslator
|
2
|
+
require 'rexml/document'
|
3
|
+
include REXML
|
4
|
+
|
5
|
+
class XMLTranslator < GenericTranslator
|
6
|
+
|
7
|
+
def getLangs(params = nil)
|
8
|
+
|
9
|
+
response = super(:xml, params)
|
10
|
+
|
11
|
+
xmldoc = REXML::Document.new(response.body)
|
12
|
+
|
13
|
+
if error = xmldoc.elements["Error"]
|
14
|
+
error_check(error.attributes['code'])
|
15
|
+
end
|
16
|
+
|
17
|
+
response = Hash.new
|
18
|
+
response["dirs"] = xmldoc.elements["Langs/dirs"].collect { |e|
|
19
|
+
e.text
|
20
|
+
}
|
21
|
+
unless (langs = xmldoc.elements["Langs/langs"]).nil?
|
22
|
+
response["langs"] = Hash.new
|
23
|
+
langs.each {|e|
|
24
|
+
response["langs"][e.attributes["key"]] = e.attributes["value"]
|
25
|
+
}
|
26
|
+
end
|
27
|
+
response
|
28
|
+
end
|
29
|
+
|
30
|
+
def detect(params)
|
31
|
+
|
32
|
+
raise ArgumentError, "text parameter not specified" unless params.has_key?(:text)
|
33
|
+
|
34
|
+
response = super(:xml, params)
|
35
|
+
xmldoc = REXML::Document.new(response.body)
|
36
|
+
|
37
|
+
if error = xmldoc.elements["Error"]
|
38
|
+
error_check(error.attributes['code'])
|
39
|
+
end
|
40
|
+
|
41
|
+
xmldoc.root.attributes["lang"]
|
42
|
+
end
|
43
|
+
|
44
|
+
def translate(params)
|
45
|
+
|
46
|
+
raise ArgumentError, "text parameter not specified" unless params.has_key?(:text)
|
47
|
+
raise ArgumentError, "lang parameter not specified" unless params.has_key?(:lang)
|
48
|
+
|
49
|
+
response = super(:xml, params)
|
50
|
+
xmldoc = REXML::Document.new(response.body)
|
51
|
+
|
52
|
+
if error = xmldoc.elements["Error"]
|
53
|
+
error_check(error.attributes['code'])
|
54
|
+
end
|
55
|
+
|
56
|
+
xmldoc.elements["Translation/text"].text
|
57
|
+
end
|
58
|
+
end # class XMLTranslator
|
59
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- enoch0x5a
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: translates text files using yandex translator api
|
14
14
|
email:
|
@@ -21,7 +21,9 @@ files:
|
|
21
21
|
- lib/yat.rb
|
22
22
|
- lib/yat/config.rb
|
23
23
|
- lib/yat/errors.rb
|
24
|
-
- lib/yat/
|
24
|
+
- lib/yat/generic_translator.rb
|
25
|
+
- lib/yat/json_translator.rb
|
26
|
+
- lib/yat/xml_translator.rb
|
25
27
|
homepage:
|
26
28
|
licenses:
|
27
29
|
- MIT
|
@@ -42,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
44
|
version: '0'
|
43
45
|
requirements: []
|
44
46
|
rubyforge_project:
|
45
|
-
rubygems_version: 2.
|
47
|
+
rubygems_version: 2.4.5
|
46
48
|
signing_key:
|
47
49
|
specification_version: 4
|
48
50
|
summary: yet another yandex translator
|
data/lib/yat/translator.rb
DELETED
@@ -1,172 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require 'net/http'
|
3
|
-
|
4
|
-
module YandexTranslator
|
5
|
-
FORMAT_ARRAY = [:xml, :json]
|
6
|
-
|
7
|
-
class GenericTranslator
|
8
|
-
attr_reader :response
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
require_relative 'config.rb'
|
12
|
-
end
|
13
|
-
|
14
|
-
def getLangs(return_as, params)
|
15
|
-
response = make_request(:getLangs, return_as, params)
|
16
|
-
@response = response.body
|
17
|
-
response
|
18
|
-
end
|
19
|
-
|
20
|
-
def detect(return_as, params)
|
21
|
-
response = make_request(:detect, return_as, params)
|
22
|
-
@response = response.body
|
23
|
-
response
|
24
|
-
end
|
25
|
-
|
26
|
-
def translate(return_as, params)
|
27
|
-
response = make_request(:translate, return_as, params)
|
28
|
-
@response = response.body
|
29
|
-
response
|
30
|
-
end
|
31
|
-
|
32
|
-
protected
|
33
|
-
def make_request(function, format, params)
|
34
|
-
if not FORMAT_ARRAY.member? format
|
35
|
-
raise ArgumentError, "wrong format: #{format}"
|
36
|
-
end
|
37
|
-
key_hash = {:key => YandexTranslator::Options[:key]}
|
38
|
-
params = params.nil? ? key_hash : key_hash.merge(params)
|
39
|
-
uri = URI(Options["#{format}_path".to_sym])
|
40
|
-
uri.path += "/#{function}"
|
41
|
-
uri.query = URI.encode_www_form(params)
|
42
|
-
Net::HTTP.start(uri.host, uri.port,
|
43
|
-
:use_ssl => uri.scheme == 'https') do |https|
|
44
|
-
request = Net::HTTP::Post.new(uri)
|
45
|
-
response = https.request(request)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def error_check(return_code)
|
50
|
-
return_code = return_code.to_i if return_code.respond_to? :to_i
|
51
|
-
unless return_code.nil? && return_code == 200 #getLangs won't return 200:OK for now :_(
|
52
|
-
raise YandexTranslator::ReturnCodeException, case return_code
|
53
|
-
when 401
|
54
|
-
"401: wrong api key"
|
55
|
-
when 402
|
56
|
-
"402: api key blocked"
|
57
|
-
when 403
|
58
|
-
"403: daily request limit exceeded"
|
59
|
-
when 404
|
60
|
-
"404: daily text volume limit exceeded"
|
61
|
-
when 413
|
62
|
-
"413: text size too big"
|
63
|
-
when 422
|
64
|
-
"422: unable to translate"
|
65
|
-
when 501
|
66
|
-
"501: translation direction is not supported"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
end # class GenericTranslator
|
72
|
-
|
73
|
-
require 'json'
|
74
|
-
|
75
|
-
class JSONTranslator < GenericTranslator
|
76
|
-
|
77
|
-
def getLangs(params = nil)
|
78
|
-
response = super(:json, params)
|
79
|
-
response = JSON.parse(response.body)
|
80
|
-
error_check(response["code"])
|
81
|
-
response
|
82
|
-
end
|
83
|
-
|
84
|
-
def detect(params)
|
85
|
-
raise ArgumentError, "text parameter not specified" unless params.has_key?(:text)
|
86
|
-
|
87
|
-
response = super(:json, params)
|
88
|
-
response = JSON.parse(response.body)
|
89
|
-
error_check(response["code"])
|
90
|
-
response["lang"]
|
91
|
-
end
|
92
|
-
|
93
|
-
def translate(params)
|
94
|
-
raise ArgumentError, "text parameter not specified" unless params.has_key?(:text)
|
95
|
-
raise ArgumentError, "lang parameter not specified" unless params.has_key?(:lang)
|
96
|
-
|
97
|
-
response = super(:json, params)
|
98
|
-
response = JSON.parse(response.body)
|
99
|
-
error_check(response["code"])
|
100
|
-
response["text"]
|
101
|
-
end
|
102
|
-
end # class JSONTranslator
|
103
|
-
|
104
|
-
require 'rexml/document'
|
105
|
-
include REXML
|
106
|
-
|
107
|
-
class XMLTranslator < GenericTranslator
|
108
|
-
|
109
|
-
def getLangs(params = nil)
|
110
|
-
|
111
|
-
response = super(:xml, params)
|
112
|
-
|
113
|
-
xmldoc = REXML::Document.new(response.body)
|
114
|
-
|
115
|
-
if error = xmldoc.elements["Error"]
|
116
|
-
error_check(error.attributes['code'])
|
117
|
-
end
|
118
|
-
|
119
|
-
response = Hash.new
|
120
|
-
response["dirs"] = xmldoc.elements["Langs/dirs"].collect { |e|
|
121
|
-
e.text
|
122
|
-
}
|
123
|
-
unless (langs = xmldoc.elements["Langs/langs"]).nil?
|
124
|
-
response["langs"] = Hash.new
|
125
|
-
langs.each {|e|
|
126
|
-
response["langs"][e.attributes["key"]] = e.attributes["value"]
|
127
|
-
}
|
128
|
-
end
|
129
|
-
response
|
130
|
-
end
|
131
|
-
|
132
|
-
def detect(params)
|
133
|
-
|
134
|
-
raise ArgumentError, "text parameter not specified" unless params.has_key?(:text)
|
135
|
-
|
136
|
-
response = super(:xml, params)
|
137
|
-
xmldoc = REXML::Document.new(response.body)
|
138
|
-
|
139
|
-
if error = xmldoc.elements["Error"]
|
140
|
-
error_check(error.attributes['code'])
|
141
|
-
end
|
142
|
-
|
143
|
-
# response = Hash.new
|
144
|
-
# response["code"] = xmldoc.root.attributes["code"]
|
145
|
-
# response["lang"] = xmldoc.root.attributes["lang"]
|
146
|
-
|
147
|
-
# response
|
148
|
-
xmldoc.root.attributes["lang"]
|
149
|
-
end
|
150
|
-
|
151
|
-
def translate(params)
|
152
|
-
|
153
|
-
raise ArgumentError, "text parameter not specified" unless params.has_key?(:text)
|
154
|
-
raise ArgumentError, "lang parameter not specified" unless params.has_key?(:lang)
|
155
|
-
|
156
|
-
response = super(:xml, params)
|
157
|
-
xmldoc = REXML::Document.new(response.body)
|
158
|
-
|
159
|
-
if error = xmldoc.elements["Error"]
|
160
|
-
error_check(error.attributes['code'])
|
161
|
-
end
|
162
|
-
|
163
|
-
# response = Hash.new
|
164
|
-
# response["code"] = xmldoc.root.attributes["code"]
|
165
|
-
# response["lang"] = xmldoc.root.attributes["lang"]
|
166
|
-
# response["text"] = xmldoc.elements["Translation/text"].text
|
167
|
-
xmldoc.elements["Translation/text"].text
|
168
|
-
|
169
|
-
# response
|
170
|
-
end
|
171
|
-
end # class XMLTranslator
|
172
|
-
end # module YandexTranslator
|