Class: KillmailParser

Inherits:
Object
  • Object
show all
Defined in:
lib/neweden-km-parser.rb

Constant Summary collapse

REGEXP_PRIMITIVES =
{
  :victim_name =>         "Victim: (.*)",
  :corp =>                "Corp: (.*)",
  :alliance =>            "Alliance: (.*)",
  :faction =>             "Faction: (.*)",
  :destroyed =>           "Destroyed: (.*)",
  :system =>              "System: (.*)",
  :security =>            "Security: (-?\\d{1,2}\\.\\d{1,2})",
  :damage_taken =>        "Damage Taken: (-?\\d+)",
  :name =>                "Name: (.*)",
  :ship =>                "Ship: (.*)",
  :weapon =>              "Weapon: (.*)",
  :damage_done =>         "Damage Done: (-?\\d+)",
  :involved_parties =>    "Involved parties:",
  :date =>                "\\d{4}\\.\\d{2}\\.\\d{2} \\d{2}:\\d{2}:\\d{2}"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(killmail) ⇒ KillmailParser

Returns a new instance of KillmailParser.



21
22
23
24
25
# File 'lib/neweden-km-parser.rb', line 21

def initialize(killmail)
  @valid = false
  @killmail = killmail.to_s.dup.freeze
  process
end

Instance Attribute Details

#attackersObject (readonly)

Returns the value of attribute attackers.



19
20
21
# File 'lib/neweden-km-parser.rb', line 19

def attackers
  @attackers
end

#dateObject (readonly)

Returns the value of attribute date.



19
20
21
# File 'lib/neweden-km-parser.rb', line 19

def date
  @date
end

#destroyed_itemsObject (readonly)

Returns the value of attribute destroyed_items.



19
20
21
# File 'lib/neweden-km-parser.rb', line 19

def destroyed_items
  @destroyed_items
end

#dropped_itemsObject (readonly)

Returns the value of attribute dropped_items.



19
20
21
# File 'lib/neweden-km-parser.rb', line 19

def dropped_items
  @dropped_items
end

#killmailObject (readonly)

Returns the value of attribute killmail.



19
20
21
# File 'lib/neweden-km-parser.rb', line 19

def killmail
  @killmail
end

#victimObject (readonly)

Returns the value of attribute victim.



19
20
21
# File 'lib/neweden-km-parser.rb', line 19

def victim
  @victim
end

Instance Method Details

#_attackerObject



154
155
156
# File 'lib/neweden-km-parser.rb', line 154

def _attacker
  Regexp.new(%{#{_name}\n#{_security}\n#{_corp}\n#{_alliance}\n#{_faction}\n#{_ship}\n#{_weapon}\n#{_damage_done}})
end

#_itemObject



146
147
148
# File 'lib/neweden-km-parser.rb', line 146

def _item
  /^([^,()]*)(, Qty: (\d+))?( \((.*)\))?$/
end

#_victimObject



150
151
152
# File 'lib/neweden-km-parser.rb', line 150

def _victim
  Regexp.new(%{#{_victim_name}\n#{_corp}\n#{_alliance}\n#{_faction}\n#{_destroyed}\n#{_system}\n#{_security}\n#{_damage_taken}})
end

#parse_attackers(pbody) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/neweden-km-parser.rb', line 35

def parse_attackers(pbody)
  raise "Could not parse attackers" if pbody.nil? || pbody.empty?

  split_on = if pbody =~ /Destroyed items:/
    @has_destroyed = true
    'Destroyed items:'
  elsif pbody =~ /Dropped items:/
    @has_destroyed = false
    'Dropped items:'
  else
    @has_destroyed = false
    nil
  end

  attacker_txt, pbody = if split_on
    pbody.split(split_on).map { |txt| txt.strip }
  else
    [pbody.strip, nil]
  end

  attackers = attacker_txt.split("\n\n").map { |txt| txt.strip }
  [attackers, pbody]
end

#parse_destroyed_items(pbody) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/neweden-km-parser.rb', line 59

def parse_destroyed_items(pbody)
  return [nil, nil] if pbody.nil? || pbody.empty?

  if pbody =~ /Dropped items:/
    pbody.split("Dropped items:").map { |txt| txt.strip }
  else
    [pbody.strip, nil]
  end
end

#parse_victim_and_date(pbody) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/neweden-km-parser.rb', line 27

def parse_victim_and_date(pbody)
  raise "Could not parse victim and date" if pbody.nil? || pbody.empty?

  header, pbody = pbody.split("Involved parties:").map { |txt| txt.strip }
  date, victim = header.split("\n\n").map { |txt| txt.strip }
  [date, victim, pbody]
end

#processObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/neweden-km-parser.rb', line 69

def process
  date, victim, pbody = parse_victim_and_date(@killmail)
  attackers, pbody = parse_attackers(pbody)
  destroyed, pbody = parse_destroyed_items(pbody) if pbody && @has_destroyed
  dropped = pbody.nil? ? nil : pbody.strip

  @date = DateTime.parse(date)

  victim_match = _victim.match(victim)
  @victim = {
    :name => victim_match[1],
    :corp => victim_match[2],
    :alliance => victim_match[3],
    :faction => victim_match[4],
    :ship => victim_match[5],
    :system => victim_match[6],
    :security => victim_match[7],
    :damage => victim_match[8]
  }

  @attackers = []
  attackers.each do |attacker|
    attacker_match = _attacker.match(attacker)
    attacker_name_match = /^([^()]+)( \((.*)\))?$/.match(attacker_match[1])
    attacker_name = attacker_name_match[1]
    final_blow = !!attacker_name_match[3]
    @attackers << {
      :name =>        attacker_name,
      :final_blow =>  final_blow,
      :security =>    attacker_match[2],
      :corp =>        attacker_match[3],
      :alliance =>    attacker_match[4],
      :faction =>     attacker_match[5],
      :ship =>        attacker_match[6],
      :weapon =>      attacker_match[7],
      :damage =>      attacker_match[8]
    }
  end

  @destroyed_items = []
  @dropped_items = []
  [[destroyed, @destroyed_items], [dropped, @dropped_items]].each do |body, items|
    body.to_s.split(/[\n\r]+/).each do |item|
      item_match = _item.match(item.strip)
      qty, loc = [item_match[3] || 1, item_match[5]]

      items << {
        :name => item_match[1],
        :quantity => qty.to_i,
        :location => loc
      }
    end
  end

  @hash = {
    :victim => @victim,
    :attackers => @attackers,
    :destroyed_items => @destroyed_items,
    :dropped_items => @dropped_items,
    :date => @date
  }

  @valid = true
end

#to_hashObject



138
139
140
# File 'lib/neweden-km-parser.rb', line 138

def to_hash
  @hash
end

#to_jsonObject



142
143
144
# File 'lib/neweden-km-parser.rb', line 142

def to_json
  @hash.to_json
end

#valid?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/neweden-km-parser.rb', line 134

def valid?
  @valid
end