Class: HermesRuby::Yandex

Inherits:
Object
  • Object
show all
Defined in:
lib/hermes-ruby/yandex.rb

Overview

HermesRuby::Yandex class needs to be used for making calls to the Yandex Translator API.

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Yandex

Returns a new instance of Yandex.

Parameters:



12
13
14
15
# File 'lib/hermes-ruby/yandex.rb', line 12

def initialize(api_key)
  @base_url = 'https://translate.yandex.net/api/v1.5/tr.json'
  @api_key = api_key
end

Instance Method Details

#call_api(query_obj, method_name) ⇒ Object

Makes calls to Yandex Translator API. for example, ‘detect’, ‘translate’.

Parameters:

  • query_obj
    • a set of key value pairs containing params passing to the API.

  • method_name
    • an API method name that should be called,



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hermes-ruby/yandex.rb', line 59

def call_api(query_obj, method_name)
  query_obj[:key] = @api_key
  query_string = QueryString.stringify(query_obj)
  headers = { 'Content-Type': 'application/x-www-form-urlencoded' }

  begin
    result = RestClient.post("#{@base_url}/#{method_name}", query_string, headers: headers)
    return JSON.parse(result.body)
  rescue RestClient::RequestFailed => e
    raise e.response.body
  end
end

#detect(text, hint = nil) ⇒ Object

Detects the language of the specified text. of the most likely languages, for example [:en, :de].

Parameters:

  • text
    • the text to detect the language for.

  • hint (defaults to: nil)
    • array of symbols containing language codes.



27
28
29
30
31
32
33
# File 'lib/hermes-ruby/yandex.rb', line 27

def detect(text, hint = nil)
  query_obj = { text: text }
  query_obj[:hint] = hint if hint
  result = call_api(query_obj, 'detect')

  result['lang']
end

#get_langs(ui_lang = 'en') ⇒ Object

Gets a list of translation directions supported by the service.

Parameters:

  • ui_lang (defaults to: 'en')
    • the language code that specifies a response language.



19
20
21
# File 'lib/hermes-ruby/yandex.rb', line 19

def get_langs(ui_lang = 'en')
  call_api({ ui: ui_lang }, 'getLangs')
end

#translate(text, lang = 'en', format = 'plain') ⇒ Object

Translates text to the specified language. You can set it in either of the following ways:

- As a pair of language codes separated by a hyphen ("from"-"to").
  For example, en-ru indicates translating from English to Russian.
- As the target language code (for example, ru).
  In this case, the service tries to detect the source language automatically.

Default value: ‘en’. Possible values:

- plain - Text without markup (default value).
- html - Text in HTML format.

Parameters:

  • text
    • the text to translate.

  • lang (defaults to: 'en')
    • the translation direction.

  • format (defaults to: 'plain')
    • text format.



48
49
50
51
52
53
# File 'lib/hermes-ruby/yandex.rb', line 48

def translate(text, lang = 'en', format = 'plain')
  query_obj = { text: text, lang: lang, format: format }
  result = call_api(query_obj, 'translate')['text']

  result.first
end