Skip to content
This repository has been archived by the owner on Mar 18, 2020. It is now read-only.

Commit

Permalink
work on specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ealdent committed Apr 20, 2012
1 parent ad0dc65 commit ef73220
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 345 deletions.
8 changes: 0 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ Rake::TestTask.new(:test) do |test|
test.verbose = true
end

require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
test.rcov_opts << '--exclude "gems/*"'
end

task :default => :test

require 'rdoc/task'
Expand Down
58 changes: 48 additions & 10 deletions lib/neweden-km-parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,53 @@ def initialize(killmail)
process
end

def process
victim, body = @killmail.split("Involved parties:").map { |txt| txt.strip }
attackers, items_body = body.split("Destroyed items:").map { |txt| txt.strip }
destroyed, dropped = items_body.split("Dropped items:").map { |txt| txt.strip }
date, victim = victim.split("\n\n").map { |txt| txt.strip }
attackers = attackers.split("\n\n").map { |txt| txt.strip }
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

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

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

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

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)

Expand All @@ -50,7 +89,6 @@ def process
@attackers = []
attackers.each do |attacker|
attacker_match = _attacker.match(attacker)
puts attacker_match
@attackers << {
:name => attacker_match[1],
:security => attacker_match[2],
Expand All @@ -66,7 +104,7 @@ def process
@destroyed_items = []
@dropped_items = []
[[destroyed, @destroyed_items], [dropped, @dropped_items]].each do |body, items|
body.split(/[\n\r]+/).each do |item|
body.to_s.split(/[\n\r]+/).each do |item|
item_match = _item.match(item.strip)
qty, loc = if item_match[3] && item_match[3] =~ /^\d+/
[item_match[3], item_match[5]]
Expand All @@ -78,7 +116,7 @@ def process

items << {
:name => item_match[1],
:quantity => qty,
:quantity => qty.to_i,
:location => loc
}
end
Expand Down
49 changes: 49 additions & 0 deletions test/km_degenerate_dropped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
2012.04.19 02:23

Victim: Waister
Corp: Tactical Soldiers
Alliance: Nulli Secunda
Faction: None
Destroyed: Capsule
System: BWF-ZZ
Security: 0.0
Damage Taken: 351

Involved parties:

Name: cas0
Security: 1.3
Corp: Interstellar eXodus
Alliance: BricK sQuAD.
Faction: None
Ship: Tornado
Weapon: 1400mm Prototype Siege Cannon
Damage Done: 341

Name: Naternine (laid the final blow)
Security: 3.8
Corp: Liberation Army
Alliance: BricK sQuAD.
Faction: None
Ship: Drake
Weapon: Caldari Navy Scourge Heavy Missile
Damage Done: 10

Name: Misera Blue
Security: 5.0
Corp: Liberation Army
Alliance: BricK sQuAD.
Faction: None
Ship: Tornado
Weapon: 1200mm Artillery Cannon II
Damage Done: 0

Name: WaveDragon
Security: -2.1
Corp: Red Sky Morning
Alliance: BricK sQuAD.
Faction: None
Ship: Unknown
Weapon: Prototype 'Arbalest' Torpedo Launcher
Damage Done: 0

Loading

0 comments on commit ef73220

Please sign in to comment.