Class: LittleFinger::Parsers::TaxSalesFile

Inherits:
Object
  • Object
show all
Defined in:
lib/little_finger/parsers/tax_sales_file.rb

Constant Summary collapse

STATE_IDX =
0
STATE_NAME_IDX =
1
CITY_IDX =
2
COMBINED_RATE_IDX =
86
EFFECTIVE_DATE_IDX =
87
ZIP_CODE_IDX =
89

Instance Method Summary collapse

Instance Method Details

#parse(tax_file) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/little_finger/parsers/tax_sales_file.rb', line 10

def parse(tax_file)
  tax_data = {}
  File.open(tax_file,"r").each_line do |line|
    data = line.split(",")
    zip_code = data[ZIP_CODE_IDX]
    effective_date = data[EFFECTIVE_DATE_IDX]
    rate = data[COMBINED_RATE_IDX]
    state = data[STATE_IDX]
    city = data[CITY_IDX]
    description = "#{city}, #{state}"

    entry = [zip_code,effective_date, rate, description, state, city]
    raise StandardError.new("Avatax file format is incorrect: #{entry}") if entry.any?{|elem| elem.nil?}
    key = (zip_code + city).strip
    effective_date = "2015/01/01" if effective_date == "0000/00/00"

    tax_data[key] = { zip: zip_code, city: city, state: state, tax_rate: BigDecimal.new(rate), effective_at: DateTime.strptime(effective_date, "%Y/%m/%d") }
  end
  tax_data
end