Class: NerdLog::Fight

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Fight

Returns a new instance of Fight.



6
7
8
9
# File 'lib/nerd_log/fight.rb', line 6

def initialize(options = {})
  @report_id       = options.fetch(:report_id)
  @http_connection = options.fetch(:http_connection, NerdLog.configuration.http_connection)
end

Instance Attribute Details

#http_connectionObject (readonly)

Returns the value of attribute http_connection.



5
6
7
# File 'lib/nerd_log/fight.rb', line 5

def http_connection
  @http_connection
end

#report_idObject (readonly)

Returns the value of attribute report_id.



5
6
7
# File 'lib/nerd_log/fight.rb', line 5

def report_id
  @report_id
end

Instance Method Details

#fetchObject



34
35
36
37
38
# File 'lib/nerd_log/fight.rb', line 34

def fetch
  response = http_connection.get("report/fights/#{report_id}")

  response
end

#fightsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nerd_log/fight.rb', line 11

def fights
  unless @fights
    body = fetch.body
    raw_fights = body['fights'].select {|f| f['boss'] != 0}
    friendlies = body['friendlies'].select {|f| f['type'] != 'NPC'}
    @fights = {}

    raw_fights.each do |raw_fight|
      @fights[raw_fight['id']] = OpenStruct.new(id: raw_fight['boss'],
                               kill: raw_fight['kill'],
                               difficulty: raw_fight['difficulty'], players: [])
    end

    friendlies.each do |player|
      player['fights'].each do |fight|
        boss_fight = @fights[fight['id']]
        boss_fight.players.push(player['name']) if boss_fight
      end
    end
  end
  @fights.values
end