Class: BasicInfoDataReader

Inherits:
Object
  • Object
show all
Defined in:
lib/udise_school_report_reader/basic_info_data_reader.rb

Class Method Summary collapse

Class Method Details

.read(lines) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/udise_school_report_reader/basic_info_data_reader.rb', line 2

def self.read(lines)
  require 'yaml'
  template_path = File.join(UdiseSchoolReportReader::ROOT_PATH, 'template.yml')
  template = YAML.load_file(template_path)
  data = { 'basic_info' => template['basic_info'] }

  lines.each_with_index do |line, i|
    next_line = lines[i + 1]&.strip

    case line
    when "UDISE CODE"
      if next_line && (match = next_line.match(/(\d{2})\s*(\d{2})\s*(\d{2})\s*(\d{2})\s*(\d{3})/))
        data['basic_info']['udise_code'] = match[1..5].join('')
      end
    when "School Name"
      if next_line && next_line.include?("CARMEL")
        data['basic_info']['name'] = next_line
      end
    when /Academic Year.*:\s*(\d{4}-\d{2})/
      data['basic_info']['academic_year'] = $1
    end
  end

  # Clean up empty sections
  data['basic_info'].reject! { |_, v| v.nil? || (v.is_a?(Hash) && v.empty?) }

  data
end