lib/eco/api/microcases/people_load.rb in eco-helpers-2.7.16 vs lib/eco/api/microcases/people_load.rb in eco-helpers-2.7.17
- old
+ new
@@ -13,23 +13,27 @@
# - `: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`)
# @return [Eco::API::Organization::People] the `People` object with the data.
- def people_load(filename = enviro.config.people.cache, modifier: [:newest, :api])
+ def people_load(filename = enviro.config.people.cache, modifier: %i[newest api]) # rubocop:disable Metrics/AbcSize
modifier = [modifier].flatten
- load_file = [:file, :newest].any? {|flag| modifier.include?(flag)}
+ load_file = %i[file newest].any? {|flag| modifier.include?(flag)}
+
case
when filename && load_file
- if file = people_load_filename(filename, newest: modifier.include?(:newest))
+ 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)
end
else
logger.error("could not find the file #{file_manager.dir.file(filename)}")
exit unless modifier.include?(:api)
- people_load(modifier: modifier - [:newest, :file])
+
+ people_load(modifier: modifier - %i[newest file])
end
when modifier.include?(:api)
logger.info("Going to get all the people via API (load)")
start = Time.now
@@ -37,16 +41,16 @@
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)")
- if modifier.include?(:save) && people && people.length > 0
+ 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}.")
+ logger.info("#{people.length} people saved to file #{file}.")
end
end
- end.yield_self do |people|
+ end.then do |people|
Eco::API::Organization::People.new(people)
end
end
private
@@ -59,9 +63,8 @@
end
else
file_manager.dir.file(filename, should_exist: true)
end
end
-
end
end
end