Class: YandexTranslator::Translator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Translator

Returns the Translator object



26
27
28
29
# File 'lib/yandex_translator.rb', line 26

def initialize(key)
  @key = key
  @detected = nil
end

Instance Attribute Details

#detectedObject

Returns the value of attribute detected.



21
22
23
# File 'lib/yandex_translator.rb', line 21

def detected
  @detected
end

#keyObject

Returns the value of attribute key.



21
22
23
# File 'lib/yandex_translator.rb', line 21

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 “,”



42
43
44
# File 'lib/yandex_translator.rb', line 42

def lang_detect(text, hint = nil)
  requester(:lang_detect, { key: @key, hint: hint }, 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)



35
36
37
# File 'lib/yandex_translator.rb', line 35

def lang_list(lang = nil)
  requester(:lang_list, { key: @key, ui: lang }, nil)
end

#requester(method, params, body) ⇒ Object



57
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/yandex_translator.rb', line 57

def requester(method, params, body)
  url_method = case method
                 when :lang_list then 'getLangs?'
                 when :lang_detect then 'detect?'
                 when :translate then 'translate?'
               end
  temp = ''
  if body
    if body.class == Array
      body.each do |v|
        temp += ('text=' + URI::encode(v.to_s) + '&')
      end
      body = temp[0..-2]
    else
      body = 'text=' + URI::encode(body.to_s)
    end
  end

  res = HTTP.headers("content-type" => "application/x-www-form-urlencoded").post(URL_BASE + url_method, params: params, body: 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
        @detected = res['detected']['lang']
      else
        @detected = nil
      end
      if res['text'].size > 1
        res['text']
      else
        res['text'][0]
      end
  end
end

#translate(text, lang, format: :plain, options: 0) ⇒ Object

Return the translation of the text argument text argument can be a string or an array of strings. 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
# File 'lib/yandex_translator.rb', line 53

def translate(text, lang, format: :plain, options: 0)
  requester(:translate, { key: @key, lang: lang, format: format, options: options },text)
end