yandex_translator 0.1.6 → 0.1.7

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: 04c46dfbe913aab1a7edd0070f0d46b75aea367b
4
- data.tar.gz: f875cbb63eb7e644180dfe8039512f31707882e3
3
+ metadata.gz: e414a4a08e40266e244f96d278d83b0871ab7f25
4
+ data.tar.gz: dbd4accc67d90d6f7cebd1acc9f8d00b0085bce9
5
5
  SHA512:
6
- metadata.gz: ab3372192dbb9789370262fae69a9aa4d42473ca6db0752113f922d4907dc6b57104bac5d40b0871b27fd46d2df4f01ca0fac2579866dfd9690f04d75f549e0d
7
- data.tar.gz: eda4d53de3c26023561f4a9aa56b0645fbf79f490f58bc69fdb0a823c301b130b097b3755749858d90a2c02578920cce3b061d7c43c14d17cb6e8474a59177d6
6
+ metadata.gz: 97e6465ddabeb5865383eea62eb840d9098b20d4d4f24f86096e31e169120e2fdb86e27ccf32cafa3d1bcca473df1aaacc00d92e2d34331aaab06e43bcfeb3f5
7
+ data.tar.gz: 94744c6cb5d494af41ec5e19e481eed443caa3e9f5c195df2ba43a67d8f56d62ffd19d866368dc714945d96b1607666c380af6e467091a8b3eace1f0b2da0b41
data/README.md CHANGED
@@ -1,50 +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:plain, options:0)
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
-
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
+
@@ -1,3 +1,3 @@
1
- module YandexTranslator
2
- VERSION = "0.1.6"
3
- end
1
+ module YandexTranslator
2
+ VERSION = "0.1.7"
3
+ end
@@ -1,88 +1,107 @@
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
- require 'http'
9
-
10
- class YandexError < StandardError; end
11
-
12
- class WrongAPIKeyError < YandexError; end
13
- class BlockedAPIKeyError < YandexError; end
14
- class DaylyLimitExceededError < YandexError; end
15
- class MaximumTextSizeExceededError < YandexError; end
16
- class TextCannotBeTranslatedError < YandexError; end
17
- class SelectedTranslationDirectionNotSupportedError < YandexError; end
18
-
19
- # A Translator class
20
- #
21
- class Translator
22
- attr_accessor :key, :detected
23
- Url_base = 'https://translate.yandex.net/api/v1.5/tr.json/'
24
-
25
- # Returns the Translator object
26
- #
27
- def initialize(key)
28
- @key = key
29
- @detected = nil
30
- end
31
-
32
- # Returns the hash with keys:
33
- # * "dirs" with values of available translation pairs
34
- # * "langs" with keys languages abbreviations transcriptions(if the _lang_ argument is set)
35
- #
36
- def lang_list(lang=nil)
37
- requester(:lang_list, {:key => @key, :ui => lang}, nil)
38
- end
39
-
40
- # Returns possible text languages
41
- # The _hint_ argument defaults to *nil*, should be a string of prefered languages, separator ","
42
- #
43
- def lang_detect(text, hint=nil)
44
- requester(:lang_detect, {:key => @key, :hint => hint}, {:text => URI::encode(text)})
45
- end
46
-
47
- # Return the translation of the _text_ argument
48
- # _lang_ argument can be 2 types:
49
- # * The pair of the languages "from-to" ('en-ru')
50
- # * One destination language ('en')
51
- # _format_ argument defaults to *plain*. Can be "plain" for plain text or "html" for HTMl marked text
52
- # _options_ argument defaults to *0*. Can be "1" to include to the response the autodetected language of the source text. You can obtain it by attribute *detected*
53
- def translate(text, lang, format: :plain, options: 0)
54
- requester(:translate, {:key => @key, :lang => lang, :format => format, :options => options},
55
- {:text => text})
56
- end
57
-
58
- def requester(method, params, body)
59
- url_method = case method
60
- when :lang_list then 'getLangs?'
61
- when :lang_detect then 'detect?'
62
- when :translate then 'translate?'
63
- end
64
- res = HTTP.post(Url_base + url_method, :params => params, :form => body)
65
- res = JSON.parse(res)
66
-
67
- if res['code'] and res['code'] != 200
68
- case res['code']
69
- when 401 then raise(WrongAPIKeyError, res['message'])
70
- when 402 then raise(BlockedAPIKeyError, res['message'])
71
- when 404 then raise(DaylyLimitExceededError, res['message'])
72
- when 413 then raise(MaximumTextSizeExceededError, res['message'])
73
- when 422 then raise(TextCannotBeTranslatedError, res['message'])
74
- when 501 then raise(SelectedTranslationDirectionNotSupportedError, res['message'])
75
- else raise(YandexError , res['message'])
76
- end
77
- end
78
-
79
- case method
80
- when :lang_list then res
81
- when :lang_detect then res['lang']
82
- when :translate then
83
- if params[:options] == 1 then @detected = res['detected']['lang'] else @detected = nil end
84
- res['text'][0]
85
- end
86
- end
87
- end
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 'json'
7
+ require 'http'
8
+
9
+ class YandexError < StandardError; end
10
+
11
+ class WrongAPIKeyError < YandexError; end
12
+ class BlockedAPIKeyError < YandexError; end
13
+ class DaylyLimitExceededError < YandexError; end
14
+ class MaximumTextSizeExceededError < YandexError; end
15
+ class TextCannotBeTranslatedError < YandexError; end
16
+ class SelectedTranslationDirectionNotSupportedError < YandexError; end
17
+
18
+ # A Translator class
19
+ #
20
+ class Translator
21
+ attr_accessor :key, :detected
22
+ URL_BASE = 'https://translate.yandex.net/api/v1.5/tr.json/'
23
+
24
+ # Returns the Translator object
25
+ #
26
+ def initialize(key)
27
+ @key = key
28
+ @detected = nil
29
+ end
30
+
31
+ # Returns the hash with keys:
32
+ # * "dirs" with values of available translation pairs
33
+ # * "langs" with keys languages abbreviations transcriptions(if the _lang_ argument is set)
34
+ #
35
+ def lang_list(lang = nil)
36
+ requester(:lang_list, { key: @key, ui: lang }, nil)
37
+ end
38
+
39
+ # Returns possible text languages
40
+ # The _hint_ argument defaults to *nil*, should be a string of prefered languages, separator ","
41
+ #
42
+ def lang_detect(text, hint = nil)
43
+ requester(:lang_detect, { key: @key, hint: hint }, text)
44
+ end
45
+
46
+ # Return the translation of the _text_ argument
47
+ # _text_ argument can be a string or an array of strings.
48
+ # _lang_ argument can be 2 types:
49
+ # * The pair of the languages "from-to" ('en-ru')
50
+ # * One destination language ('en')
51
+ # _format_ argument defaults to *plain*. Can be "plain" for plain text or "html" for HTMl marked text
52
+ # _options_ argument defaults to *0*. Can be "1" to include to the response the autodetected language of the source text. You can obtain it by attribute *detected*
53
+ def translate(text, lang, format: :plain, options: 0)
54
+ requester(:translate, { key: @key, lang: lang, format: format, options: options },text)
55
+ end
56
+
57
+ def requester(method, params, body)
58
+ url_method = case method
59
+ when :lang_list then 'getLangs?'
60
+ when :lang_detect then 'detect?'
61
+ when :translate then 'translate?'
62
+ end
63
+ temp = ''
64
+ if body
65
+ if body.class == Array
66
+ body.each do |v|
67
+ temp += ('text=' + URI::encode(v.to_s) + '&')
68
+ end
69
+ body = temp[0..-2]
70
+ else
71
+ body = 'text=' + URI::encode(body.to_s)
72
+ end
73
+ end
74
+
75
+ res = HTTP.headers("content-type" => "application/x-www-form-urlencoded").post(URL_BASE + url_method, params: params, body: body)
76
+ res = JSON.parse(res)
77
+
78
+ if res['code'] and res['code'] != 200
79
+ case res['code']
80
+ when 401 then raise(WrongAPIKeyError, res['message'])
81
+ when 402 then raise(BlockedAPIKeyError, res['message'])
82
+ when 404 then raise(DaylyLimitExceededError, res['message'])
83
+ when 413 then raise(MaximumTextSizeExceededError, res['message'])
84
+ when 422 then raise(TextCannotBeTranslatedError, res['message'])
85
+ when 501 then raise(SelectedTranslationDirectionNotSupportedError, res['message'])
86
+ else raise(YandexError , res['message'])
87
+ end
88
+ end
89
+
90
+ case method
91
+ when :lang_list then res
92
+ when :lang_detect then res['lang']
93
+ when :translate then
94
+ if params[:options] == 1
95
+ @detected = res['detected']['lang']
96
+ else
97
+ @detected = nil
98
+ end
99
+ if res['text'].size > 1
100
+ res['text']
101
+ else
102
+ res['text'][0]
103
+ end
104
+ end
105
+ end
106
+ end
88
107
  end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.author = "Kozlov_Evgeny"
