Class: Translator::Translate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, to: '', from: '') ⇒ Translate

Returns a new instance of Translate.



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

def initialize(string, to: '', from: '')
  @string = string
  @to = to
  @from = from

  if to.empty? && from.empty?
    @to = Translator::ConfigFile.default_to || 'auto'
    @from = Translator::ConfigFile.default_from
  end
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



11
12
13
# File 'lib/translator.rb', line 11

def from
  @from
end

#stringObject (readonly)

Returns the value of attribute string.



10
11
12
# File 'lib/translator.rb', line 10

def string
  @string
end

#toObject

Returns the value of attribute to.



11
12
13
# File 'lib/translator.rb', line 11

def to
  @to
end

Instance Method Details

#parsed_stringObject



40
41
42
# File 'lib/translator.rb', line 40

def parsed_string
  string.split(' ').join('%20')
end

#resultObject



32
33
34
# File 'lib/translator.rb', line 32

def result
  find('#result_box').text
end

#translate!Object



24
25
26
27
28
29
30
# File 'lib/translator.rb', line 24

def translate!
  visit_google_translator

  sleep 3

  result.colorize(:light_green)
end

#visit_google_translatorObject



36
37
38
# File 'lib/translator.rb', line 36

def visit_google_translator
  visit "https://translate.google.com.br/?source=osdd##{from}/#{to}/#{parsed_string}"
end