yandex_translator 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83f0cf7063ed71ae8739f98a90b5c67980a8129c
4
- data.tar.gz: 51ce6404e99d02204bac390f57171ce8023f152c
3
+ metadata.gz: '0843fc19087c1318e55258f3e9f7e6afaaad2c8b'
4
+ data.tar.gz: 85aec34640917e5b4acad66de892f722708655dd
5
5
  SHA512:
6
- metadata.gz: ca9b4e6f0775c3dfe57f9c5d89f6b387d42fc9846e0e303aca6d44624a7e4a0c3760f2a83815955de7ad0a512077bbfe9c1e82f40b724d4a42dec38805cb185a
7
- data.tar.gz: 9e86a931329ec98eddc1d7d3bc72a6015414b82ca801f15b3abe426eb90365ce1bbdd9629e6547ee0fe12e4c17d558e6739ef8dc8d05a7fafeae68193efcf238
6
+ metadata.gz: fe3798226174329b91392196b49af1e94d64e6735fcc9f351133fbc8b24ffd733a8cadd719c486aadb529ac572a187f27a077c84115066e21cedab340b683149
7
+ data.tar.gz: d52020060426b7df2431d988da2401692b4b3ef1fd03aecf56d1d0dbd7aad5370c34e5bdf7c6bc85213f1dead6b2f84b5e5c9a3af7a7b3827abe146dff03fa99
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: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
+ # 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,88 +1,88 @@
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}, 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']
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 '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}, 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
88
88
  end
@@ -1,3 +1,3 @@
1
- module YandexTranslator
2
- VERSION = "0.1.4"
3
- end
1
+ module YandexTranslator
2
+ VERSION = "0.1.5"
3
+ 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.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kozlov_Evgeny
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  requirements: []
133
133
  rubyforge_project:
134
- rubygems_version: 2.6.8
134
+ rubygems_version: 2.6.12
135
135
  signing_key:
136
136
  specification_version: 4
137
137
  summary: A library for translating text using Yandex Translate API