Sha256: 9fab2faa18e2ef05ce8029ef37a083dae9ff4f42eccba8ddf6d698e00c0fda1a

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

require "csv"

module Ddr::Index
  class CSVQueryResult < AbstractQueryResult

    MAX_ROWS         = 10**8 # Just set to a really high number :)
    CSV_MV_SEPARATOR = ";"

    delegate :headers, :to_s, :to_csv, to: :table

    def delete_empty_columns!
      table.by_col!.delete_if { |c, vals| vals.all?(&:nil?) }
    end

    def each(&block)
      table.by_row!.each(&block)
    end

    def [](index_or_header)
      table.by_col_or_row![index_or_header]
    end

    def table
      @table ||= CSV.parse(data, csv_opts)
    end

    def csv_opts
      { headers:        csv_headers,
        converters:     [convert_semicolons, convert_escaped_newlines],
        return_headers: false,
        write_headers:  true,
      }
    end

    def solr_csv_opts
      { "csv.mv.separator" => CSV_MV_SEPARATOR,
        "csv.header"       => solr_csv_header?,
        "rows"             => solr_csv_rows,
        "wt"               => "csv",
      }
    end

    def query_field_headings
      query.fields.map { |f| f.respond_to?(:heading) ? f.heading : f.to_s }
    end

    def csv_headers
      :first_row
    end

    def solr_csv_header?
      query.fields.empty?
    end

    def solr_csv_rows
      query.rows || MAX_ROWS
    end

    def solr_csv_params
      params.merge(solr_csv_opts)
    end

    def data
      if solr_csv_header?
        solr_data
      else
        [ query_field_headings.join(","), solr_data ].join("\n")
      end
    end

    def solr_data
      Connection.get("select", params: solr_csv_params)
    end

    def convert_semicolons
      lambda { |f| f.gsub(/\\#{CSV_MV_SEPARATOR}/, CSV_MV_SEPARATOR) rescue f }
    end

    def convert_escaped_newlines
      lambda { |f| f.gsub(/\\r/, "\r").gsub(/\\n/, "\n") rescue f }
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ddr-core-1.1.2 lib/ddr/index/csv_query_result.rb
ddr-core-1.1.1 lib/ddr/index/csv_query_result.rb
ddr-core-1.1.0 lib/ddr/index/csv_query_result.rb
ddr-core-1.0.0 lib/ddr/index/csv_query_result.rb
ddr-core-0.3.0 lib/ddr/index/csv_query_result.rb
ddr-core-0.2.2 lib/ddr/index/csv_query_result.rb
ddr-core-0.2.1 lib/ddr/index/csv_query_result.rb