Class: WordTree::Disk::Librarian
- Inherits:
-
Object
- Object
- WordTree::Disk::Librarian
- Includes:
- Enumerable
- Defined in:
- lib/wordtree/disk/librarian.rb
Constant Summary collapse
- MissingContent =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#library ⇒ Object
readonly
Returns the value of attribute library.
Instance Method Summary collapse
- #archive_org_get(*book_ids, &block) ⇒ Object
- #archive_org_get_range_of_years(start_year, end_year, &block) ⇒ Object
-
#archive_org_get_with_conditions(conditions, &block) ⇒ Object
Downloads a set of books to the on-disk library and returns a list of book_ids.
- #each(file_suffix_re = /\.(md|txt)$/, &block) ⇒ Object
- #find(book_id) ⇒ Object
-
#initialize(library) ⇒ Librarian
constructor
WordTree::Disk::Library object.
- #save(book) ⇒ Object
Constructor Details
Instance Attribute Details
#library ⇒ Object (readonly)
Returns the value of attribute library.
11 12 13 |
# File 'lib/wordtree/disk/librarian.rb', line 11 def library @library end |
Instance Method Details
#archive_org_get(*book_ids, &block) ⇒ Object
47 48 49 50 51 |
# File 'lib/wordtree/disk/librarian.rb', line 47 def archive_org_get(*book_ids, &block) book_ids.map do |book_id| archive_org_get_with_conditions(identifier: book_id, &block) end.flatten(1) end |
#archive_org_get_range_of_years(start_year, end_year, &block) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/wordtree/disk/librarian.rb', line 53 def archive_org_get_range_of_years(start_year, end_year, &block) archive_org_get_with_conditions({ :start_year => start_year, :end_year => end_year }, &block) end |
#archive_org_get_with_conditions(conditions, &block) ⇒ Object
Downloads a set of books to the on-disk library and returns a list of book_ids
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/wordtree/disk/librarian.rb', line 62 def archive_org_get_with_conditions(conditions, &block) archdown = Archdown.new [].tap do |archive_org_ids| archdown.download_all(conditions) do |, content, failure| if failure #TODO: logging $stderr.puts "Unable to download from archive.org: #{failure}" raise failure else book = Book.create(["archive_org_id"], , content) save(book) yield book, self if block_given? archive_org_ids << book.id end end end end |
#each(file_suffix_re = /\.(md|txt)$/, &block) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/wordtree/disk/librarian.rb', line 34 def each(file_suffix_re=/\.(md|txt)$/, &block) library.each_with_id(file_suffix_re) do |path, id| retrieved = Preamble.load(path, :external_encoding => "utf-8") yield Book.new(retrieved..merge("content" => retrieved.content)) end end |
#find(book_id) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/wordtree/disk/librarian.rb', line 25 def find(book_id) begin retrieved = Preamble.load(library.path_to(book_id), :external_encoding => "utf-8") Book.create(book_id, retrieved., retrieved.content) rescue Errno::ENOENT nil end end |
#save(book) ⇒ Object
41 42 43 44 45 |
# File 'lib/wordtree/disk/librarian.rb', line 41 def save(book) library.mkdir(book.id) raise MissingContent, "book #{book.id} is missing content" unless book.content Preamble.new(book., book.content).save(library.path_to(book.id)) end |