Class: Langouste

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

Constant Summary collapse

@@config_path =
File.join(File.expand_path(File.dirname(__FILE__)), '../config/langouste.yaml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Langouste

Returns a new instance of Langouste.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/langouste.rb', line 19

def initialize(options = {})
  options = {
    :from_lang  => :russian,
    :to_lang    => :english,
    :service    => :google,
    :config     => @@config_path
  }.merge options

  Langouste.config = YAML.load(File.open(options[:config]))

  @from_lang   = deabbreviate_language(options[:from_lang])
  @to_lang     = deabbreviate_language(options[:to_lang])
  @service     = deabbreviate_service(options[:service])
  @config      = Langouste.config[@service]
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#from_langObject

Returns the value of attribute from_lang.



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

def from_lang
  @from_lang
end

#serviceObject

Returns the value of attribute service.



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

def service
  @service
end

#to_langObject

Returns the value of attribute to_lang.



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

def to_lang
  @to_lang
end

Class Method Details

.configObject



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

def self.config
  @@config ||= YAML.load(File.open(@@config_path))
end

.config=(config) ⇒ Object



15
16
17
# File 'lib/langouste.rb', line 15

def self.config=(config)
  @@config = config
end

.list(config_path = nil) ⇒ Object



104
105
106
# File 'lib/langouste.rb', line 104

def self.list(config_path = nil)
  Langouste.config.keys.select{|k| k.is_a?(Symbol)}
end

Instance Method Details

#translate(input_text) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/langouste.rb', line 35

def translate(input_text)

  output_text = ''

  begin
    input_form = config['input']['form']
  rescue
    raise "Bad service: '#{service}'"
  end

  # если у переводчика нет нужных языков - запрос не выполняется
  if input_form['to'] and input_form['from']
    return '' unless input_form['to']['languages'][to_lang] and input_form['from']['languages'][from_lang]
  elsif input_form['directions']
    return '' unless input_form['directions']['directions']["#{from_lang}-#{to_lang}".to_sym]
  else
    raise "Bad YAML-config for service: '#{service}'"
  end

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

  agent.get(config['url']) do |page|

    translated_page = page.form_with(input_form['selector']) do |form|

      text = form.field_with(input_form['text']['selector'])
      text.value = input_text

      if input_form['directions'] and input_form['directions']['field']['selector']

        value = input_form['directions']['directions']["#{from_lang}-#{to_lang}".to_sym]
        direction = form.field_with(input_form['directions']['field']['selector'])
        direction.option_with(:value => value).select

      else

        if input_form['from'] and input_form['from']['field'] and input_form['from']['field']['selector']

          value = input_form['from']['languages'][from_lang]
          from_lang = form.field_with(input_form['from']['field']['selector'])
          from_lang.option_with(:value => value).select

        end

        if input_form['to'] and input_form['to']['field'] and input_form['to']['field']['selector']

          value = input_form['to']['languages'][to_lang]
          to_lang = form.field_with(input_form['to']['field']['selector'])
          to_lang.option_with(:value => value).select

        end

      end

    end.submit

    output_text = if config['output']['xpath']
      xpath = config['output']['xpath']
      translated_page.search(xpath)
    else
      output_form = config['output']['form']
      translated_page.form_with(output_form['selector']).field_with(output_form['text']['selector']).value
    end

  end

  output_text.to_s
end