Class: Lakes::Texas::LakeCharacteristicsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lakes/texas/lake_characteristics_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ LakeCharacteristicsParser

Returns a new instance of LakeCharacteristicsParser.



9
10
11
12
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 9

def initialize(text)
  @raw_text = text
  parse
end

Instance Attribute Details

#location_descObject (readonly)

Returns the value of attribute location_desc.



4
5
6
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 4

def location_desc
  @location_desc
end

#max_depth_in_feetObject (readonly)

Returns the value of attribute max_depth_in_feet.



6
7
8
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 6

def max_depth_in_feet
  @max_depth_in_feet
end

#max_depth_raw_textObject (readonly)

Returns the value of attribute max_depth_raw_text.



6
7
8
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 6

def max_depth_raw_text
  @max_depth_raw_text
end

#raw_textObject (readonly)

Returns the value of attribute raw_text.



4
5
6
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 4

def raw_text
  @raw_text
end

#surface_area_in_acresObject (readonly)

Returns the value of attribute surface_area_in_acres.



5
6
7
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 5

def surface_area_in_acres
  @surface_area_in_acres
end

#surface_area_raw_textObject (readonly)

Returns the value of attribute surface_area_raw_text.



5
6
7
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 5

def surface_area_raw_text
  @surface_area_raw_text
end

#year_impoundedObject (readonly)

Returns the value of attribute year_impounded.



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

def year_impounded
  @year_impounded
end

#year_impounded_raw_textObject (readonly)

Returns the value of attribute year_impounded_raw_text.



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

def year_impounded_raw_text
  @year_impounded_raw_text
end

Instance Method Details

#cleanup_raw_text(raw_text) ⇒ Object



56
57
58
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 56

def cleanup_raw_text(raw_text)
  raw_text.try(:gsub, /\s+/, ' ').try(:strip)
end

#parseObject



14
15
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
51
52
53
54
# File 'lib/lakes/texas/lake_characteristics_parser.rb', line 14

def parse
  @location_desc = @raw_text.match(/^location:(.*)(surface area)|(surface acres)|(maximum depth|impounded):/im).captures.first
  @surface_area_raw_text = @raw_text.match(/surface (area|acres):(.*)/i).try(:captures).try(:[], 1)
  @max_depth_raw_text = @raw_text.match(/maximum depth:(.*)/i).try(:captures).try(:first)
  @year_impounded_raw_text = @raw_text.match(/impounded:(.*)/im).try(:captures).try(:first)

  @location_desc = cleanup_raw_text(@location_desc)

  @surface_area_in_acres = cleanup_raw_text(@surface_area_raw_text)
    .try(:match, /^([0-9,]+)/)
    .try(:captures)
    .try(:first)
    .try(:delete, ',')
    .try(:to_i)

  @max_depth_in_feet = cleanup_raw_text(@max_depth_raw_text)
    .try(:match, /^([0-9,]+)/)
    .try(:captures)
    .try(:first)
    .try(:delete, ',')
    .try(:to_i)

  # need to handle bad data like Lake Fryer which is:
  # Maximum depth: Average 13 feet, maximum 25 feet
  if @max_depth_in_feet.nil?
    @max_depth_in_feet = cleanup_raw_text(@max_depth_raw_text)
      .try(:match, /maximum ([0-9,]+) feet/i)
      .try(:captures)
      .try(:first)
      .try(:delete, ',')
      .try(:to_i)
  end

  @year_impounded = cleanup_raw_text(@year_impounded_raw_text)
    .try(:match, /([0-9,]+)/)
    .try(:captures)
    .try(:first)
    .try(:delete, ',')
    .try(:to_i)

end