class ClippingsPluck::CsvParser
Constants
- AMZN_DIVIDER
- AMZN_SEP
- EXCEL_DIVIDER
- EXCEL_SEP
Public Class Methods
new()
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 11 def initialize @clippings = Clippings.new end
Public Instance Methods
run(string)
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 15 def run(string) @string = string @raw_metadata, @clipping_data = @string.split(divider) @book, @authors = parse_metadata build_clippings @clippings end
Private Instance Methods
attach_note(data)
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 62 def attach_note(data) if @clippings.last[:note].empty? @clippings.last[:note] = data['Annotation'] else @clippings.last[:note] += " | #{data['Annotation']}" end end
build_clippings()
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 43 def build_clippings remove_ghost_rows if !excel? csv_hash = CSV.parse(@clipping_data, headers: true, col_sep: sep).map(&:to_h) csv_hash.each { |data| format_according_to_type(data) } end
divider()
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 29 def divider excel? ? EXCEL_DIVIDER : AMZN_DIVIDER end
excel?()
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 25 def excel? @string.include? EXCEL_DIVIDER end
format_according_to_type(data)
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 53 def format_according_to_type(data) if data['Annotation Type'] == 'Note' attach_note(data) else @clipping = Clipping.new @clippings << format_clipping(data) end end
format_clipping(data)
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 70 def format_clipping(data) @clipping[:quote] = data.delete 'Annotation' @clipping.location = (data.delete 'Location').gsub(/Location /, '') @clipping[:note] = "" @clipping[:book_title] = @book @clipping[:author] = @authors data.delete 'Annotation Type' data.delete 'Starred?' @clipping end
parse_metadata()
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 37 def parse_metadata metadata = @raw_metadata.split("\r")[1..2].map(&:strip).map{ |line| line.gsub(/by /, "") } metadata.map! { |string| string.gsub("\"", "").gsub(",,,", "") } if !excel? metadata end
remove_ghost_rows()
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 49 def remove_ghost_rows @clipping_data = @clipping_data.gsub(",,,\r\n,,,\r\n", "") end
sep()
click to toggle source
# File lib/clippings_pluck/csv_parser.rb, line 33 def sep excel? ? EXCEL_SEP : AMZN_SEP end