Module: GetUrl::FileManager

Extended by:
FileManager
Included in:
FileManager
Defined in:
lib/geturl/geturl-file-manager.rb

Overview

The FileManager deals with the local (cached) files.

Instance Method Summary collapse

Instance Method Details

#all_items_cache_exist?TrueClass|FalseClass

Returns true if the all-items-cache file exists.

Returns:

  • (TrueClass|FalseClass)


81
82
83
# File 'lib/geturl/geturl-file-manager.rb', line 81

def all_items_cache_exist?
  return File.exists?(get_all_items_cache_file)
end

#cache_all_items(items) ⇒ Object

Cache the given items to the all-items-cache.

Parameters:

  • items (Object)


88
89
90
# 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

#clean_and_save_items_to_yaml_file(str, file) ⇒ Integer

Loads the clean items in the given Yaml string and stores it in the given file

Parameters:

  • str (Object)

    The Yaml string

  • file (Object)

    The path of the file

Returns:

  • (Integer)


57
58
59
60
61
# 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

#clear_all_items_cacheObject

Clears the all-items-cache.

Returns:

  • (Object)


102
103
104
# 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

#get_all_items_cache_fileString

Returns the path of the all-items-cache file.

Returns:

  • (String)

    The path of the all-items-cache file.



74
75
76
# 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

#get_all_items_from_cacheObject

Returns all items from the all-items-cache.

Returns:

  • (Object)

    The items



95
96
97
# 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

#get_local_source_file(id) ⇒ String

Returns the local file name for the given source id.

Parameters:

  • id (Object)

    The source id.

Returns:

  • (String)

    The path of the local source file.



67
68
69
# File 'lib/geturl/geturl-file-manager.rb', line 67

def get_local_source_file(id)
  return File.join(@sources_path, "#{id}.yaml")
end

#load_items_from_yaml_file(file) ⇒ Array

Loads the items from the given local file.

Parameters:

  • file (String)

    The path of the file

Returns:

  • (Array)

    The items



17
18
19
20
# 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

#load_items_from_yaml_string(str) ⇒ Array

Loads the items from the given string.

Parameters:

  • str (String)

    The string

Returns:

  • (Array)


27
28
29
30
31
32
33
34
35
36
37
38
39
# 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_items_to_yaml_file(items, file) ⇒ Integer

Save the given items to the given file in Yaml.

Parameters:

  • items (Array)

    An array of items

  • file (String)

    The path of the file

Returns:

  • (Integer)


46
47
48
49
50
# 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