Sha256: 817f72960fbfb6e778cc0ab07b8abef42e4379842efced4b64bd8e329dc9e6d5

Contents?: true

Size: 1.42 KB

Versions: 7

Compression:

Stored size: 1.42 KB

Contents

require 'helper'

class NaturalGasParser
  def initialize(options = {})
    # nothing
  end
  def apply(row)
    virtual_rows = []
    row.keys.grep(/\A(.*) Natural Gas/) do |location_column_name|
      match_1 = $1
      next if (price = row[location_column_name]).blank? or (date = row['Date']).blank?
      if match_1 == 'U.S.'
        locatable_id = 'US'
        locatable_type = 'Country'
      else
        locatable_id = match_1 # name
        locatable_type = 'State'
      end
      date = Time.parse(date)
      new_row = ActiveSupport::OrderedHash.new
      new_row['locatable_id'] = locatable_id
      new_row['locatable_type'] = locatable_type
      new_row['price'] = price
      new_row['year'] = date.year
      new_row['month'] = date.month
      row_hash = RemoteTable::Transform.row_hash new_row
      new_row['row_hash'] = row_hash
      virtual_rows << new_row
    end
    virtual_rows
  end
end

describe RemoteTable do
  describe "when using old-style parser" do
    it "open an XLS with a parser" do
      t = RemoteTable.new(:url => 'http://www.eia.gov/dnav/ng/xls/ng_pri_sum_a_EPG0_FWA_DMcf_a.xls',
             :sheet => 'Data 1',
             :skip => 2,
             :select => proc { |row| row['year'].to_i > 1989 },
             :transform => { :class => NaturalGasParser })
      t[0]['locatable_type'].must_equal 'Country'
      t[0]['locatable_id'].must_equal 'US'
      t[0].row_hash.must_be :present?
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
remote_table-2.1.2 test/test_old_transform.rb
remote_table-2.1.1 test/test_old_transform.rb
remote_table-2.1.0 test/test_old_transform.rb
remote_table-2.0.2 test/test_old_transform.rb
remote_table-2.0.1 test/test_old_transform.rb
remote_table-2.0.0 test/test_old_transform.rb
remote_table-1.4.0 test/test_old_transform.rb