Skip to content

Commit

Permalink
Added pipe parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
willawill committed Feb 9, 2014
1 parent 48212f6 commit 9ee65a8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Rakefile
@@ -1,11 +1,12 @@
require_relative "lib/comma_parser.rb"
require_relative "lib/pipe_parser.rb"
require_relative "lib/record.rb"
require_relative "lib/display_manager.rb"
require_relative "lib/record_generator.rb"

task :display_records do |t|
parser = CommaParser.new("spec/fixtures/comma.txt")
raw_input = parser.parse_record_data
raw_input = parser.parse_record_data + PipeParser.new("spec/fixtures/pipe.txt").parse_record_data
records = RecordGenerator.generate_record_set(raw_input)
display_manager = DisplayManager.new(records)
puts "Output 1"
Expand Down
6 changes: 5 additions & 1 deletion lib/comma_parser.rb
Expand Up @@ -8,7 +8,11 @@ def initialize file_name
def parse_record_data
@raw_data.inject([]) do |result,record_data|
record_data.collect!(&:strip)
result << {last_name: record_data[0], first_name: record_data[1], gender: record_data[2], favorite_color: record_data[3], date_of_birth: record_data[4]}
result << {last_name: record_data[0].downcase,
first_name: record_data[1].downcase,
gender: record_data[2].downcase,
favorite_color: record_data[3].downcase,
date_of_birth: record_data[4]}
end
end
end
35 changes: 35 additions & 0 deletions lib/pipe_parser.rb
@@ -0,0 +1,35 @@
class PipeParser
attr_accessor :raw_data

def initialize file_name
@raw_data = File.open(file_name, "r").map do |line|
line.split("|").collect!(&:strip)
end
end

def parse_record_data
@raw_data.inject([]) do |result, record_data|
result << {last_name: record_data[0].downcase,
first_name: record_data[1].downcase,
middle_name: record_data[2],
gender: normalize_gender(record_data[3]),
favorite_color: record_data[4].downcase,
date_of_birth: normalize_date(record_data[5])
}
end
end

private
def normalize_gender gender
case gender.downcase
when "f" then"female"
when "m" then "male"
else
raise ArgumentError, "Gender can only be male or female"
end
end

def normalize_date date
date.gsub("-", "/")
end
end
2 changes: 1 addition & 1 deletion lib/record.rb
Expand Up @@ -18,6 +18,6 @@ def == anotherRecord

private
def to_s
"#{self.last_name} #{self.first_name} #{self.gender} #{self.date_of_birth} #{self.favorite_color}"
"#{self.last_name.capitalize} #{self.first_name.capitalize} #{self.gender.capitalize} #{self.date_of_birth} #{self.favorite_color.capitalize}"
end
end
2 changes: 1 addition & 1 deletion spec/comma_parser_spec.rb
Expand Up @@ -9,7 +9,7 @@
describe "#parse_record_data" do
it "should parse data into record" do
subject.parse_record_data.should == [
{last_name: "foo", first_name: "bar", gender:"Male", favorite_color: "Tan", date_of_birth:"2/13/1943" }, {last_name: "baz", first_name: "qux", gender:"Male", favorite_color: "Yellow", date_of_birth:"4/23/1967" }]
{last_name: "foo", first_name: "bar", gender:"male", favorite_color: "tan", date_of_birth:"2/13/1943" }, {last_name: "baz", first_name: "qux", gender:"male", favorite_color: "yellow", date_of_birth:"4/23/1967" }]
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/pipe.txt
@@ -0,0 +1 @@
Smith | Steve | D | M | Red | 3-3-1985
17 changes: 17 additions & 0 deletions spec/pipe_parser_spec.rb
@@ -0,0 +1,17 @@
require 'pipe_parser.rb'
describe "PipeParser" do
let(:file_name) {"spec/fixtures/pipe.txt"}
subject { PipeParser.new(file_name)}
describe "#initialize" do
it "should assign content to raw_data" do
subject.raw_data.should_not be_nil
end
end

describe "#parse_record_data" do
let(:expected_data) {[{last_name: "smith", first_name:"steve", middle_name: "D", gender: "male", favorite_color: "red", date_of_birth: "3/3/1985"}]}
it "should parse raw data" do
expect(subject.parse_record_data).to eq expected_data
end
end
end

0 comments on commit 9ee65a8

Please sign in to comment.