Skip to content

Instantly share code, notes, and snippets.

@lyleunderwood
Forked from timting/gist:1503640
Created December 20, 2011 22:41
Show Gist options
  • Save lyleunderwood/1503647 to your computer and use it in GitHub Desktop.
Save lyleunderwood/1503647 to your computer and use it in GitHub Desktop.
def build_address_lines(record)
lines = []
%w{store_name address1 address2 address3}.each do |field|
lines << record[field].strip unless record[field].blank?
end
last_line = ''
%w{city state zip country}.each do |field|
last_line << ' ' + record[field].strip unless record[field].blank?
end
lines << last_line unless last_line.blank?
lines
end
csv.map do |line|
customer = {
name: line['name'].strip,
number: line['number'].strip,
region: line['region'].strip,
inventory_source: line['source'].strip,
access_key: line['key'],
address_lines: build_address_lines(line),
locations: []
}
first = true
line.children.each do |location|
customer[:locations] << {
number: first ? '' : location['store_number'],
name: location['store_name'],
address_lines: build_address_lines(location)
}
first = false
end
if customer_record = Customer.where(number: customer[:number]).first
customer_record.update_attributes!(customer)
else
Customer.create!(customer)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment