Created
May 3, 2018 11:00
-
-
Save charusat09/c8b4310dca9c2236d20da23bcfb14c0e to your computer and use it in GitHub Desktop.
This script will read data from provided file and also use master data file to map ids to data from it.
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
require "csv" | |
require "byebug" | |
class ParseCSV | |
attr_reader :file, :store, :table | |
def initialize(file_path) | |
@file = CSV.read(file_path, headers: true, row_sep: :auto) | |
@table = CSV.read("table.csv", headers: true, row_sep: :auto) | |
@store = [] | |
end | |
def read | |
file.each_with_index do |row, i| | |
id = grep_id_from(row) | |
row << id | |
store << row.to_a.map {|a| a[1]} | |
end | |
store | |
end | |
def generate | |
attributes = %w{sr provider_input name id} | |
CSV.open("file.csv", "a+", headers: true) do |csv| | |
csv << attributes | |
store.each do |data| | |
csv << data | |
end | |
end | |
end | |
private | |
def grep_id_from(row) | |
condition = row.to_a.flatten[5] | |
bb = condition&.split("&")&.map {|d| d.split("=") }&.flatten | |
return if bb.nil? | |
if bb[0].to_i == 0 | |
fetch_id_from_master_table(bb) | |
else | |
bb.select.with_index { |_, i| i.even? && _.to_i > 0 }.join(",") | |
end | |
end | |
def fetch_id_from_master_table(bb) | |
querries = bb[0].split(",") | |
querries = querries.reject {|q| q.length < 15 } | |
collector = [] | |
querries.each do |query| | |
master_row = table.find {|row| row["Condition Name"].include? query.strip} | |
id = master_row.nil? ? "" : master_row[1] | |
collector << id | |
end | |
collector = collector.reject { |c| c.empty? }.uniq.join(",") | |
end | |
end | |
csv_parser = ParseCSV.new("data.csv") | |
store = csv_parser.read | |
csv_parser.generate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment