Sha256: ee3409a662600b9848a6df24e4d929c7445e67c256db92fe6afdef2558989263

Contents?: true

Size: 1.08 KB

Versions: 18

Compression:

Stored size: 1.08 KB

Contents

require 'delegate'
require 'csv'
require 'digest/md5'

module Peddler
  # @api private
  class FlatFileParser < SimpleDelegator
    # http://stackoverflow.com/questions/8073920/importing-csv-quoting-error-is-driving-me-nuts
    OPTIONS = { col_sep: "\t", quote_char: "\x00", headers: true }

    attr_reader :content, :summary, :encoding

    def initialize(res, encoding)
      super(res)
      @encoding = encoding
      extract_content
    end

    def parse
      CSV.parse(scrub_content, OPTIONS) if content
    end

    def records_count
      summarize if summary?
    end

    def valid?
      headers['Content-MD5'] == Digest::MD5.base64digest(body)
    end

    private

    def extract_content
      if summary?
        @summary, @content = body.split("\n\n")
      else
        @content = body.dup
      end
    end

    def scrub_content
      content.force_encoding(encoding).encode('UTF-8')
    end

    def summary?
      body.start_with?('Feed Processing Summary')
    end

    def summarize
      Hash[summary.split("\n\t")[1, 2].map { |line| line.split("\t\t") }]
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
peddler-0.17.1 lib/peddler/flat_file_parser.rb
peddler-0.17.0 lib/peddler/flat_file_parser.rb
peddler-0.16.0 lib/peddler/flat_file_parser.rb
peddler-0.15.0 lib/peddler/flat_file_parser.rb
peddler-0.14.0 lib/peddler/flat_file_parser.rb
peddler-0.13.0 lib/peddler/flat_file_parser.rb
peddler-0.12.7 lib/peddler/flat_file_parser.rb
peddler-0.12.5 lib/peddler/flat_file_parser.rb
peddler-0.12.4 lib/peddler/flat_file_parser.rb
peddler-0.12.2 lib/peddler/flat_file_parser.rb
peddler-0.12.1 lib/peddler/flat_file_parser.rb
peddler-0.12.0 lib/peddler/flat_file_parser.rb
peddler-0.11.1 lib/peddler/flat_file_parser.rb
peddler-0.11.0 lib/peddler/flat_file_parser.rb
peddler-0.10.0 lib/peddler/flat_file_parser.rb
peddler-0.9.2 lib/peddler/flat_file_parser.rb
peddler-0.9.1 lib/peddler/flat_file_parser.rb
peddler-0.9.0 lib/peddler/flat_file_parser.rb