module Eco module API class Session class Task < API::Common::Session::BaseSession NEWEST_FILE_MODE = [:newest, :last_file, :previous] LOAD_FILE_MODE = [:file] SAVE_FILE_MODE = [:save] API_MODE = [:api, :api_get] def file_people(filename = enviro.config.people.cache) logger.info("Going to get all the people via API") people = session.batch.get_people file = file_manager.save_json(people, filename, :timestamp) logger.info("#{people.length} people loaded and saved locally to #{file}.") Eco::API::Organization::People.new(people) end def load_people(filename = enviro.config.people.cache, modifier: [:newest, :api]) modifier = [modifier].flatten people = [] case when !!filename && (load_file?(modifier) || newest_file?(modifier)) case when newest_file?(modifier) # search input file based on pattern (in case the name has a timestamp) file = file_manager.dir.newest_file(file: filename) logger.info("previous file found: #{file}") if !!file else file = file_manager.dir.file(filename) end if !file logger.error("could not find the file #{file_manager.dir.file(filename)}") exit if !use_api?(modifier) people = self.load_people(modifier: modifier - NEWEST_FILE_MODE - LOAD_FILE_MODE) else people = file_manager.load_json(file) if !!people && people.is_a?(Array) logger.info("#{people&.length} people loaded from file #{file}") end end when use_api?(modifier) # no previous file: use API to get all people logger.info("Going to get all the people via API") people = session.batch.get_people if save_file?(modifier) && people && people.length > 0 file = file_manager.save_json(people, filename, :timestamp) logger.info("#{people.length } people saved to file #{file}.") end end Eco::API::Organization::People.new(people) end private # MODIFIERS def use_api?(modifier) modifiers = [modifier].flatten modifiers.any? { |m| API_MODE.include?(m) } end def load_file?(modifier) modifiers = [modifier].flatten modifiers.any? { |m| LOAD_FILE_MODE.include?(m) } end def newest_file?(modifier) modifiers = [modifier].flatten modifiers.any? { |m| NEWEST_FILE_MODE.include?(m) } end def save_file?(modifier) modifiers = [modifier].flatten modifiers.any? { |m| SAVE_FILE_MODE.include?(m) } end end end end end