Class: SalesforceBulkQuery::Api
- Inherits:
-
Object
- Object
- SalesforceBulkQuery::Api
- Defined in:
- lib/salesforce_bulk_query.rb
Overview
Abstracts the whole library, class the user interacts with
Constant Summary collapse
- CHECK_INTERVAL =
30
- @@DEFAULT_API_VERSION =
'29.0'
Instance Method Summary collapse
-
#initialize(client, options = {}) ⇒ Api
constructor
Constructor.
-
#instance_url ⇒ Object
Get the Salesforce instance URL.
-
#query(sobject, soql, options = {}) ⇒ Object
Query the Salesforce API.
-
#start_query(sobject, soql, options = {}) ⇒ Object
Start the query (synchronous method).
Constructor Details
#initialize(client, options = {}) ⇒ Api
Constructor
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/salesforce_bulk_query.rb', line 20 def initialize(client, ={}) @logger = [:logger] api_version = [:api_version] || @@DEFAULT_API_VERSION # use our own logging middleware if logger passed if @logger && client.respond_to?(:middleware) client.middleware.use(SalesforceBulkQuery::Logger, @logger, ) end # initialize connection @connection = SalesforceBulkQuery::Connection.new(client, api_version, @logger, [:filename_prefix]) end |
Instance Method Details
#instance_url ⇒ Object
Get the Salesforce instance URL
35 36 37 38 39 40 |
# File 'lib/salesforce_bulk_query.rb', line 35 def instance_url # make sure it ends with / url = @connection.client.instance_url url += '/' if url[-1] != '/' return url end |
#query(sobject, soql, options = {}) ⇒ Object
Query the Salesforce API. It’s a blocking method - waits until the query is resolved can take quite some time
49 50 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/salesforce_bulk_query.rb', line 49 def query(sobject, soql, ={}) @logger.info "Running query: #{soql}. Gem version salesforce_bulk_query: #{SalesforceBulkQuery::VERSION}" if @logger check_interval = [:check_interval] || CHECK_INTERVAL time_limit = [:time_limit] # in seconds start_time = Time.now # start the machinery query = start_query(sobject, soql, ) results = nil loop do # get available results and check the status results = query.get_available_results() @logger.debug "get_available_results: #{results}" # if finished get the result and we're done if results[:succeeded] # we're done @logger.info "Query succeeded. Results: #{results}" if @logger break end # if we've run out of time limit, go away if time_limit && (Time.now - start_time > time_limit) @logger.warn "Ran out of time limit, downloading what's available and terminating" if @logger @logger.info "Downloaded the following files: #{results[:filenames]} The following didn't finish in time: #{results[:unfinished_subqueries]}." if @logger break end @logger.info "Sleeping #{check_interval}" if @logger @logger.info "Downloaded files: #{results[:filenames].length} Jobs in progress: #{query.jobs_in_progress.length}" sleep(check_interval) end # nice list of files to log if @logger && ! results[:filenames].empty? @logger.info "Download finished. Downloaded files in #{File.dirname(results[:filenames][0])}. Filename size [line count]:" @logger.info "\n" + results[:filenames].sort.map{|f| "#{File.basename(f)} #{File.size(f)} #{Utils.line_count(f) if [:count_lines]}"}.join("\n") end return results end |
#start_query(sobject, soql, options = {}) ⇒ Object
Start the query (synchronous method)
98 99 100 101 102 103 |
# File 'lib/salesforce_bulk_query.rb', line 98 def start_query(sobject, soql, ={}) # create the query, start it and return it query = SalesforceBulkQuery::Query.new(sobject, soql, @connection, {:logger => @logger}.merge()) query.start() return query end |