Module: RfcReader::Library
- Defined in:
- lib/rfc_reader/library.rb
Class Method Summary collapse
-
.add_to_catalog(title:, url:, path:) ⇒ Object
Adds the RFC to the beginning of the catalog and removes any existing entries.
-
.catalog ⇒ Array<Hash<String, String>>
These are referenced later on by the ‘rfc-reader library` command.
-
.download_document(title:, url:) ⇒ String
The RFC content.
- .library_cache_dir ⇒ String
- .library_cache_list_path ⇒ String
-
.load_document(title:, url:, path:) ⇒ String
The RFC content.
- .program_cache_dir ⇒ String
- .xdg_cache_home ⇒ String
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.
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 |
.catalog ⇒ Array<Hash<String, String>>
These are referenced later on by the ‘rfc-reader library` command.
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.
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_dir ⇒ 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_path ⇒ 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.
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_dir ⇒ 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_home ⇒ 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.(path) } end |