Class: ApiBomb::Commander

Inherits:
Object
  • Object
show all
Includes:
Strategy::Naive
Defined in:
lib/api_bomb/commander.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Strategy::Naive

#attack, #attack_in_requests, #attack_in_time

Constructor Details

#initialize(army:, fronts: 1, duration: 10, logger: Logger.new(STDOUT), requests: nil) ⇒ Commander

Returns a new instance of Commander.



6
7
8
9
10
11
12
13
14
15
# File 'lib/api_bomb/commander.rb', line 6

def initialize(army:, fronts: 1, duration: 10, logger: Logger.new(STDOUT), requests: nil)
  @duration = duration
  @army = army
  @fighters = []
  @statuses = []
  @hold_times = []
  @fronts = fronts
  @logger = logger
  @requests = requests
end

Instance Attribute Details

#armyObject (readonly)

Returns the value of attribute army.



4
5
6
# File 'lib/api_bomb/commander.rb', line 4

def army
  @army
end

#durationObject (readonly)

Returns the value of attribute duration.



4
5
6
# File 'lib/api_bomb/commander.rb', line 4

def duration
  @duration
end

#frontsObject (readonly)

Returns the value of attribute fronts.



4
5
6
# File 'lib/api_bomb/commander.rb', line 4

def fronts
  @fronts
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/api_bomb/commander.rb', line 4

def logger
  @logger
end

#requestsObject (readonly)

Returns the value of attribute requests.



4
5
6
# File 'lib/api_bomb/commander.rb', line 4

def requests
  @requests
end

Instance Method Details

#start_attack!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/api_bomb/commander.rb', line 17

def start_attack!
  begin
    #I know that Timeout is really bad
    #but literarly there is no other generic way doing this
    #Fortunately we only do requests to an API so it shouldn't affect us
    logger.info "Starts firing requests"
    Timeout::timeout(duration) {
      attack
    }
  rescue Timeout::Error
    logger.info "Ceasefire!"
  end

  report_attack_result
end