Module: Eco::API::Session::Batch::Searcher

Included in:
Eco::API::Session::Batch
Defined in:
lib/eco/api/session/batch/searcher.rb

Instance Method Summary collapse

Instance Method Details

#get_people(people = nil, params: {}, silent: false, options: self.options) ⇒ Array<People>

Note:
  • If people is given keys page: and q of params:.

Gets the people of the organization according params. If people is not nil, scopes to only the people specified.

Parameters:

  • people (Nil, People, Enumerable<Person>, Enumerable<Hash>) (defaults to: nil)

    target People to launch the batch against.

  • params (Hash) (defaults to: {})

    api request options.

Options Hash (params:):

  • :page (String)

    the page number page based on :per_page.

  • :per_page (String)

    the number of people included per each batch api request.

  • :q (String)

    some text to search. Omit this parameter to target all the people.

Returns:

  • (Array<People>)

    all the people based on params



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eco/api/session/batch/searcher.rb', line 29

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

#search(data, silent: false, params: {}, options: self.options) ⇒ Object

rubocop:disable Metrics/AbcSize



41
42
43
44
45
46
47
48
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/eco/api/session/batch/searcher.rb', line 41

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