require_relative 'launcher/valid_methods' require_relative 'launcher/options' module Eco module API class Session class Batch module Searcher def self.included(base) unless base <= Eco::API::Session::Batch msg = "To be included only in Eco::API::Common::Session::BaseSession. " msg << "Tried on '#{base}'" raise msg end super end # 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, Enumerable] 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] all the people based on `params` def get_people(people = nil, params: {}, silent: false, options: self.options) return get(params: params, silent: silent, options: options) unless people.is_a?(Enumerable) launch( people, method: :get, params: params, silent: silent, options: options ).people end def search(data, silent: false, params: {}, options: self.options) # rubocop:disable Metrics/AbcSize params = {per_page: batch_size(options)}.merge(params) launch( data, method: :get, params: params, silent: silent, options: options ).tap do |status| status.mode = :search entries = status.queue puts "\n" entries.each_with_index do |entry, i| if (i % 10).zero? percent = i * 100 / entries.length print "Searching: #{percent.round}% (#{i}/#{entries.length} entries)\r" $stdout.flush end next if status.success?(entry) email = nil if entry.respond_to?(:email) email = entry.email elsif entry.respond_to?(:to_h) email = entry.to_h["email"] end people_matching = [] email = email.to_s.strip.downcase unless email.empty? people_matching = get( params: params.merge(q: email), silent: silent, options: options ).select do |person| person.email == email end end case people_matching.length when 1 status.set_person_match(entry, people_matching.first) when 2..Float::INFINITY status.set_people_match(entry, people_matching) end end end end private def get(params: {}, silent: false, options: self.options) msg = "cannot batch get without api connnection, please provide a valid api connection!" fatal msg unless (people_api = api&.people) params = {per_page: batch_size(options)}.merge(params) people_api.get_all(params: params, silent: silent) end end end end end end