Class: Langouste::Translator
- Inherits:
-
Object
- Object
- Langouste::Translator
- Defined in:
- lib/langouste.rb
Overview
Langouste translator
Instance Attribute Summary collapse
-
#from_lang ⇒ Object
Returns the value of attribute from_lang.
-
#service ⇒ Object
Returns the value of attribute service.
-
#to_lang ⇒ Object
Returns the value of attribute to_lang.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Translator
constructor
A new instance of Translator.
- #translate(input_text) ⇒ Object
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( = {}) = { :from_lang => :russian, :to_lang => :english, :service => :google, :path => nil }.merge config = Configuration.instance([:path]) @service = deabbreviate_service([:service]) @from_lang = deabbreviate_language([:from_lang]) @to_lang = deabbreviate_language([:to_lang]) @direction = "#{@from_lang}-#{@to_lang}" @config = config.data.services[@service] end |
Instance Attribute Details
#from_lang ⇒ Object
Returns the value of attribute from_lang.
31 32 33 |
# File 'lib/langouste.rb', line 31 def from_lang @from_lang end |
#service ⇒ Object
Returns the value of attribute service.
31 32 33 |
# File 'lib/langouste.rb', line 31 def service @service end |
#to_lang ⇒ Object
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 |