Skip to content

Instantly share code, notes, and snippets.

@elijahc
Created March 6, 2014 21:29
Show Gist options
  • Save elijahc/9400081 to your computer and use it in GitHub Desktop.
Save elijahc/9400081 to your computer and use it in GitHub Desktop.
Subject model importer logic
require 'resque-status'
module Importer
require_relative 'importer_base'
require 'rbc'
class Pipe < ImporterBase
def initialize(key, options={})
@key = key
@options = options
#debug = options[:debug]
debug = true
@bsi = RBC.new(key, options.merge!({:debug=>true}))
@batch
@batch_id
end
def import(all_subjects, options={})
$LOG.debug "Attempting to import #{all_subjects.map{|s| s.to_bfh}.ai :plain => true}"
all_subjects.each do |s|
s = s.to_bfh
begin
exists = @bsi.subject.getSubject(s['subject.study_id'], s['subject.subject_id'])
rescue RuntimeError => e
# do nothing, exists should still be nil
# TODO: log this error
$LOG.debug e.to_s
end
# If it exists update it
if exists
puts "Subject already exists in db, checking attribute validity"
l1_pass = @bsi.subject.performL1Checks(s, s['subject.study_id'], s['subject.subject_id']).nil?
if l1_pass
puts "Subject cleared for updating"
@bsi.subject.saveSubject(s)
end
else
# If not, check if we can create a new one?
puts "Subject doesn't exist, checking attribute validity"
l1_pass = @bsi.subject.performL1Checks(s, s['subject.study_id'], s['subject.subject_id'].to_s).nil?
l2_pass = @bsi.subject.performL2Checks(s, s['subject.study_id'], s['subject.subject_id'].to_s).nil?
if l1_pass && l2_pass
puts "Subject cleared for creation"
@bsi.subject.saveNewSubject(s, s['subject.study_id'], s['subject.subject_id'])
else
raise "L1/L2 Errors: #{l1_pass} | #{l2_pass}"
end
end
end
end
def setup
end
def terminate
@bsi.common.logoff
nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment