Class: Langouste::Translator

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

Overview

Langouste translator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Translator

Returns a new instance of Translator


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/langouste.rb', line 33

def initialize(options = {})
  options = {
    :from_lang  => :russian,
    :to_lang    => :english,
    :service    => :google,
    :path       => nil
  }.merge options

  config = Configuration.instance(options[:path])

  @service     = deabbreviate_service(options[:service])
  @from_lang   = deabbreviate_language(options[:from_lang])
  @to_lang     = deabbreviate_language(options[:to_lang])
  @direction   = "#{@from_lang}-#{@to_lang}"
  @config      = config.data.services[@service]

end

Instance Attribute Details

#from_langObject

Returns the value of attribute from_lang


31
32
33
# File 'lib/langouste.rb', line 31

def from_lang
  @from_lang
end

#serviceObject

Returns the value of attribute service


31
32
33
# File 'lib/langouste.rb', line 31

def service
  @service
end

#to_langObject

Returns the value of attribute to_lang


31
32
33
# File 'lib/langouste.rb', line 31

def to_lang
  @to_lang
end

Instance Method Details

#translate(input_text) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/langouste.rb', line 51

def translate(input_text)

  return '' if not has_languages? or input_text.empty?

  agent = Mechanize.new {|a| a.user_agent_alias = 'Linux Konqueror'}

  input_form = @config.input.form
  page = agent.get(@config.url)
  translated_page = page.form_with(input_form.selector) do |form|
    fill_form form, input_form, input_text
  end.submit

  get_translation translated_page, @config.output

end