-
-
Save r3nya/8891192 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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