Class: WiFiddler::CLI

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

Defined Under Namespace

Classes: ArgumentParser, TooManyAttempts

Constant Summary collapse

STEP_DOWN_RATE =
1.5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.



22
23
24
# File 'lib/wifiddler.rb', line 22

def initialize(options = {})
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.run(io, arguments) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/wifiddler.rb', line 13

def self.run(io, arguments)
  options = { io: io }
  options.merge! ArgumentParser.new.parse(arguments)
  self.new(options).run
rescue ArgumentParser::InvalidOption => e
  io.puts e
  1
end

Instance Method Details

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wifiddler.rb', line 26

def run
  Timeout::timeout(options[:timeout]) do
    wait_time = options[:interval]
    attempts = 0
    until network
      raise TooManyAttempts if attempts > options[:attempts]
      cycle_airport
      print '.'
      attempts += 1
      sleep wait_time
      wait_time *= STEP_DOWN_RATE
    end
  end
  self.io.puts "\nConnected to '#{network}'"
  0
rescue TooManyAttempts
  self.io.puts "\nUnable to establish connection after #{options[:attempts]} attempts"
  1
rescue Timeout::Error
  self.io.puts "\nUnable to establish connection after #{options[:timeout]} seconds"
  1
end