2016-07-29 82 views
0

我有一個函數可以通過從FTP服務器調用屬性來下載CSV文件,我使用了字段映射來相應地映射屬性。錯誤:ruby中CSV的字段映射

怎麼可能解決此錯誤

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 

下面是我得到

client.rb:224: syntax error, unexpected tLABEL 
... fetch_grid_ids.first, mapping: mapping, csv_options: csv_op... 
...        ^
client.rb:452: syntax error, unexpected keyword_end, expecting $end 

我怎樣才能解決這個

回答

0

在方法定義的任何線索錯誤,雙splatted參數可能具有默認值,並且您嘗試使用不存在的局部變量提供默認值:

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

有人可能會提出nil那裏,或任何適用和定義默認值,例如:

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