Module: RfcReader::Library

Defined in:
lib/rfc_reader/library.rb

Class Method Summary collapse

Class Method Details

.add_to_catalog(title:, url:, path:) ⇒ Object

Adds the RFC to the beginning of the catalog and removes any existing entries. These are referenced later on by the ‘rfc-reader library` command.

Parameters:

  • title (String)

    the RFC title

  • url (String)

    the text file URL for the RFC

  • path (String)

    the path to the local copy of the RFC



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rfc_reader/library.rb', line 65

def self.add_to_catalog(title:, url:, path:)
  list = catalog.reject do |rfc|
    title == rfc[:title] ||
      url == rfc[:url] ||
      path == rfc[:path]
  end

  rfc = {
    title: title,
    url: url,
    path: path,
  }

  list = [rfc, *list]
  while list.size > MAX_DOCUMENT_COUNT
    path = list.pop[:path]
    FileUtils.rm_f(path) if path.start_with?(library_cache_dir)
  end

  json = JSON.pretty_generate(list)
  FileUtils.mkdir_p(program_cache_dir)
  File.write(library_cache_list_path, json)
end

.catalogArray<Hash<String, String>>

These are referenced later on by the ‘rfc-reader library` command.

Examples:

[
  { title: "My RFC", url: "www.my-rfc.com/my-rfc.txt", path: ".cache/rfc-reader/library/my-rfc.txt" },
  ...
]

Returns:

  • (Array<Hash<String, String>>)

    a list of RFC info hashes



50
51
52
53
54
55
56
57
# File 'lib/rfc_reader/library.rb', line 50

def self.catalog
  if File.exist?(library_cache_list_path)
    content = File.read(library_cache_list_path)
    JSON.parse(content, symbolize_names: true)
  else
    []
  end
end

.download_document(title:, url:) ⇒ String

Returns the RFC content.

Parameters:

  • title (String)

    the RFC title

  • url (String)

    the text file URL for the RFC

Returns:

  • (String)

    the RFC content



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rfc_reader/library.rb', line 15

def self.download_document(title:, url:)
  file_name = File.basename(url)
  file_path = File.join(library_cache_dir, file_name)

  content = ErrorContext.wrap("Downloading RFC document") do
    Net::HTTP.get(URI(url))
  end

  FileUtils.mkdir_p(library_cache_dir)
  File.write(file_path, content)
  add_to_catalog(title: title, url: url, path: file_path)

  content
end

.library_cache_dirString

Returns:

  • (String)


109
110
111
# File 'lib/rfc_reader/library.rb', line 109

def self.library_cache_dir
  File.join(program_cache_dir, "library")
end

.library_cache_list_pathString

Returns:

  • (String)


104
105
106
# File 'lib/rfc_reader/library.rb', line 104

def self.library_cache_list_path
  File.join(program_cache_dir, "library_list.json")
end

.load_document(title:, url:, path:) ⇒ String

Returns the RFC content.

Parameters:

  • title (String)

    the RFC title

  • url (String)

    the text file URL for the RFC

  • path (String)

    the path to the local copy of the RFC

Returns:

  • (String)

    the RFC content



34
35
36
37
38
39
40
# File 'lib/rfc_reader/library.rb', line 34

def self.load_document(title:, url:, path:)
  content = File.read(path)

  add_to_catalog(title: title, url: url, path: path)

  content
end

.program_cache_dirString

Returns:

  • (String)


99
100
101
# File 'lib/rfc_reader/library.rb', line 99

def self.program_cache_dir
  File.join(xdg_cache_home, "rfc_reader")
end

.xdg_cache_homeString

Returns:

  • (String)


92
93
94
95
96
# File 'lib/rfc_reader/library.rb', line 92

def self.xdg_cache_home
  ENV
    .fetch("XDG_CACHE_HOME") { File.join(Dir.home, ".cache") }
    .then { |path| File.expand_path(path) }
end