10
10
  spec.email = ["eeeeevgen@gmail.com"]
11
11
  spec.homepage = 'https://github.com/Eeeeevgen/yandex_translator'
12
- spec.date = '2017-05-25'
12
+ spec.date = '2017-07-26'
13
13
  spec.summary = "A library for translating text using Yandex Translate API"
14
14
 
15
15
  spec.license = "MIT"
@@ -21,7 +21,6 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.required_ruby_version = "~> 2.0"
23
23
  spec.add_dependency "http", "~> 2.2"
24
- spec.add_dependency "yard", "~> 0.9.9"
25
24
  spec.add_development_dependency "bundler", "~> 1.14"
26
25
  spec.add_development_dependency "rake", "~> 10.0"
27
26
  spec.add_development_dependency "rspec", "~> 3.6"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kozlov_Evgeny
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-25 00:00:00.000000000 Z
11
+ date: 2017-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.2'
27
- - !ruby/object:Gem::Dependency
28
- name: yard
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: 0.9.9
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: 0.9.9
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: bundler
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -131,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
117
  version: '0'
132
118
  requirements: []
133
119
  rubyforge_project:
134
- rubygems_version: 2.6.12
120
+ rubygems_version: 2.6.8
135
121
  signing_key:
136
122
  specification_version: 4
137
123
  summary: A library for translating text using Yandex Translate API