Class: Oversetter::Yandex

Inherits:
Object
  • Object
show all
Defined in:
lib/oversetter/yandex/text.rb,
lib/oversetter/yandex/detect.rb,
lib/oversetter/yandex/getlangs.rb,
lib/oversetter/yandex/translate.rb

Overview

Yandex.com’s service provides translations and detections.

Defined Under Namespace

Classes: Detect, Getlangs, Translate

Instance Method Summary collapse

Instance Method Details

#get_word(search, func, params, result) ⇒ Object

Fetches dynamically generated URL. Functions are translate, detect, and getLangs.

Parameters:

  • search (String)

    The word or phrase to search for.

  • func (String)

    The search function to use.

  • params (Hash)

    The search parameters to use.

  • result (String)

    The search response.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/oversetter/yandex/text.rb', line 14

def get_word(search, func, params, result)
			search = URI.encode(search)
			prefix = 'https://translate.yandex.net/api/v1.5/tr.json/'
			word, pcont = "#{prefix}#{func}?", []
			if func != 'getLangs' then pcont.push "text=#{search}&"; end
  params.map { |k, v|
    if k == :lang then pcont.push "lang=#{v}&"; end
if k == :ui then pcont.push "ui=#{v}&"; end
  }
			if func == 'translate' || func == 'detect' then pcont.push 'format=plain&'; end
			if func == 'translate' then pcont.push 'options=1&'; end
  apikey = ENV['YANDEX']
			pcont.push "key=#{apikey}"
  url = "#{word}#{pcont.join}"
  request = HTTPI::Request.new(url)
  getter = HTTPI.get(request)
  result = getter.body
end