lib/updateagent/updateagent.rb in onebody-updateagent-0.4.0 vs lib/updateagent/updateagent.rb in onebody-updateagent-0.4.1
- old
+ new
@@ -28,11 +28,11 @@
if invalid = @data.detect { |row| row['id'] }
puts "Error: one or more records contain an 'id' column."
puts "You must utilize 'legacy_id' rather than 'id' so that"
puts "identity and foreign keys are maintained from your"
puts "existing membership management database."
- exit
+ exit(1)
end
end
# load data from csv file and do some type conversion for bools and dates
# first row must be attribute names
@@ -69,13 +69,18 @@
end
hash[key] = value
end
record_count += 1
print "reading record #{record_count}\r"
- hash['deleted'] = false
- hash
+ if hash.any?
+ hash['deleted'] = false
+ hash
+ else
+ nil
+ end
end
+ @data.compact!
puts
@attributes << 'deleted'
@attributes.reject! { |a| IGNORE_ATTRIBUTES.include?(a) }
end
@@ -126,11 +131,13 @@
response = resource.post(:batch, {}, records.to_xml)
statuses = Hash.from_xml(response.body)['records']
statuses.each do |status|
record = data_by_id[status['legacy_id']]
record['id'] = status['id']
- if status['status'] == 'error'
+ record['name'] = status['name']
+ record['status'] = status['status']
+ if status['error']
puts "#{status['legacy_id']}: #{status['error']}"
@errors << {:record => record, :error => status['error']}
record['error_messages'] = status['error']
end
end
@@ -164,11 +171,11 @@
end
end
end
def start_sync
- @sync = Sync.create(:complete => false)
+ @sync = Sync.create(:complete => false, :started_at => Time.now)
end
def finish_sync(create_items=true, mark_complete=true)
if create_items
@sync.post(
@@ -180,28 +187,29 @@
).to_xml
)
end
if mark_complete
@sync.complete = true
+ @sync.finished_at = Time.now
@sync.error_count = errors.length
@sync.success_count = (@create + @update).length - errors.length
@sync.save
end
end
def to_sync_item(record, operation)
h = {
:syncable_type => self.resource.name,
:syncable_id => record['id'],
- :name => name_for(record),
+ :name => record['name'],
:legacy_id => record['legacy_id'],
:operation => operation,
- :status => record['error_messages'] ? 'error' : 'success'
+ :status => record['status']
}
if record['error_messages']
- h[:error_messages] = record['error_messages'].split('; ')
+ h[:error_messages] = record['error_messages']
end
- return h
+ return h
end
def data_by_id
@data_by_id ||= begin
by_id = {}