yandex_translate 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/example.rb +15 -0
- data/lib/client.rb +27 -0
- data/lib/connect.rb +24 -0
- data/lib/error.rb +13 -0
- data/lib/url.rb +3 -0
- data/lib/yandex_translate/version.rb +3 -0
- data/lib/yandex_translate.rb +13 -0
- data/spec/yandex_translate_spec.rb +15 -0
- data/yandex_translate.gemspec +23 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1cb14055cd9e3514dab1642370f6e1c4804b185d
|
4
|
+
data.tar.gz: 8bc57c18b2b5d9288fd6a28be880d48765ef46f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e84cbb02a9db2cbff579ea9c0bc66c12f0c4e6e28946e55485b4424becda6f958649a138e27fe498a390be3cd27ec9cce902f1046827142ba7adae8d78a714d5
|
7
|
+
data.tar.gz: da39bf1de9a6e922266452433a7c85856b0177b73f34420965fb2804c4300e65a8ed668ef9717552ca5309921baca4d1178ab77f3f5c5948f4d674c723681351
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Makvik
|
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,31 @@
|
|
1
|
+
# YandexTranslate
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'yandex_translate'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install yandex_translate
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
See example.rb
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/yandex_translate/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/example.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative 'lib/yandex_translate.rb'
|
2
|
+
|
3
|
+
# Need Yandex Key
|
4
|
+
key = ""
|
5
|
+
|
6
|
+
yandex = YandexTranslate::Client.new(key)
|
7
|
+
|
8
|
+
puts "\nLanguage detection - \"Привет\"\n"
|
9
|
+
puts yandex.detect("Привет")
|
10
|
+
|
11
|
+
puts "\nLanguage translate - Я перевожу слово - Привет"
|
12
|
+
puts yandex.translate("Привет")
|
13
|
+
|
14
|
+
puts "\nAll Language"
|
15
|
+
puts yandex.get_langs
|
data/lib/client.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module YandexTranslate
|
2
|
+
class Client
|
3
|
+
def initialize(key)
|
4
|
+
@connect = Connect.new(key)
|
5
|
+
@langs = get_langs_update
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_langs
|
9
|
+
return @langs
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_langs_update(lang = 'en')
|
13
|
+
data = {"ui" => lang}
|
14
|
+
@langs = @connect.request('getLangs',data)
|
15
|
+
end
|
16
|
+
|
17
|
+
def detect(text)
|
18
|
+
data = {"text" => text}
|
19
|
+
return @connect.request('detect', data)
|
20
|
+
end
|
21
|
+
|
22
|
+
def translate(text, lang = 'ru', format = 'plain')
|
23
|
+
data = {"text" => text, "lang" => lang, "format" => format}
|
24
|
+
return @connect.request('translate', data)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/connect.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module YandexTranslate
|
2
|
+
class Connect
|
3
|
+
def initialize(key)
|
4
|
+
@key = key
|
5
|
+
end
|
6
|
+
|
7
|
+
def request(method, args = {})
|
8
|
+
url = URI.parse("#{BASE_URI}#{method}?key=#{@key}")
|
9
|
+
response = Net::HTTP.post_form(url, args).body
|
10
|
+
begin
|
11
|
+
response = JSON(response)
|
12
|
+
rescue
|
13
|
+
raise Error.new response
|
14
|
+
end
|
15
|
+
if response.kind_of?(Hash) && response['code']
|
16
|
+
code = response['code'].to_i
|
17
|
+
if ERROR_CODES.key?(code)
|
18
|
+
raise Error.new(ERROR_CODES[code])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
return response
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/error.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module YandexTranslate
|
2
|
+
ERROR_CODES = {
|
3
|
+
401 =>:ERR_KEY_INVALID,
|
4
|
+
402 =>:ERR_KEY_BLOCKED,
|
5
|
+
403 =>:ERR_DAILY_REQ_LIMIT_EXCEEDED,
|
6
|
+
404 =>:ERR_DAILY_CHAR_LIMIT_EXCEEDED,
|
7
|
+
413 =>:ERR_TEXT_TOO_LONG,
|
8
|
+
422 =>:ERR_UNPROCESSABLE_TEXT,
|
9
|
+
501 =>:ERR_LANG_NOT_SUPPORTED}
|
10
|
+
|
11
|
+
class Error < StandardError
|
12
|
+
end
|
13
|
+
end
|
data/lib/url.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative '../lib/yandex_translate.rb'
|
2
|
+
|
3
|
+
key = "trnsl.1.1.20150813T202035Z.0bf93fe64f14cffb.f22ba458304ebe91b9d659cc2a381c694edd9d74"
|
4
|
+
|
5
|
+
describe 'YandexTranslate' do
|
6
|
+
it 'should exists' do
|
7
|
+
YandexTranslate::Client.new(key)
|
8
|
+
end
|
9
|
+
it 'get_langs' do
|
10
|
+
YandexTranslate::Client.new(key).get_langs.values_at("dirs").should_not include(nil)
|
11
|
+
end
|
12
|
+
it 'translate' do
|
13
|
+
YandexTranslate::Client.new(key).translate("Привет").values_at("text").should_not include(nil)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yandex_translate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "yandex_translate"
|
8
|
+
spec.version = YandexTranslate::VERSION
|
9
|
+
spec.authors = ["Makvik"]
|
10
|
+
spec.email = ["storm.gman@mail.ru"]
|
11
|
+
spec.summary = ""
|
12
|
+
spec.description = ""
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yandex_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Makvik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- storm.gman@mail.ru
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- example.rb
|
55
|
+
- lib/client.rb
|
56
|
+
- lib/connect.rb
|
57
|
+
- lib/error.rb
|
58
|
+
- lib/url.rb
|
59
|
+
- lib/yandex_translate.rb
|
60
|
+
- lib/yandex_translate/version.rb
|
61
|
+
- spec/yandex_translate_spec.rb
|
62
|
+
- yandex_translate.gemspec
|
63
|
+
homepage: ''
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.2.2
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: ''
|
87
|
+
test_files:
|
88
|
+
- spec/yandex_translate_spec.rb
|