Class: YandexTranslator::Translator
- Inherits:
-
Object
- Object
- YandexTranslator::Translator
- Defined in:
- lib/yandex_translator.rb
Overview
A Translator class
Constant Summary collapse
- Url_base =
'https://translate.yandex.net/api/v1.5/tr.json/'
Instance Attribute Summary collapse
-
#detected ⇒ Object
Returns the value of attribute detected.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
-
#initialize(key) ⇒ Translator
constructor
Returns the Translator object.
-
#lang_detect(text, hint = nil) ⇒ Object
Returns possible text languages The hint argument defaults to nil, should be a string of prefered languages, separator “,”.
-
#lang_list(lang = nil) ⇒ Object
Returns the hash with keys: * “dirs” with values of available translation pairs * “langs” with keys languages abbreviations transcriptions(if the lang argument is set).
- #requester(method, params, body) ⇒ Object
-
#translate(text, lang, format: :plain, options: 0) ⇒ Object
Return the translation of the text argument lang argument can be 2 types: * The pair of the languages “from-to” (‘en-ru’) * One destination language (‘en’) format argument defaults to plain.
Constructor Details
#initialize(key) ⇒ Translator
Returns the Translator object
27 28 29 30 |
# File 'lib/yandex_translator.rb', line 27 def initialize(key) @key = key @detected = nil end |
Instance Attribute Details
#detected ⇒ Object
Returns the value of attribute detected.
22 23 24 |
# File 'lib/yandex_translator.rb', line 22 def detected @detected end |
#key ⇒ Object
Returns the value of attribute key.
22 23 24 |
# File 'lib/yandex_translator.rb', line 22 def key @key end |
Instance Method Details
#lang_detect(text, hint = nil) ⇒ Object
Returns possible text languages The hint argument defaults to nil, should be a string of prefered languages, separator “,”
43 44 45 |
# File 'lib/yandex_translator.rb', line 43 def lang_detect(text, hint=nil) requester(:lang_detect, {:key => @key, :hint => hint}, {:text => URI::encode(text)}) end |
#lang_list(lang = nil) ⇒ Object
Returns the hash with keys:
-
“dirs” with values of available translation pairs
-
“langs” with keys languages abbreviations transcriptions(if the lang argument is set)
36 37 38 |
# File 'lib/yandex_translator.rb', line 36 def lang_list(lang=nil) requester(:lang_list, {:key => @key, :ui => lang}, nil) end |
#requester(method, params, body) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/yandex_translator.rb', line 58 def requester(method, params, body) url_method = case method when :lang_list then 'getLangs?' when :lang_detect then 'detect?' when :translate then 'translate?' end res = HTTP.post(Url_base + url_method, :params => params, :form => body) res = JSON.parse(res) if res['code'] and res['code'] != 200 case res['code'] when 401 then raise(WrongAPIKeyError, res['message']) when 402 then raise(BlockedAPIKeyError, res['message']) when 404 then raise(DaylyLimitExceededError, res['message']) when 413 then raise(MaximumTextSizeExceededError, res['message']) when 422 then raise(TextCannotBeTranslatedError, res['message']) when 501 then raise(SelectedTranslationDirectionNotSupportedError, res['message']) else raise(YandexError , res['message']) end end case method when :lang_list then res when :lang_detect then res['lang'] when :translate then if params[:options] == 1 then @detected = res['detected']['lang'] else @detected = nil end res['text'][0] end end |
#translate(text, lang, format: :plain, options: 0) ⇒ Object
Return the translation of the text argument lang argument can be 2 types:
-
The pair of the languages “from-to” (‘en-ru’)
-
One destination language (‘en’)
format argument defaults to plain. Can be “plain” for plain text or “html” for HTMl marked text 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 54 55 56 |
# File 'lib/yandex_translator.rb', line 53 def translate(text, lang, format: :plain, options: 0) requester(:translate, {:key => @key, :lang => lang, :format => format, :options => }, {:text => text}) end |