Skip to content

Commit

Permalink
refactors translation_map_fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Feb 27, 2024
1 parent b39256c commit 99fdfb3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 54 deletions.
36 changes: 25 additions & 11 deletions umich_catalog_indexing/lib/jobs/electronic_collections.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
module Jobs
module ElectronicCollections
def self.generate_translation_map
fetch.to_yaml(line_width: 1000)
end
class << self
# @returns [String] name of the translation map
def name
"Electronic Collections"
end

def self.fetch
response = AlmaRestClient.client.get_report(path: "/shared/University of Michigan 01UMICH_INST/Reports/apps/library-search/electronic-collections")
List.new(response.body).to_h
end
# @returns [String] where in the translation map directory the file
# should go
def file_path
File.join("umich", "electronic_collections.yaml")
end

# This is used for debugging.
def self.all
response = AlmaRestClient.client.get_report(path: "/shared/University of Michigan 01UMICH_INST/Reports/apps/library-search/electronic-collections")
List.new(response.body)
# @returns [String] YAML string of translation map
def generate_translation_map
fetch.to_yaml(line_width: 1000)
end

def fetch
response = AlmaRestClient.client.get_report(path: "/shared/University of Michigan 01UMICH_INST/Reports/apps/library-search/electronic-collections")
List.new(response.body).to_h
end

# This is used for debugging.
def all
response = AlmaRestClient.client.get_report(path: "/shared/University of Michigan 01UMICH_INST/Reports/apps/library-search/electronic-collections")
List.new(response.body)
end
end

class List
Expand Down
18 changes: 16 additions & 2 deletions umich_catalog_indexing/lib/jobs/lib_loc_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@

module Jobs
module LibLocInfo
def self.generate_translation_map
LibraryLocationList.new.list.to_yaml(line_width: 1000)
class << self
# @returns [String] name of the translation map
def name
"Library and Location Information"
end

# @returns [String] where in the translation map directory the file
# should go
def file_path
File.join("umich", "libLocInfo.yaml")
end

# @returns [String] YAML string of translation map
def generate_translation_map
LibraryLocationList.new.list.to_yaml(line_width: 1000)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,46 @@ module Jobs
module Utilities
class TranslationMapFetcher
def initialize(
lib_loc_info_klass: Jobs::LibLocInfo,
electronic_collections_klass: Jobs::ElectronicCollections,
high_level_browse_klass: HighLevelBrowse,
translation_map_generators: [
Jobs::LibLocInfo,
Jobs::ElectronicCollections
],
translation_map_dir: "/app/lib/translation_maps"
)
@logger = S.logger
@lib_loc_info_klass = lib_loc_info_klass
@electronic_collections_klass = electronic_collections_klass
@high_level_browse_klass = high_level_browse_klass
@translation_map_generators = translation_map_generators
@translation_map_dir = translation_map_dir
end

def run
@logger.info "fetching high level browse file"
fetch_high_level_browse
@logger.info "fetching library and location info"
fetch_lib_loc_info
@logger.info "fetching electronic collections info"
fetch_electronic_collections
@translation_map_generators.each do |klass|
@logger.info "fetching #{klass.name}"
fetch_translation_map(klass: klass)
end
end

private

def fetch_high_level_browse
if should_fetch?(hlb_file)
@high_level_browse_klass.fetch_and_save(dir: hlb_dir)
@high_level_browse_klass.fetch_and_save(dir: @translation_map_dir)
@logger.info "updated #{hlb_file}"
else
@logger.info "#{hlb_file} is less than one day old. Did not update"
end
end

def fetch_lib_loc_info
fetch_translation_map(path: lib_loc_info_file, fetcher: lambda { @lib_loc_info_klass.generate_translation_map })
end

def fetch_electronic_collections
fetch_translation_map(path: electronic_collection_file, fetcher: lambda { @electronic_collections_klass.generate_translation_map })
end

