Class: Lakes::Texas::LakeRecordsParser

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/lakes/texas/lake_records_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#cleanup_data, #cleanup_raw_text, #convert_relative_href, #http_get, #process_data_table

Constructor Details

#initialize(text) ⇒ LakeRecordsParser

Returns a new instance of LakeRecordsParser.



10
11
12
13
14
# File 'lib/lakes/texas/lake_records_parser.rb', line 10

def initialize(text)
  @raw_text = text
  @records = {}
  parse
end

Instance Attribute Details

#raw_textObject (readonly)

Returns the value of attribute raw_text.



7
8
9
# File 'lib/lakes/texas/lake_records_parser.rb', line 7

def raw_text
  @raw_text
end

#recordsObject (readonly)

Returns the value of attribute records.



8
9
10
# File 'lib/lakes/texas/lake_records_parser.rb', line 8

def records
  @records
end

Instance Method Details

#parseObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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