Created
December 20, 2011 22:39
-
-
Save timting/1503640 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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: [] | |
} | |
customer[:locations] << { | |
name: line['name'], | |
address_lines: build_address_lines(line) | |
} | |
first = true | |
line.children.each do |location| | |
next if first | |
first = false | |
customer[:locations] << { | |
number: location['store_number'], | |
name: location['store_name'], | |
address_lines: build_address_lines(location) | |
} | |
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