# @param path [String] [path to where the translation map should be saved]
# @param fetcher [[Proc]] [The block of code that generates the string to
# @param klass [Class] Translation Map Generater Class
# be written to a file]
def fetch_translation_map(path:, fetcher:)
def fetch_translation_map(klass:)
path = File.join(@translation_map_dir, klass.file_path)
if should_fetch?(path)
temporary_path = "#{path}_#{SecureRandom.alphanumeric(8)}.temporary"
File.write(temporary_path, fetcher.call)
File.write(temporary_path, klass.generate_translation_map)
raise StandardError, "#{temporary_path} does not exist; Failed to load file" if !File.exist?(temporary_path)
raise StandardError, "#{temporary_path} is too small; Failed to load file" if File.size?(temporary_path) < 15
File.rename(temporary_path, path)
Expand All @@ -65,21 +58,9 @@ def should_fetch?(file)
File.stat(file).mtime < Time.now - (60 * 60 * 24)
end

def hlb_dir
@translation_map_dir
end

def hlb_file
"#{@translation_map_dir}/hlb.json.gz"
end

def lib_loc_info_file
"#{@translation_map_dir}/umich/libLocInfo.yaml"
end

def electronic_collection_file
"#{@translation_map_dir}/umich/electronic_collections.yaml"
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
@umich_dir = File.join(@tmp_dir, "umich")
@hlb_path = File.join(@tmp_dir, "hlb.json.gz")
@lib_loc_info_path = File.join(@umich_dir, "libLocInfo.yaml")
@lib_loc_info_klass = class_double(Jobs::LibLocInfo, generate_translation_map: string_of_size(20), file_path: Jobs::LibLocInfo.file_path)
@electronic_collections_path = File.join(@umich_dir, "electronic_collections.yaml")
@electronic_collections_klass = class_double(Jobs::ElectronicCollections, generate_translation_map: string_of_size(20), file_path: Jobs::ElectronicCollections.file_path)

Dir.mkdir(@tmp_dir) unless File.exist?(@tmp_dir)
Dir.mkdir(@umich_dir) unless File.exist?(@umich_dir)
@params = {
lib_loc_info_klass: class_double(Jobs::LibLocInfo, generate_translation_map: string_of_size(20)),
electronic_collections_klass: class_double(Jobs::ElectronicCollections, generate_translation_map: string_of_size(20)),
high_level_browse_klass: class_double(HighLevelBrowse, fetch_and_save: nil),
translation_map_generators: [@lib_loc_info_klass, @electronic_collections_klass],
translation_map_dir: @tmp_dir

}
end
after(:each) do
Expand Down Expand Up @@ -48,8 +51,8 @@ def string_of_size(size)
`touch #{@hlb_path}`
subject.run
expect(@params[:high_level_browse_klass]).not_to have_received(:fetch_and_save)
expect(@params[:lib_loc_info_klass]).not_to have_received(:generate_translation_map)
expect(@params[:electronic_collections_klass]).not_to have_received(:generate_translation_map)
expect(@lib_loc_info_klass).not_to have_received(:generate_translation_map)
expect(@electronic_collections_klass).not_to have_received(:generate_translation_map)
end
end
context "has old translation map files" do
Expand All @@ -59,17 +62,17 @@ def string_of_size(size)
`touch -d "-2 days" #{@hlb_path}`
subject.run
expect(@params[:high_level_browse_klass]).to have_received(:fetch_and_save)
# expect(@params[:lib_loc_info_klass]).to have_received(:generate_translation_map)
expect(@params[:electronic_collections_klass]).to have_received(:generate_translation_map)
expect(@lib_loc_info_klass).to have_received(:generate_translation_map)
expect(@electronic_collections_klass).to have_received(:generate_translation_map)
end
end
context "fails to generate big enough files" do
it "errors out for too small lib_loc_info" do
allow(@params[:lib_loc_info_klass]).to receive(:generate_translation_map).and_return(string_of_size(2))
allow(@lib_loc_info_klass).to receive(:generate_translation_map).and_return(string_of_size(2))
expect { subject.run }.to raise_error(StandardError)
end
it "errors out for too small electronic_collections_file" do
allow(@params[:electronic_collections_klass]).to receive(:generate_translation_map).and_return(string_of_size(2))
allow(@electronic_collections_klass).to receive(:generate_translation_map).and_return(string_of_size(2))
expect { subject.run }.to raise_error(StandardError)
end
end
Expand Down

0 comments on commit 99fdfb3

Please sign in to comment.