Class: Eco::API::Session::Batch

Inherits:
Common::Session::BaseSession show all
Defined in:
lib/eco/api/session/batch.rb,
lib/eco/api/session/batch/job.rb,
lib/eco/api/session/batch/jobs.rb,
lib/eco/api/session/batch/errors.rb,
lib/eco/api/session/batch/status.rb,
lib/eco/api/session/batch/feedback.rb,
lib/eco/api/session/batch/policies.rb,
lib/eco/api/session/batch/base_policy.rb,
lib/eco/api/session/batch/jobs_groups.rb,
lib/eco/api/session/batch/request_stats.rb

Defined Under Namespace

Classes: BasePolicy, Errors, Feedback, Job, Jobs, JobsGroups, Policies, RequestStats, Status

Constant Summary collapse

DEFAULT_BATCH_SIZE =
50
DEFAULT_JOB_SIZE =
100
VALID_METHODS =
%i[get create update upsert delete].freeze

Instance Attribute Summary

Attributes inherited from Common::Session::BaseSession

#config, #environment, #session

Attributes included from Language::AuxiliarLogger

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Common::Session::BaseSession

#api, #api?, #fatal, #file_manager, #initialize, #logger, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?

Methods included from Language::AuxiliarLogger

#log

Constructor Details

This class inherits a constructor from Eco::API::Common::Session::BaseSession

Class Method Details

.valid_method?(value) ⇒ Boolean

Returns true if the method is supported, false otherwise.

Returns:

  • (Boolean)

    true if the method is supported, false otherwise.



11
12
13
# File 'lib/eco/api/session/batch.rb', line 11

def valid_method?(value)
  VALID_METHODS.include?(value)
end

Instance Method Details

#batch_mode(opts = options) ⇒ Symbol

Returns the batch mode to run.

Returns:

  • (Symbol)

    the batch mode to run



23
24
25
# File 'lib/eco/api/session/batch.rb', line 23

def batch_mode(opts = options)
  opts.dig(:workflow, :batch, :mode) || :batch
end

#batch_size(opts = options) ⇒ Object



16
17
18
19
20
# File 'lib/eco/api/session/batch.rb', line 16

def batch_size(opts = options)
  return self.class::DEFAULT_JOB_SIZE if job_mode?(opts)

  self.class::DEFAULT_BATCH_SIZE
end

#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



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/eco/api/session/batch.rb', line 42

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

#job_mode?(opts = options) ⇒ Boolean

Returns are we running in :job mode?.

Returns:

  • (Boolean)

    are we running in :job mode?



28
29
30
# File 'lib/eco/api/session/batch.rb', line 28

def job_mode?(opts = options)
  batch_mode(opts) == :job
end

#launch(people, method:, params: {}, silent: false, options: self.options) ⇒ Batch::Status

launches a batch of method type using people and the specified params

Parameters:

  • people (People, Enumerable<Person>, Enumerable<Hash>)

    target People to launch the batch against.

  • method (Symbol)

    the method to launch the batch api request with.

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

    api request options.

Options Hash (params:):

  • :per_page (String)

    the number of people included per each batch api request.

Returns:

Raises:

  • Exception

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


63
64
65
66
67
68
69
70
71
# File 'lib/eco/api/session/batch.rb', line 63

def launch(people, method:, params: {}, silent: false, options: self.options)
  batch_from(
    people,
    method:  method,
    params:  params,
    silent:  silent,
    options: options
  )
end

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

rubocop:disable Metrics/AbcSize



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/eco/api/session/batch.rb', line 73

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