class Lakes::Texas::LakeRecordsParser

Attributes

raw_text[R]
records[R]

Public Class Methods

new(text) click to toggle source
# File lib/lakes/texas/lake_records_parser.rb, line 10
def initialize(text)
  @raw_text = text
  @records = {}
  parse
end

Public Instance Methods

parse() click to toggle source
# File lib/lakes/texas/lake_records_parser.rb, line 16
def parse
  lake_records_doc = Nokogiri::HTML(@raw_text)
  lake_records_main_div = lake_records_doc.at('div#maincontent')

  # H2's are record types like:
  # - weight records
  # - catch and release records (by length)

  element = lake_records_main_div.children.first
  current_record_type = nil # Weight or Length
  current_age_group = nil # all ages, youth, etc
  while element = element.next_element
    case element.name
    when 'h2'
      current_record_type = cleanup_data(element.text)
      @records[current_record_type] = {}
    when 'h3'
      current_age_group = cleanup_data(element.text)
      @records[current_record_type][current_age_group] = {}
    when 'table'
      fishing_method = cleanup_data(element.xpath('caption/big').text)

      if @records[current_record_type][current_age_group][fishing_method].nil?
        @records[current_record_type][current_age_group][fishing_method] = []
      end

      headers = element.xpath('tr/th').map{ |r| r.text }
      rows = element.xpath('tr/td').map{ |r| r.text }

      table_data = process_data_table(headers, rows)
      @records[current_record_type][current_age_group][fishing_method] = table_data
    end
  end

end