lib/relaton/index/file_io.rb in relaton-index-0.1.3 vs lib/relaton/index/file_io.rb in relaton-index-0.1.4
- old
+ new
@@ -4,34 +4,37 @@
# File IO class is used to read and write index files.
# In searh mode url is used to fetch index from external repository and save it to storage.
# In index mode url should be nil.
#
class FileIO
+ attr_reader :url
+
#
# Initialize FileIO
#
# @param [String] dir local directory in ~/.relaton to store index
# @param [String, nil] url git repository URL to fetch index from
# (if not exists, or older than 24 hours) or nil if index is used to
# index files
#
- def initialize(dir, url)
+ def initialize(dir, url, filename)
@dir = dir
@url = url
+ @filename = filename
end
#
# Read index from storage or fetch from external repository
#
# @return [Array<Hash>] index
#
def read
- if @url
- @file ||= File.join(Index.config.storage_dir, ".relaton", @dir, Index.config.filename)
+ if url
+ @file ||= File.join(Index.config.storage_dir, ".relaton", @dir, @filename)
check_file || fetch_and_save
else
- @file ||= Index.config.filename
+ @file ||= @filename
read_file
end
end
#
@@ -62,10 +65,10 @@
# Fetch index from external repository and save it to storage
#
# @return [Array<Hash>] index
#
def fetch_and_save
- resp = URI(@url).open
+ resp = URI(url).open
zip = Zip::InputStream.new resp
entry = zip.get_next_entry
index = YAML.safe_load(entry.get_input_stream.read, permitted_classes: [Symbol])
save index
index