class Eco::API::Session::Batch

Constants

DEFAULT_BATCH_BLOCK
VALID_METHODS

Public Class Methods

valid_method?(value) click to toggle source

@return [Boolean] `true` if the method is supported, `false` otherwise.

# File lib/eco/api/session/batch.rb, line 11
def valid_method?(value)
  VALID_METHODS.include?(value)
end

Public Instance Methods

get_people(people = nil, params: {}, silent: false) click to toggle source

Gets the people of the organization according `params`.

If `people` is not `nil`, scopes to only the people specified.

@note

- If `people` is given keys `page:` and `q` of `params:`.

@param people [Nil, People, Enumerable<Person>, Enumerable<Hash>] target People to launch the batch against. @param params [Hash] api request options. @option params [String] :page the page number `page` based on `:per_page`. @option params [String] :per_page the number of people included per each batch api request. @option params [String] :q some text to search. Omit this parameter to target all the people. @return [Array<People>] all the people based on `params`

# File lib/eco/api/session/batch.rb, line 26
def get_people(people = nil, params: {}, silent: false)
  return launch(people, method: :get, params: params, silent: silent).people if people.is_a?(Enumerable)
  return get(params: params, silent: silent)
end
launch(people, method:, params: {} , silent: false) click to toggle source

launches a batch of `method` type using `people` and the specified `params` @raise Exception

- if `people` is `nil` or is not an `Enumerable`.
- if there's no `api` connection linked to the current `Batch`.

@param people [People, Enumerable<Person>, Enumerable<Hash>] target People to launch the batch against. @param method [Symbol] the method to launch the batch api request with. @param params [Hash] api request options. @option params [String] :per_page the number of people included per each batch api request. @return [Batch::Status] the `status` of this batch launch.

# File lib/eco/api/session/batch.rb, line 40
def launch(people, method:, params: {} , silent: false)
  batch_from(people, method: method, params: params, silent: silent)
end

Private Instance Methods

batch_from(data, method:, params: {}, silent: false) click to toggle source
# File lib/eco/api/session/batch.rb, line 95
def batch_from(data, method:, params: {}, silent: false)
  fatal "Invalid batch method: #{method}." if !self.class.valid_method?(method)
  return nil if !data || !data.is_a?(Enumerable)
  fatal "cannot batch #{method} without api connnection, please provide a valid api connection!" unless people_api = api&.people

  # param q does not make sense here, even for GET method
  params   = {per_page: DEFAULT_BATCH_BLOCK}.merge(params)
  per_page = params[:per_page] || DEFAULT_BATCH_BLOCK

  iteration  = 1; done = 0
  iterations = (data.length.to_f / per_page).ceil

  Eco::API::Session::Batch::Status.new(enviro, queue: data, method: method).tap do |status|
    start_time  = Time.now
    start_slice = Time.now; slice = []
    data.each_slice(per_page) do |slice|
      msg  = "starting batch '#{method}' iteration #{iteration}/#{iterations},"
      msg += " with #{slice.length} entries of #{data.length} -- #{done} done"
      msg += " (last: #{str_stats(start_slice, slice.length)}; total: #{str_stats(start_time, done)})"
      logger.info(msg) unless silent

      start_slice = Time.now
      offer_retry_on(Ecoportal::API::Errors::TimeOut) do
        people_api.batch do |batch|
          slice.each do |person|
            batch.public_send(method, person) do |response|
              faltal("Request with no response") unless !!response
              status[person] = response
            end
          end
        end # end batch
      end

      iteration  += 1
      done       += slice.length
    end # next slice
  end
end
get(params: {}, silent: false) click to toggle source
# File lib/eco/api/session/batch.rb, line 89
def get(params: {}, silent: false)
  fatal "cannot batch get without api connnection, please provide a valid api connection!" unless people_api = api&.people
  params = {per_page: DEFAULT_BATCH_BLOCK}.merge(params)
  return people_api.get_all(params: params, silent: silent)
end
offer_retry_on(error_type, retries_left = 3, &block) click to toggle source
# File lib/eco/api/session/batch.rb, line 134
def offer_retry_on(error_type, retries_left = 3, &block)
  begin
    block.call
  rescue error_type => e
    raise unless retries_left > 0
    explanation = "Batch TimeOut. You have #{retries_left} retries left."
    prompt_user(" Do you want to retry (y/N)?", default: "Y", explanation: explanation, timeout: 10) do |response|
      if response.upcase.start_with?("Y")
        puts "\nOkay... let's retry!"
        offer_retry_on(error_type, retries_left - 1, &block)
      else
        raise
      end
    end
  end
end
str_stats(start, count) click to toggle source
# File lib/eco/api/session/batch.rb, line 151
def str_stats(start, count)
  now     = Time.now
  secs    = (now - start).round(3)
  if secs > 0.0
    per_sec = (count.to_f / secs).round(2)
    "#{secs}s -> #{per_sec} people/s"
  else
    " -- "
  end
end