class PropaneParser

Public Class Methods

new(options = {}) click to toggle source
# File lib/earth/residence/residence_fuel_price/data_miner.rb, line 40
def initialize(options = {})
  # nothing
end

Public Instance Methods

apply(row) click to toggle source
# File lib/earth/residence/residence_fuel_price/data_miner.rb, line 43
def apply(row)
  virtual_rows = []
  row.keys.grep(/(.*) Propane Residential Price/) do |location_column_name|
    match_1 = $1
    next if (price = row[location_column_name]).blank? or (date = row['Date']).blank?
    if match_1.starts_with?('U.S.')
      locatable_id = 'US'
      locatable_type = 'Country'
    else
      /\(PADD (.*)\)/.match match_1
      match_2 = $1
      next if match_2 == '1' # skip PADD 1 because we always prefer subdistricts
      locatable_id = match_2
      locatable_type = 'PetroleumAdministrationForDefenseDistrict'
    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.to_f.cents.to(:dollars)
    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