class Alexandria::LibraryThingCSVImport

Public Class Methods

new(header) click to toggle source
Calls superclass method Alexandria::CSVImport::new
# File lib/alexandria/import_library_csv.rb, line 121
def initialize(header)
  super(header)
  @title = index_of("'TITLE'")
  @author = index_of("'AUTHOR (first, last)'")
  @isbn = index_of("'ISBN'")
  @publisher_info = index_of("'PUBLICATION INFO'")
  @publishing_year = index_of("'DATE'")

  # optional extras
  @notes = index_of("'COMMENTS'")
  @rating = index_of("'RATING'")
  @tags = index_of("'TAGS'")
end

Public Instance Methods

row_to_book(row) click to toggle source
# File lib/alexandria/import_library_csv.rb, line 135
def row_to_book(row)
  title = normalize(row[@title])
  authors = []
  authors << normalize(row[@author])
  isbn = row[@isbn]
  if isbn
    isbn = Regexp.last_match[1] if isbn =~ /\[([^\]]+)\]/
    isbn = Library.canonicalise_ean(isbn)
  end

  # usually "Publisher (YEAR), Binding, NUM pages"
  # sometimes "Publisher (YEAR), Edition: NUM, Binding, NUM pages"
  publisher_info = normalize(row[@publisher_info])
  publisher = publisher_info
  publisher = Regexp.last_match[1] if publisher_info =~ /([^(]+)\(/
  edition = publisher_info # binding
  edition_info = publisher_info.split(",")
  edition = publisher_info.split(",")[-2] if edition_info.size >= 3

  year = row[@publishing_year].to_i

  book = Alexandria::Book.new(title,
                              authors,
                              isbn,
                              publisher,
                              year,
                              edition)
  book.notes = normalize(row[@notes]) if row[@notes]

  book.rating = row[@rating].to_i if row[@rating]
  if row[@tags]
    tags = normalize(row[@tags]).split(",")
    tags.each do |tag|
      book.tags << tag unless book.tags.include? tag
    end
  end

  log.debug { "LibraryThing loading #{book.title}" }
  book
end