class SimpleNoteParser::Processor

Attributes

destination[RW]
file[RW]
headers[RW]

Public Class Methods

new(file: "./source/notes.json", destination: "./dist", headers: %w[title content tags]) click to toggle source
# File lib/simple_note_parser/processor.rb, line 8
def initialize(file: "./source/notes.json", destination: "./dist", headers: %w[title content tags])
  @file = file
  @destination = destination
  @headers = headers
end

Public Instance Methods

save_as_csv() click to toggle source
# File lib/simple_note_parser/processor.rb, line 14
def save_as_csv
  json_data = load_json_data(file)
  notes = parse_json_data(json_data)
  create_directory(destination)
  create_csv
  process_notes(notes)
end

Private Instance Methods

add_row_to_csv(note, csv) click to toggle source
# File lib/simple_note_parser/processor.rb, line 37
def add_row_to_csv(note, csv)
  title = note["content"].split("\r").first
  content = note["content"]
  tags = note["tags"]
  formatted_tags = tags.join(", ") unless tags.nil?
  csv << [title, content, formatted_tags]
end
create_csv() click to toggle source
# File lib/simple_note_parser/processor.rb, line 45
def create_csv
  CSV.new(csv_path, headers: true)
end
csv_path() click to toggle source
# File lib/simple_note_parser/processor.rb, line 24
def csv_path
  destination + "/notes.csv"
end
process_notes(notes) click to toggle source
# File lib/simple_note_parser/processor.rb, line 28
def process_notes(notes)
  CSV.open(csv_path, "wb", headers: true) do |csv|
    csv << headers
    notes.each do |note|
      add_row_to_csv(note, csv)
    end
  end
end