Class: HashcardsReadwise::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/hashcards_readwise/converter.rb

Constant Summary collapse

DEFAULT_AUTHOR =
"Derek Stride"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: Logger.new($stderr, level: Logger::WARN)) ⇒ Converter

Returns a new instance of Converter.



14
15
16
17
# File 'lib/hashcards_readwise/converter.rb', line 14

def initialize(logger: Logger.new($stderr, level: Logger::WARN))
  @logger = logger
  @frontmatter_cache = {}
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/hashcards_readwise/converter.rb', line 12

def logger
  @logger
end

Instance Method Details

#convert(hashcards_path:, db_path: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hashcards_readwise/converter.rb', line 19

def convert(hashcards_path:, db_path: nil)
  hashcards = JSON.parse(File.read(hashcards_path))
  logger.info("Loaded #{hashcards["cards"].size} cards from #{hashcards_path}")

  timestamps = load_timestamps(db_path)
  logger.info("Loaded #{timestamps.size} timestamps from database") if timestamps.any?

  highlights = hashcards["cards"].filter_map { |card| card_to_highlight(card, timestamps) }
  logger.info("Converted #{highlights.size} highlights")
  logger.info("Parsed frontmatter from #{@frontmatter_cache.size} files")

  { "highlights" => highlights }
end