Module: Eco::API::MicroCases::People::Manage::Load

Included in:
Eco::API::MicroCases::People::Manage
Defined in:
lib/eco/api/microcases/people/manage/load.rb

Instance Method Summary collapse

Instance Method Details

#people_load(filename = enviro.config.people.cache, modifier: %i[newest api])) ⇒ Eco::API::Organization::People

Note:
  • filename will be relative to the working directory (the one of the session enviro set by the user).

Helper to load People that works in different phases:

  1. first tries to get the newest cached file that follows filename pattern
    • if not the newest, it tries to find the specific filename
  2. if it succeeds to identify a cached file, it loads it
    • if it fails, it tries to get people from the server

Parameters:

  • filename (String) (defaults to: enviro.config.people.cache)

    the name of the file where the cached data is to be found.

  • modifier (Array<Symbol>) (defaults to: %i[newest api]))

    modifiers to specify how this function should proceed:

    • :newest if it should try to find the newest file (pattern alike).
    • :api if it should try to get people from the server in case there's no cache.
    • :file if it is supposed to load people from a file.
    • :save if it is supposed to cache/save the data locally once obtained people from the server (:api)

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/eco/api/microcases/people/manage/load.rb', line 21

def people_load(filename = enviro.config.people.cache, modifier: %i[newest api]) # rubocop:disable Metrics/AbcSize
  modifier  = [modifier].flatten
  load_file = %i[file newest].any? {|flag| modifier.include?(flag)}

  case
  when filename && load_file
    file = people_load_filename(filename, newest: modifier.include?(:newest))

    if file
      file_manager.load_json(file).tap do |people|
        next unless people.is_a?(Array)

        log(:info) {
          "#{people&.length} people loaded from file #{file}"
        }
      end
    else
      log(:error) {
        "could not find the file #{file_manager.dir.file(filename)}"
      }

      exit unless modifier.include?(:api)

      people_load(modifier: modifier - %i[newest file])
    end
  when modifier.include?(:api)
    log(:info) { 'Going to get all the people via API (load)' }

    start = Time.now
    session.batch.get_people.tap do |people|
      secs    = (Time.now - start).round(3)
      cnt     = people.count
      per_sec = (cnt.to_f / secs).round(2)

      log(:info) {
        "Loaded #{cnt} people in #{secs} seconds (#{per_sec} people/sec)"
      }

      if modifier.include?(:save) && people && people.length.positive?
        file = file_manager.save_json(people, filename, :timestamp)
        log(:info) { "#{people.length} people saved to file #{file}." }
      end
    end
  end.then do |people|
    Eco::API::Organization::People.new(people)
  end
end