y-translate 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/y/translate/api.rb +48 -0
- data/lib/y/translate/configuration.rb +15 -0
- data/lib/y/translate/error.rb +23 -0
- data/lib/y/translate/version.rb +5 -0
- data/lib/y/translate.rb +53 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f84446310cb02365ebe8a3b3345dd87b85fe2291
|
4
|
+
data.tar.gz: 6f24c386fcd2b6da0d509c027d8563be69526a80
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3da430286dcbb65cb6583e7ccdfe61fe83c8da940be2f77aa3933ce958a5a1caf30a653f4aab1a91c777bf8edec5f0a1f0bdacd362ab2246a1258179f9eb147
|
7
|
+
data.tar.gz: 94aa34f71cc3e2d8bda1b97a9cb077acd4b4bb5d296739ef022e85f9e56a48d549217760e399f05a23043c531bce5fc4403ee08430b7b3f0391c6c6eb28d7d4c
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'y/translate/error'
|
2
|
+
require 'http'
|
3
|
+
|
4
|
+
module Y
|
5
|
+
module Translate
|
6
|
+
class API
|
7
|
+
BASE_URI = 'https://translate.yandex.net/api/v1.5/tr.json'.freeze
|
8
|
+
|
9
|
+
METHOD_GET_LANGS = 'getLangs'.freeze
|
10
|
+
METHOD_DETECT = 'detect'.freeze
|
11
|
+
METHOD_TRANSLATE = 'translate'.freeze
|
12
|
+
|
13
|
+
attr_accessor :key
|
14
|
+
|
15
|
+
def initialize(key)
|
16
|
+
@key = key
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
attr_accessor :api
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.key=(key)
|
24
|
+
@api = new(key)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.key
|
28
|
+
api.key
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.request(type, method, params = {})
|
32
|
+
url = URI.parse("#{BASE_URI}/#{method}")
|
33
|
+
params[:key] = key
|
34
|
+
|
35
|
+
begin
|
36
|
+
response = HTTP.send(type, url, params: params)
|
37
|
+
rescue
|
38
|
+
raise Error.new
|
39
|
+
end
|
40
|
+
|
41
|
+
code = response.code.to_i
|
42
|
+
raise Error.new(code) if Error::ERROR_CODES.key?(code)
|
43
|
+
|
44
|
+
response.parse
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Y
|
2
|
+
module Translate
|
3
|
+
class Configuration
|
4
|
+
DEFAULT_LANG_FROM = 'en'.freeze
|
5
|
+
DEFAULT_LANG_TO = 'ru'.freeze
|
6
|
+
|
7
|
+
attr_accessor :api_key, :lang_from, :lang_to
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@lang_from = DEFAULT_LANG_FROM
|
11
|
+
@lang_to = DEFAULT_LANG_TO
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Y
|
2
|
+
module Translate
|
3
|
+
class Error < StandardError
|
4
|
+
ERROR_CODES = {
|
5
|
+
default: 'Неизвестная ошибка запроса',
|
6
|
+
401 => 'Неправильный API-ключ',
|
7
|
+
402 => 'API-ключ заблокирован',
|
8
|
+
404 => 'Превышено суточное ограничение на объем переведенного текста',
|
9
|
+
413 => 'Превышен максимально допустимый размер текста',
|
10
|
+
422 => 'Текст не может быть переведен',
|
11
|
+
501 => 'Заданное направление перевода не поддерживается'
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
attr_reader :code
|
15
|
+
|
16
|
+
def initialize(code = :default)
|
17
|
+
@code = code
|
18
|
+
msg = ERROR_CODES[code]
|
19
|
+
super(msg)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/y/translate.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'y/translate/api'
|
2
|
+
require 'y/translate/version'
|
3
|
+
require 'y/translate/configuration'
|
4
|
+
|
5
|
+
module Y
|
6
|
+
module Translate
|
7
|
+
class << self
|
8
|
+
attr_writer :configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configuration
|
12
|
+
@configuration ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield(configuration)
|
17
|
+
API.key = configuration.api_key
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.reset_default
|
21
|
+
api_key = configuration.api_key
|
22
|
+
@configuration = Configuration.new
|
23
|
+
configure do |config|
|
24
|
+
config.api_key = api_key
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get_langs
|
29
|
+
request(
|
30
|
+
API::METHOD_GET_LANGS,
|
31
|
+
ui: Configuration::DEFAULT_LANG_TO
|
32
|
+
)['dirs']
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.detect(text)
|
36
|
+
request(API::METHOD_DETECT, text: text)['lang']
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.translate(text, params = {})
|
40
|
+
params[:text] = text
|
41
|
+
params[:lang] = Configuration::DEFAULT_LANG_TO unless params.key?(:lang)
|
42
|
+
response = request(API::METHOD_TRANSLATE, params)['text']
|
43
|
+
response = response[0] if response.count == 1
|
44
|
+
response
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def self.request(method, params)
|
50
|
+
API.request(:post, method, params)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: y-translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Voronov Dmitry
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Client for Yandex translate API.
|
70
|
+
email:
|
71
|
+
- dmdropkick@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- lib/y/translate.rb
|
77
|
+
- lib/y/translate/api.rb
|
78
|
+
- lib/y/translate/configuration.rb
|
79
|
+
- lib/y/translate/error.rb
|
80
|
+
- lib/y/translate/version.rb
|
81
|
+
homepage: https://github.com/dmdropkick/y-translate
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.4.6
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Yandex translate API.
|
105
|
+
test_files: []
|