Class: GlobalCollect::LogParsing::FinancialStatement::ReportFile

Inherits:
Object
  • Object
show all
Defined in:
lib/global_collect/log_parsing/financial_statement/report_file.rb

Constant Summary collapse

FILENAME_FORMAT =

RG - Ch10 => Technical aspects => File name 4 chars numeric account id, 1 char numeric year, 3 char numeric day-of-year, extension is .asc

However, in practice it looks something like: FS55550127COMPANY.asc

/^FS(\d{4})(\d{1})(\d{3})[A-Z]+\.asc/
YEAR_BASE_ADJUSTED =
( Date.today.year * 10 ) / 10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ReportFile

Returns a new instance of ReportFile.



20
21
22
23
24
25
26
27
28
29
# File 'lib/global_collect/log_parsing/financial_statement/report_file.rb', line 20

def initialize(path)
  @path = path
  if File.basename(path) =~ FILENAME_FORMAT
    @account_id = $1
    @date = Date.ordinal(YEAR_BASE_ADJUSTED + $2.to_i, $3.to_i)
    @environment = :production
  else
    raise ArgumentError.new("Unparseable path '#{path}'!")
  end
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



3
4
5
# File 'lib/global_collect/log_parsing/financial_statement/report_file.rb', line 3

def 
  @account_id
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/global_collect/log_parsing/financial_statement/report_file.rb', line 3

def data
  @data
end

#dateObject (readonly)

Returns the value of attribute date.



3
4
5
# File 'lib/global_collect/log_parsing/financial_statement/report_file.rb', line 3

def date
  @date
end

#environmentObject (readonly)

Returns the value of attribute environment.



3
4
5
# File 'lib/global_collect/log_parsing/financial_statement/report_file.rb', line 3

def environment
  @environment
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/global_collect/log_parsing/financial_statement/report_file.rb', line 3

def path
  @path
end

Instance Method Details

#parseObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/global_collect/log_parsing/financial_statement/report_file.rb', line 31

def parse
  begin
    file = File.open(@path, "r")
    # Skip the BOM if it appears
    if (1..3).map{|i| file.getc.chr }.join != "\357\273\277"
      file.rewind
    end
    csv = FasterCSV.new(file, :col_sep => "\t")
    hashes = csv.map{|l| convert_line(l) }
    @data = {
      :header  => hashes.first,
      :trailer => hashes.last ,
      :data   => hashes[1..-2]
    }
  ensure
    file.close
  end
end