class BrregGrunndata::Utils::ConcurrentOperations

Runs operations against a service or client concurrently

Public Class Methods

new(service_or_client, operations, args) click to toggle source
# File lib/brreg_grunndata/utils.rb, line 9
def initialize(service_or_client, operations, args)
  @service_or_client = service_or_client
  @operations = operations
  @args = args
end

Public Instance Methods

call() click to toggle source

Calls all operations

NOTE There hasn't been put much effort in to this class, so

when it comes to timeour or error handling you may encounter
situaitons it does not currently handle

@return Array of all results. Same order as operations where given.

# File lib/brreg_grunndata/utils.rb, line 22
def call
  results = []

  threads = @operations.each_with_index.map do |operation, index|
    Thread.new do
      results[index] = call_on_service_or_client operation
    end
  end

  threads.each(&:join)

  results
end

Private Instance Methods

call_on_service_or_client(operation) click to toggle source
# File lib/brreg_grunndata/utils.rb, line 38
def call_on_service_or_client(operation)
  unless @service_or_client.respond_to? operation
    raise UnkownOperationError, "#{@service_or_client} does not respond to #{operation}"
  end

  @service_or_client.public_send operation, @args
end