Class: PSGC::Import::ImportProvinceMunicipalities::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/psgc/import/import_province_municipalities.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



57
58
59
60
61
# File 'lib/psgc/import/import_province_municipalities.rb', line 57

def initialize
  @cities = []
  @municipalities = []
  @hrefs = {}
end

Instance Attribute Details

#citiesObject (readonly)

Returns the value of attribute cities.



55
56
57
# File 'lib/psgc/import/import_province_municipalities.rb', line 55

def cities
  @cities
end

#hrefsObject (readonly)

Returns the value of attribute hrefs.



55
56
57
# File 'lib/psgc/import/import_province_municipalities.rb', line 55

def hrefs
  @hrefs
end

#municipalitiesObject (readonly)

Returns the value of attribute municipalities.



55
56
57
# File 'lib/psgc/import/import_province_municipalities.rb', line 55

def municipalities
  @municipalities
end

Instance Method Details

#parse(html) ⇒ Object



63
64
65
66
67
68
# File 'lib/psgc/import/import_province_municipalities.rb', line 63

def parse(html)
  html.css('table').each do |table|
    rows = table/:tr
    parse_row(rows[0]) if rows.count == 1
  end
end

#parse_row(tr) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/psgc/import/import_province_municipalities.rb', line 70

def parse_row(tr)
  tds = tr/:td
  if tds.size == 8
    a = tds[0].css('p a')
    if a.count > 0
      name = a.text
      href = a[0]['href']
      code = tds[1].text
      city_class = tds[3].text.gsub("\u00A0", '').strip
      city = !city_class.empty?
      h = [code, name]
      if city
        @cities << h
      else
        @municipalities << h
      end
      @hrefs[code] = href
    end
  end
end