Class: YoudaoFanyi::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/youdao_fanyi/translator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Translator

Returns a new instance of Translator.



7
8
9
# File 'lib/youdao_fanyi/translator.rb', line 7

def initialize(*options)
  options.extract_options!.each {|k,v| instance_variable_set :"@#{k.to_s}", v}
end

Instance Attribute Details

#queriesObject (readonly)

Returns the value of attribute queries.



5
6
7
# File 'lib/youdao_fanyi/translator.rb', line 5

def queries
  @queries
end

Class Method Details

.t(*words) ⇒ Object



25
26
27
# File 'lib/youdao_fanyi/translator.rb', line 25

def self.t(*words)
  translate(*words) {|connector| connector.translation}
end

.translate(*words, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/youdao_fanyi/translator.rb', line 11

def self.translate(*words, &block)
  options = words.extract_options!
  not_translate = options.delete(:not_translate) || false
  words = format_words(words)
  if not_translate
    self.new(:queries => words)
  else
    words.map do |word|
      connector = YoudaoFanyi::Connector.new.request(word)
      block ? (yield connector) : connector
    end
  end
end