Class: Remi::DataSource::Salesforce

Inherits:
Remi::DataSubject show all
Includes:
DataStub, Remi::DataSubject::DataSource, Remi::DataSubject::Salesforce
Defined in:
lib/remi/cucumber/data_source.rb,
lib/remi/data_subject/salesforce.rb

Overview

Hmmm.… this gets called first because I’m trying to split SF off as a “plugin”

Instance Attribute Summary

Attributes inherited from Remi::DataSubject

#fields

Instance Method Summary collapse

Methods included from Remi::DataSubject::Salesforce

#field_symbolizer, #restforce_client

Methods included from Remi::DataSubject::DataSource

#df, #extract

Methods included from DataStub

#empty_stub_df, #stub_df, #stub_row_array, #stub_values

Methods inherited from Remi::DataSubject

#df, #df=, #field_symbolizer

Constructor Details

#initialize(*args, **kargs, &block) ⇒ Salesforce

Returns a new instance of Salesforce.



28
29
30
31
# File 'lib/remi/data_subject/salesforce.rb', line 28

def initialize(*args, **kargs, &block)
  super
  init_salesforce(*args, **kargs, &block)
end

Instance Method Details

#extract!Object

Public: Called to extract data from the source.

Returns data in a format that can be used to create a dataframe.



36
37
38
39
40
41
# File 'lib/remi/data_subject/salesforce.rb', line 36

def extract!
  @extract = sf_bulk.query(@sfo, @query, 10000)

  check_for_errors(@extract)
  @extract
end

#sf_bulkObject



43
44
45
# File 'lib/remi/data_subject/salesforce.rb', line 43

def sf_bulk
  @sf_bulk ||= SalesforceBulkApi::Api.new(restforce_client).tap { |o| o.connection.set_status_throttle(5) }
end

#to_dataframeObject

Public: Converts extracted data to a dataframe. Currently only supports Daru DataFrames.

Returns a Remi::DataFrame



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/remi/data_subject/salesforce.rb', line 51

def to_dataframe
  @logger.info "Converting salesforce query results to a dataframe"

  hash_array = {}
  extract['batches'].each do |batch|
    next unless batch['response']

    batch['response'].each do |record|
      record.each do |field, value|
        next if ['xsi:type','type'].include? field
        (hash_array[field.to_sym] ||= []) << case value.first
          when Hash
            value.first["xsi:nil"] == "true" ? nil : value.first
          else
            value.first
          end
      end
    end

    # delete raw result at end of processing to free memory
    batch['response'] = nil
  end

  Remi::DataFrame.create(@remi_df_type, hash_array, order: hash_array.keys)
end