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
-
#people_load(filename = enviro.config.people.cache, modifier: %i[newest api])) ⇒ Eco::API::Organization::People
Helper to load
Peoplethat works in different phases: 1.
Instance Method Details
#people_load(filename = enviro.config.people.cache, modifier: %i[newest api])) ⇒ Eco::API::Organization::People
Note:
filenamewill be relative to the working directory (the one of the sessionenviroset by the user).
Helper to load People that works in different phases:
- first tries to get the newest cached file that follows
filenamepattern- if not the newest, it tries to find the specific filename
- if it succeeds to identify a cached file, it loads it
- if it fails, it tries to get people from the server
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 |