lib/relaton/index/file_io.rb in relaton-index-0.2.15 vs lib/relaton/index/file_io.rb in relaton-index-0.2.16

- old
+ new

@@ -104,16 +104,11 @@ # def read_file yaml = Index.config.storage.read(file) return unless yaml - index = YAML.safe_load yaml, permitted_classes: [Symbol] - return deserialize_pubid(index) if check_format index - - warn_local_index_error "Wrong structure of the" - rescue Psych::SyntaxError - warn_local_index_error "YAML parsing error when reading" + load_index(yaml) || [] end def deserialize_pubid(index) return index unless @pubid_class @@ -132,27 +127,38 @@ def progname @progname ||= "relaton-#{@dir}" end + def load_index(yaml, save = false) + index = YAML.safe_load(yaml, permitted_classes: [Symbol]) + save index if save + return deserialize_pubid(index) if check_format index + + if save + warn_remote_index_error "Wrong structure of" + else + warn_local_index_error "Wrong structure of" + end + rescue Psych::SyntaxError + if save + warn_remote_index_error "YAML parsing error when reading" + else + warn_local_index_error "YAML parsing error when reading" + end + end # # Fetch index from external repository and save it to storage # # @return [Array<Hash>] index # def fetch_and_save resp = StringIO.new(URI(url).read) zip = Zip::InputStream.new resp entry = zip.get_next_entry yaml = entry.get_input_stream.read - index = YAML.safe_load(yaml, permitted_classes: [Symbol]) - save index Util.info "Downloaded index from `#{url}`", progname - return index if check_format index - - warn_remote_index_error "Wrong structure of" - rescue Psych::SyntaxError - warn_remote_index_error "YAML parsing error when reading" + load_index(yaml, save = true) end def warn_remote_index_error(reason) Util.info "#{reason} newly downloaded file `#{file}` at `#{url}`, " \ "the remote index seems to be invalid. Please report this " \