lib/eco/api/microcases/people_load.rb in eco-helpers-2.7.24 vs lib/eco/api/microcases/people_load.rb in eco-helpers-2.7.25

- old
+ new

@@ -2,11 +2,11 @@ module API class MicroCases # 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 identif a cached file, it loads it + # 2. if it succeeds to identify a cached file, it loads it # - if it fails, it tries to get people from the server # @note # - `filename` will be relative to the working directory (the one of the session `enviro` set by the user). # @param filename [String] the name of the file where the cached data is to be found. # @param modifier [Array<Symbol>] modifiers to specify how this function should proceed: @@ -23,31 +23,41 @@ when filename && load_file file = people_load_filename(filename, newest: modifier.include?(:newest)) if file file_manager.load_json(file).tap do |people| - logger.info("#{people&.length} people loaded from file #{file}") if people.is_a?(Array) + next unless people.is_a?(Array) + + log(:info) { + "#{people&.length} people loaded from file #{file}" + } end else - logger.error("could not find the file #{file_manager.dir.file(filename)}") + 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) - logger.info("Going to get all the people via API (load)") + 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) - logger.info("Loaded #{cnt} people in #{secs} seconds (#{per_sec} people/sec)") + 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) - logger.info("#{people.length} people saved to file #{file}.") + log(:info) { "#{people.length} people saved to file #{file}." } end end end.then do |people| Eco::API::Organization::People.new(people) end @@ -57,10 +67,14 @@ def people_load_filename(filename, newest: false) if newest # search input file based on pattern (in case the name has a timestamp) file_manager.dir.newest_file(file: filename).tap do |file| - logger.info("previous file found: #{file}") if file + next unless file + + log(:info) { + "previous file found: #{file}" + } end else file_manager.dir.file(filename, should_exist: true) end end