jobs/sample_job.rb in remi-0.2.42 vs jobs/sample_job.rb in remi-0.3.0

- old
+ new

@@ -1,90 +1,120 @@ # This is an example Remi job that was auto-generated by Remi. require_relative 'all_jobs_shared' -require 'remi/data_subject/salesforce' +require 'remi/data_subjects/salesforce' -class SampleJob - include AllJobsShared +class SampleJob < AllJobsShared - define_source :existing_contacts, Remi::DataSource::Salesforce, - object: :Contact, - credentials: params[:salesforce_credentials], - api: :bulk, - fields: { - :Id => {}, - :External_ID__c => {}, - :IsActive => { type: :boolean }, - :CreatedDate => { type: :date, in_format: '%Y-%m-%d %H:%M:%S' } - }, - query: <<-EOQ - SELECT - Id, - External_ID__c - FROM - Contact - EOQ + param :program_name_lookup do + RegexSieve.new( + { + /^BIO$/ => "Biology", + /^Fake Biology$/ => nil, + /(?:B|Microb)iology/ => "Biology", + /^CHEM$/ => "Chemistry", + /Chemistry/ => "Chemistry", + /Physics/ => "Physics" + } + ) + end + source :existing_contacts do + extractor Remi::Extractor::Salesforce.new( + object: :Contact, + credentials: params[:salesforce_credentials], + api: :bulk, + query: <<-EOQ + SELECT + Id, + External_ID__c, + IsActive, + CreatedDate + FROM + Contact + EOQ + ) + parser Remi::Parser::Salesforce.new - define_source :sample_file, Remi::DataSource::CsvFile, - extractor: Remi::Extractor::SftpFile.new( + field_symbolizer :salesforce + fields( + { + :Id => {}, + :External_ID__c => {}, + :IsActive => { type: :boolean }, + :CreatedDate => { type: :date, in_format: '%Y-%m-%d %H:%M:%S' } + } + ) + end + + source :sample_file do + extractor Remi::Extractor::SftpFile.new( credentials: params[:sftp], remote_path: '/', pattern: /^SampleFile_(\d+)\.txt/, most_recent_only: true - ), - csv_options: { - headers: true, - col_sep: ",", - encoding: "ISO-8859-1:UTF-8" - }, - fields: { - :student_id => {}, - :school_id => {}, - :school_name => {}, - :program => {}, - :last_name => {}, - :first_name => {}, - :current_email => {}, - :mailing_address_line_1 => {}, - :mailing_address_line_2 => {}, - :mailing_city => {}, - :mailing_state => {}, - :mailing_postal_code => {}, - :birthdate => { type: :date, in_format: '%m/%d/%Y'}, - :applied_date => { type: :date, in_format: '%m/%d/%Y'} - } + ) - define_target :all_contacts, Remi::DataTarget::DataFrame + parser Remi::Parser::CsvFile.new( + csv_options: { + headers: true, + col_sep: ",", + encoding: "ISO-8859-1:UTF-8" + } + ) - define_target :contact_updates, Remi::DataTarget::Salesforce, - credentials: params[:salesforce_credentials], - object: :Contact, - operation: :update, - api: :bulk + fields( + { + :student_id => {}, + :school_id => {}, + :school_name => {}, + :program => {}, + :last_name => {}, + :first_name => {}, + :current_email => {}, + :mailing_address_line_1 => {}, + :mailing_address_line_2 => {}, + :mailing_city => {}, + :mailing_state => {}, + :mailing_postal_code => {}, + :birthdate => { type: :date, in_format: '%m/%d/%Y'}, + :applied_date => { type: :date, in_format: '%m/%d/%Y'} + } + ) + end - define_target :contact_creates, Remi::DataTarget::Salesforce, - credentials: params[:salesforce_credentials], - object: :Contact, - operation: :create, - api: :bulk - define_param :program_name_lookup, RegexSieve.new({ - /^BIO$/ => "Biology", - /^Fake Biology$/ => nil, - /(?:B|Microb)iology/ => "Biology", - /^CHEM$/ => "Chemistry", - /Chemistry/ => "Chemistry", - /Physics/ => "Physics" - }) + target :all_contacts - define_transform :map_common_fields, sources: [:sample_file, :existing_contacts], targets: :all_contacts do + target :contact_updates do + encoder Remi::Encoder::Salesforce.new + loader Remi::Loader::Salesforce.new( + credentials: params[:salesforce_credentials], + object: :Contact, + operation: :update, + api: :bulk + ) + field_symbolizer :salesforce + end + target :contact_creates do + encoder Remi::Encoder::Salesforce.new + loader Remi::Loader::Salesforce.new( + credentials: params[:salesforce_credentials], + object: :Contact, + operation: :create, + api: :bulk + ) + field_symbolizer :salesforce + end + + + transform :map_common_fields do # Exclude all source records with an invalid program name all_contacts.df = sample_file.df.dup Remi::SourceToTargetMap.apply(all_contacts.df) do map source(:program) .target(:Major__c) - .transform(Remi::Transform::Lookup.new(params[:program_name_lookup])) + .transform(Remi::Transform::Lookup.new(job.params[:program_name_lookup])) end all_contacts.df = all_contacts.df.where(all_contacts.df[:Major__c].not_eq(nil)) student_id_to_sf_id = existing_contacts.df.map_rows { |row| [row[:External_ID__c], row[:Id]] }.to_h @@ -100,12 +130,11 @@ }) end end - define_transform :map_creates, sources: :all_contacts, targets: :contact_creates do - + transform :map_creates do work_contact_creates = all_contacts.df.where(all_contacts.df[:Id].eq(nil)) Remi::SourceToTargetMap.apply(work_contact_creates) do map source(:school_id) .target(:School_ID__c) @@ -164,10 +193,10 @@ :Birthdate, :Applied_Date__c ] end - define_transform :map_updates, sources: :all_contacts, targets: :contact_updates do + transform :map_updates do contact_updates.df = all_contacts.df[ :Id, :Major__c ].where(all_contacts.df[:Id].not_eq(nil)) end