Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Smith | Steve | D | M | Red | 3-3-1985 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |