Skip to content

Instantly share code, notes, and snippets.

@Olefine
Created September 1, 2013 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Olefine/6406311 to your computer and use it in GitHub Desktop.
Save Olefine/6406311 to your computer and use it in GitHub Desktop.
require 'yandex-translator'
class Translate
attr_accessor :words_for_translate
attr_reader :translated_word
def initialize(words)
set_api_key
@words_for_translate = words
end
def translate(options = nil)
raise ArgumentError unless options
p @words_for_translate
@translated_word = if @words_for_translate.size > 1
translated = @words_for_translate.map {|word| yandex_translate(word, options) }
translated.join(' ')
else
yandex_translate(@words_for_translate, options)
end
@translated_word
end
private
def set_api_key
Yandex::Translator.set_api_key('trnsl.1.1.20130901T135648Z.f9baa157528bd5a8.9f830c39c1e02e3cf0cf97d79932ddb245fb0684')
end
def yandex_translate(word, options = {})
Yandex::Translator.translate(word, options.values.first.to_s, options.keys.first.to_s)
end
end
tr = Translate.new(ARGV)
puts tr.translate(:en => :ru)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment