class OlDumpParser::Parser

Parser class that will parses ol dump

Attributes

inp_file[RW]

@return [String] Path to the input file

out_file[RW]

@return [String] Path to the output file

verbose[RW]

@return [Boolean] Verbose mode

Public Class Methods

new(params) click to toggle source

Constructor for Parser @param [Hash] params Inputs to parser @option params [String] :inp_file Path to the input file @option params [String] :out_file Path to the output file

# File lib/ol_dump_parser/parser.rb, line 20
def initialize(params)
  self.inp_file = params[:inp_file]
  self.out_file = params[:out_file]
  self.verbose  = params[:verbose] || false 
end

Public Instance Methods

parse() click to toggle source

Parses the input csv file and converts to a json file

# File lib/ol_dump_parser/parser.rb, line 27
def parse
  output = File.open out_file, 'w'
  File.open(inp_file).each do |line|
    # CSV parse returns an array of rows - we need only the first
    parsed_line = CSV.parse(line.chomp, col_sep: "\t", liberal_parsing: true).first
    value_map = csv_row_to_map(parsed_line)
    puts value_map[:details]['name'] if verbose
    output.puts value_map.to_json
  end
  output.close
end

Private Instance Methods

csv_row_to_map(row_values) click to toggle source

Convert the given row values to a hashmap @param row_values [Array] Csv row values @return [Hash] hash map

# File lib/ol_dump_parser/parser.rb, line 44
def csv_row_to_map(row_values)
  {
    type: row_values[0],
    key: row_values[1],
    revision: row_values[2],
    last_modified: row_values[3],
    details: JSON.parse(row_values[4])
  }
end