错误:ruby中CSV的字段映射

时间:2016-07-29 09:31:30

标签: ruby csv ftp ftp-client

我有一个通过从FTP服务器调用属性来下载CSV文件的功能,我使用字段映射来相应地映射属性。

如何解决此错误

ContentReader

以下是我得到的错误

def download_csv(page_id = fetch_grid_ids.first, mapping: mapping, csv_options: csv_options)
      results = fetch_grid(page_id)
      records = results['response']['data']
      output = []
      record_count = records.size

      if mapping
        headers = mapping.values
        download_headers = mapping.keys
      elsif @config[:output_file_headers]
        headers = @config[:output_file_headers]
        download_headers = headers
      else
        headers = data.first.keys
        headers.shift
        download_headers = headers
      end

      download_headers = download_headers.map(&:to_s)

      output << CSV.generate_line(headers, csv_options)

      records.each_with_index do |r, i|
        row = []

        download_headers.each do |h|
          row << r[h] rescue nil
        end

        logger.info "PriceFx::Client.download: #{i + 1}/#{record_count} :: #{row.size}"
        output << CSV.generate_line(row, csv_options)
      end

      filename = "#{Rails.configuration.tmp_dir}/#{@config[:output_file]}_#{page_id}.csv"
      File.open(filename, 'wb') {|f| f.puts output }

      return filename
    end

任何线索如何解决此问题

1 个答案:

答案 0 :(得分:0)

在方法定义中,double-splatted params可能具有默认值,并且您尝试使用不存在的局部变量提供默认值:

#                                        ⇓⇓⇓⇓⇓⇓⇓ ???
download_csv(page_id = default, mapping: mapping, ...)

有人可能会将nil放在那里,或任何适用的和已定义的默认值,例如:

DEFAULT_MAPPING = ...
download_csv(page_id = default, mapping: DEFAULT_MAPPING, ...)