yandex_translator 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '0579b88ecb55af9950551a6a242d5aa77e9569ce'
4
- data.tar.gz: 1d97277044165f1af460a8733022ab3c016e91a3
3
+ metadata.gz: 86053b58b044ca2b8cb7e9afa2c392fe67fddb0d
4
+ data.tar.gz: 8162d1912471f0215c3c967779e2a27c59222893
5
5
  SHA512:
6
- metadata.gz: 446ef411d73d0a9039a98271476efb13a87d8f80b95c047f9d0996efa0b6f480afbb28de9338911e4bda260600a04bc5621aecda809674d40d802f14a4edc823
7
- data.tar.gz: 2838d3d2eefaeb45e0decbf54cf1bd2395890eb163610ad337ed012ad3f7bfda35c6d7bd6834e8bb44ea2cc89892e56916ec2573a9f0cf2d42381d0b4d677341
6
+ metadata.gz: 956e37fdd4adeb7af34673ad7659f4ea9e607cfe56df54cd09ea8b40ecfa170c8252e95b7a9be77f1e8cc63bce87649a8fa9646de922eab85dea9fbc83f3eafa
7
+ data.tar.gz: 351bada1a7ba9caa2551fa0cebddeb2cc66d7a6ff0d1c5e2ff511b7133468273c21119cc62ebca85bc9f59d7fb6be3b548d0b37235e41acec82514025d6dac1f
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .idea/
12
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yandex_translator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Eeeeevgen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # YandexTranslator::Translator
2
+
3
+ A library for translating text using Yandex Translate API version 1.5
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'yandex_translator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install yandex_translator
20
+
21
+ ## Usage
22
+
23
+ 1. Create translator object using your API key. You can obtain a key [here](https://tech.yandex.ru/keys/get/?service=trnsl).
24
+
25
+ ```ruby
26
+ translator = YandexTranslator::Translator.new(key)
27
+ ```
28
+
29
+ 2. To get the list of available translation directions and transcriptions of languages abbreviations use method **lang_list**:
30
+
31
+ ```ruby
32
+ translator.lang_list(text, hint=nil)
33
+ ```
34
+
35
+ 3. To get possible text languages use method **lang_detect**:
36
+
37
+ ```ruby
38
+ translator.lang_detect(text, hint=nil)
39
+ ```
40
+
41
+ 4. To translate text use method **translate**:
42
+
43
+ ```ruby
44
+ translator.translate(text, lang, format:nil, options:nil)
45
+ ```
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
50
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ require 'yard'
4
+
5
+ task :default => :test
6
+ RSpec::Core::RakeTask.new('spec')
7
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,3 @@
1
+ module YandexTranslator
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,79 @@
1
+ require "yandex_translator/version"
2
+
3
+ # A library for translating text using Yandex Translate API version 1.5
4
+ #
5
+ module YandexTranslator
6
+ require 'net/http'
7
+ require 'json'
8
+
9
+ class YandexError < StandardError; end
10
+
11
+ class Translator
12
+ attr_accessor :key, :detected
13
+ @@url_base = 'https://translate.yandex.net/api/v1.5/tr.json/'
14
+
15
+ # Returns the Translator object
16
+ #
17
+ def initialize(key)
18
+ @key = key
19
+ @detected = nil
20
+ end
21
+
22
+ # Returns the list of available translation pairs and
23
+ # If the _lang_ argument is set also returns transcriptions of languages abbreviations
24
+ #
25
+ def lang_list(lang=nil)
26
+ url = @@url_base + 'getLangs?' + 'key=' + @key
27
+ if lang then url += '&ui=' + lang end
28
+ url = URI(url)
29
+ res = JSON.parse(Net::HTTP.get(url))
30
+ check_errors(res)
31
+ return res
32
+ end
33
+
34
+ # Returns possible text languages
35
+ # The _hint_ argument defaults to *nil*, can be a string or an array of prefered languages
36
+ #
37
+ def lang_detect(text, hint=nil)
38
+ url = @@url_base + 'detect?key=' + @key + '&text=' + URI::encode(text)
39
+ if hint
40
+ url += '&hint='
41
+ if hint.is_a?(String) # parse hint if it is a string or array
42
+ url += hint
43
+ else
44
+ hint.each { |x| url += x + ','}
45
+ url = url[0..-2]
46
+ end
47
+ end
48
+ url = URI(url)
49
+ res = JSON.parse(Net::HTTP.get(url))
50
+ check_errors(res)
51
+ return res['lang']
52
+ end
53
+
54
+ # Return the translation of the _text_ argument
55
+ # _lang_ argument can be 2 types:
56
+ # * The pair of the languages "from-to" ('en-ru')
57
+ # * One destination language ('en')
58
+ # _format_ argument defaults to *nil*. Can be "plain" for plain text or "html" for HTMl marked text
59
+ # _options_ argument defaults to *nil*. Can be "1" to include to the response the autodetected language of the source text. You can obtain it by attribute *detected*
60
+ def translate(text, lang, format:nil, options:nil)
61
+ url = @@url_base + 'translate?' + 'key=' + @key + '&' + 'text=' + URI::encode(text) + '&' + 'lang=' + lang
62
+ if format then url += '&format=' + format end
63
+ if options then url += '&options=' + options.to_s end
64
+
65
+ url = URI(url)
66
+ res = JSON.parse(Net::HTTP.get(url))
67
+ check_errors(res)
68
+ if options == 1 then @detected = res['detected']['lang'] else @detected = nil end
69
+ return res['text']
70
+ end
71
+
72
+ def check_errors(res)
73
+ if res['code'] and res['code'] != 200
74
+ raise(YandexError , res['message'])
75
+ end
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,25 @@
1
+ # coding: 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 |spec|
7
+ spec.name = "yandex_translator"
8
+ spec.version = YandexTranslator::VERSION
9
+ spec.author = "Kozlov_Evgeny"
10
+ spec.email = ["eeeeevgen@gmail.com"]
11
+ spec.date = '2017-05-14'
12
+ spec.summary = "A library for translating text using Yandex Translate API"
13
+ spec.license = "MIT"
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.14"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "webmock"
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kozlov_Evgeny
@@ -72,7 +72,15 @@ email:
72
72
  executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
- files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/yandex_translator.rb
82
+ - lib/yandex_translator/version.rb
83
+ - yandex_translator.gemspec
76
84
  homepage:
77
85
  licenses:
78
86
  - MIT