Class: EnergyAir::Bot

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/energy_air/bot.rb

Defined Under Namespace

Classes: UnknownQuestionError

Instance Method Summary collapse

Constructor Details

#initialize(tel_number, visual: false) ⇒ Bot

Returns a new instance of Bot.



16
17
18
19
20
21
22
# File 'lib/energy_air/bot.rb', line 16

def initialize(tel_number, visual: false)
  Capybara.current_driver = visual ? :selenium_chrome : :selenium_chrome_headless
  @tel_number = tel_number

  register
  loop { run }
end

Instance Method Details

#registerObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/energy_air/bot.rb', line 24

def register
  visit 'https://game.energy.ch'

  fill_in 'inlineFormInput', with: @tel_number
  click_on 'Verifizieren'
  check_for_error

  print 'Activation code (received via SMS): '
  activation_code = $stdin.gets.strip
  activation_code.chars.each.with_index do |num, index|
    fill_in (index + 1).to_s, with: num
  end
  sleep 5
  click_on 'Verifizieren'
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/energy_air/bot.rb', line 40

def run
  visit 'https://game.energy.ch'
  answer_question until finished?

  return register if reconfirmation_needed?
  return if lost?

  choose_random_bubble

  if won?
    print "√\a"
    sleep
  else
    print '.'
  end
rescue StandardError => e
  print "× (#{e})"
end