class EnergyAir::Bot

Public Class Methods

new(tel_number, visual: false) click to toggle source
# 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

Public Instance Methods

register() click to toggle source
# 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
run() click to toggle source
# 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

Private Instance Methods

answer_question() click to toggle source
# File lib/energy_air/bot.rb, line 61
def answer_question
  current_question = find('.question-text').text
  answer = EnergyAir::QUESTIONS.fetch(current_question) do
    raise UnknownQuestionError, "Cannot find an answer to the question: #{current_question}"
  end

  2.times { find('label', text: answer).click }
  click_on 'Weiter'
end
check_for_error() click to toggle source
# File lib/energy_air/bot.rb, line 91
def check_for_error
  if all('.error-message').any?
    warn "An error message appeared: #{find('.error-message').text}\nExiting.."
    exit
  end
end
choose_random_bubble() click to toggle source
# File lib/energy_air/bot.rb, line 75
def choose_random_bubble
  all('.circle').sample.click
end
finished?() click to toggle source
# File lib/energy_air/bot.rb, line 71
def finished?
  all('.question-text').empty?
end
lost?() click to toggle source
# File lib/energy_air/bot.rb, line 79
def lost?
  all('h1').map(&:text).include?('Leider verloren')
end
lost_bubble?() click to toggle source
# File lib/energy_air/bot.rb, line 83
def lost_bubble?
  all('img[src="https://cdn.energy.ch/game-web/images/eair/bubble-lose.png"]').any?
end
reconfirmation_needed?() click to toggle source
# File lib/energy_air/bot.rb, line 98
def reconfirmation_needed?
  all('.title-verification').any?
end
won?() click to toggle source
# File lib/energy_air/bot.rb, line 87
def won?
  !lost_bubble?
end