module GetUrl::FileManager
The FileManager
deals with the local (cached) files.
Public Instance Methods
Returns true if the all-items-cache file exists.
@return [TrueClass|FalseClass]
# File lib/geturl/geturl-file-manager.rb, line 81 def all_items_cache_exist? return File.exists?(get_all_items_cache_file) end
Cache the given items to the all-items-cache.
@param [Object] items
# File lib/geturl/geturl-file-manager.rb, line 88 def cache_all_items(items) save_items_to_yaml_file(items, get_all_items_cache_file) end
Loads the clean items in the given Yaml string and stores it in the given file
@param [Object] str The Yaml string @param [Object] file The path of the file @return [Integer]
# File lib/geturl/geturl-file-manager.rb, line 57 def clean_and_save_items_to_yaml_file(str, file) clear_all_items_cache cleaned_items = load_items_from_yaml_string(str) save_items_to_yaml_file(cleaned_items, file) end
Clears the all-items-cache.
@return [Object]
# File lib/geturl/geturl-file-manager.rb, line 102 def clear_all_items_cache File::unlink(get_all_items_cache_file) if all_items_cache_exist? end
Returns the path of the all-items-cache file.
@return [String] The path of the all-items-cache file.
# File lib/geturl/geturl-file-manager.rb, line 74 def get_all_items_cache_file File.join(Dir.home, '.geturl', 'all-items-cache.yaml') end
Returns all items from the all-items-cache.
@return [Object] The items
# File lib/geturl/geturl-file-manager.rb, line 95 def get_all_items_from_cache return load_items_from_yaml_file(get_all_items_cache_file) end
Returns the local file name for the given source id.
@param [Object] id The source id. @return [String] The path of the local source file.
# File lib/geturl/geturl-file-manager.rb, line 67 def get_local_source_file(id) return File.join(@sources_path, "#{id}.yaml") end
Loads the items from the given local file.
@param [String] file The path of the file @return [Array] The items
# File lib/geturl/geturl-file-manager.rb, line 17 def load_items_from_yaml_file(file) str = File.read(file) rescue '' return load_items_from_yaml_string(str) end
Loads the items from the given string.
@param [String] str The string @return [Array]
# File lib/geturl/geturl-file-manager.rb, line 27 def load_items_from_yaml_string(str) items = [] yaml = YAML.load(str).to_a || [] rescue [] yaml.each {|raw| items << { 'url' => raw['url'], 'name' => raw['name'], 'description' => raw['description'], 'tags' => raw['tags'].to_a } if (raw.include?('url')) && (raw.include?('name')) } return items end
Save the given items to the given file in Yaml.
@param [Array] items An array of items @param [String] file The path of the file @return [Integer]
# File lib/geturl/geturl-file-manager.rb, line 46 def save_items_to_yaml_file(items, file) str = "# This file is generated by geturl.\n" str += items.to_yaml File.write(file, str) end