Class: Archdown::Librarian

Inherits:
Object
  • Object
show all
Defined in:
lib/archdown/librarian.rb

Overview

The Librarian takes a book and puts it in the library

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library, book) ⇒ Librarian

Returns a new instance of Librarian.



11
12
13
14
15
# File 'lib/archdown/librarian.rb', line 11

def initialize(library, book)
  @library = library
  @book = book
  @failure = nil
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



9
10
11
# File 'lib/archdown/librarian.rb', line 9

def book
  @book
end

#libraryObject (readonly)

Returns the value of attribute library.



9
10
11
# File 'lib/archdown/librarian.rb', line 9

def library
  @library
end

Instance Method Details

#book_dirObject



30
31
32
# File 'lib/archdown/librarian.rb', line 30

def book_dir
  @library.path_for_identifier(@book.identifier)
end

#book_filepathObject



34
35
36
# File 'lib/archdown/librarian.rb', line 34

def book_filepath
  File.join(book_dir, @book.identifier + '.md')
end

#make_book_dirObject



38
39
40
# File 'lib/archdown/librarian.rb', line 38

def make_book_dir
  FileUtils.mkdir_p(book_dir)
end

#metadataObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/archdown/librarian.rb', line 17

def 
  {
    'title'  => @book.title,
    'author' => @book.creators ? @book.creators.join(';') : nil,
    'year'   => @book.date.year,
    'source' => "http://archive.org/details/#{@book.identifier}",
    'status' => "OCR ONLY",
    'archive_org_id' => @book.identifier,
  }.tap do |meta|
    meta['failure'] = @failure if @failure
  end
end

#store_book {|metadata, _self| ... } ⇒ Object

Yields:

Yield Parameters:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/archdown/librarian.rb', line 42

def store_book(&block)
  make_book_dir

  begin
    text = @book.download
  rescue Archivist::Model::Document::UnsupportedFormat => e
    @failure = e.to_s
  rescue StandardError => e
    @failure = e.to_s
  end

  yield , self if block_given?

  content = .to_yaml
  content += "---\n"
  content += text if text

  File.open(book_filepath, "w") do |file|
    file.write content
  end
end