-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rakefile
104 lines (86 loc) · 2.67 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
$LOAD_PATH.push './lib'
require 'fileutils'
require 'csv'
require 'grnds/ediot'
require 'grnds/file_faker'
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
namespace :sample_data do
desc 'Makes a single sample data file for 834 testing'
task :generate do |t|
dirname = 'tmp'
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
Grnds::FileFaker::Filename.new.each do |filename|
path = File.join(dirname, filename)
File.open(path,'w') do |file|
faker = Grnds::FileFaker::Faux834.new(employee_count: 1000)
faker.render(file)
puts faker.render_meta
puts "Wrote '#{path}'"
end
end
end
desc 'Makes a sample Walmart multipart data file for 834 testing'
task :generate_all do |t|
dirname = 'tmp'
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
Grnds::FileFaker::WalmartMultipartFilename.new.each do |filename|
path = File.join(dirname, filename)
File.open(path,'w') do |file|
faker = Grnds::FileFaker::Faux834.new(employee_count: 1000)
faker.render(file)
puts faker.render_meta
puts "Wrote '#{path}'"
end
end
end
end
namespace :demo do
INPUT_FILE_PATH = 'tmp/834_fake_file.txt'
OUTPUT_FILE_PATH = 'tmp/834_fake_file.csv'
DEFINITION = {
INS: {size: 17},
REF: {occurs: 5, size: 2},
DTP: {occurs: 10, size: 3},
NM1: {occurs: 2, size: 9},
PER: {size: 8},
N3: {size: 2},
N4: {size: 3},
DMG: {size: 3},
HLH: {size: 3},
HD: {size: 5},
AMT: {size: 2},
}.freeze
desc 'Parse sample 834 file to a csv'
task :parse_to_csv do |t|
start_time = Time.now
row_count = 0
puts "Reading '#{INPUT_FILE_PATH}'"
print_file_stats(INPUT_FILE_PATH)
file_enum = Grnds::Ediot::Parser.lazy_file_stream(INPUT_FILE_PATH)
File.open(OUTPUT_FILE_PATH, 'w') do |out_file|
parser = Grnds::Ediot::Parser.new(DEFINITION)
column_headers = parser.row_keys
out_file << CSV::Row.new(column_headers,column_headers, true).to_s
parser.parse(file_enum) do |row|
print '.' if row_count % 1000 == 0
row_count += 1
out_file << CSV::Row.new(column_headers, row)
end
end
print_run_stats(row_count, start_time)
puts "Wrote to '#{OUTPUT_FILE_PATH}'"
end
def print_file_stats(input_file_path)
s = File.stat(input_file_path)
file_mb = s.size/1000.0/1000.0
puts "Input Filesize: %5.2f MB" % [file_mb]
end
def print_run_stats(row_count, start_time)
puts 'Done!'
seconds = Time.now - start_time
puts "Processed file with %d rows in %5.2f seconds" % [row_count, seconds]
end
end