yandex-translator 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/lib/yandex-translator.rb +51 -0
- data/lib/yandex-translator/version.rb +5 -0
- data/yandex-translator.gemspec +21 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Artur Egorov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Yandex::Translator
|
2
|
+
|
3
|
+
Library for Yandex Translate API | Библиотека для API Яндекс.Переводчика
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'yandex-translator'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install yandex-translator
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
To detect language text use detect method:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
Yandex::Translator.detect('Hello, world!')
|
26
|
+
```
|
27
|
+
To translate text use translate method:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Yandex::Translator.translate('ru', 'Car')
|
31
|
+
```
|
32
|
+
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "yandex-translator/version"
|
2
|
+
require "httparty"
|
3
|
+
|
4
|
+
module Yandex
|
5
|
+
module Translator
|
6
|
+
|
7
|
+
class TranslationError < StandardError
|
8
|
+
end
|
9
|
+
|
10
|
+
class Translation
|
11
|
+
include HTTParty
|
12
|
+
base_uri 'http://translate.yandex.net/api/v1/tr/'
|
13
|
+
|
14
|
+
def detect_lang(text)
|
15
|
+
options = {}
|
16
|
+
options[:query] = { :text => text }
|
17
|
+
language = self.class.get("/detect", options)
|
18
|
+
language["DetectedLang"]["lang"]
|
19
|
+
end
|
20
|
+
|
21
|
+
def translate(text, language)
|
22
|
+
options = {}
|
23
|
+
options[:query] = { :lang => language, :text => text }
|
24
|
+
translation = self.class.get("/translate", options)
|
25
|
+
case translation["Translation"]["code"].to_i
|
26
|
+
when 200
|
27
|
+
translation["Translation"]["text"]
|
28
|
+
when 413
|
29
|
+
raise TranslationError.new("Text too long")
|
30
|
+
when 422
|
31
|
+
raise TranslationError.new("Can't translate text")
|
32
|
+
when 501
|
33
|
+
raise TranslationError.new("Can't translate to this language")
|
34
|
+
else
|
35
|
+
raise TranslationError.new("Try again later")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def self.detect(text = '')
|
43
|
+
Translation.new.detect_lang(text)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.translate(text = '', language)
|
47
|
+
Translation.new.translate(language, text)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yandex-translator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "yandex-translator"
|
8
|
+
gem.version = Yandex::Translator::VERSION
|
9
|
+
gem.authors = ["Artur Egorov"]
|
10
|
+
gem.email = ["artur@egorov.in"]
|
11
|
+
gem.description = %q{Library for Yandex Translate API}
|
12
|
+
gem.summary = %q{Yandex Translate API}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.add_dependency "httparty"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yandex-translator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Artur Egorov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: &70307109597480 !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: *70307109597480
|
25
|
+
description: Library for Yandex Translate API
|
26
|
+
email:
|
27
|
+
- artur@egorov.in
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/yandex-translator.rb
|
38
|
+
- lib/yandex-translator/version.rb
|
39
|
+
- yandex-translator.gemspec
|
40
|
+
homepage: ''
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.10
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Yandex Translate API
|
64
|
+
test_files: []
|