Class: MyRepresentatives::SA::CSVLower

Inherits:
Object
  • Object
show all
Includes:
Fileable, Guessable
Defined in:
lib/my_representatives/sa/csv_lower.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Guessable

#guess_first, #guess_gender, #guess_last, #guess_preferred, #guess_title

Methods included from Fileable

#create_tmp

Constructor Details

#initializeCSVLower

Returns a new instance of CSVLower.



9
10
11
12
13
14
15
16
# File 'lib/my_representatives/sa/csv_lower.rb', line 9

def initialize
  @csv_url      = "http://www2.parliament.sa.gov.au/Internet/DesktopModules/CreateMM.aspx?type=ha"
  @csv_filename = "tmp/csv_sa_lower.csv"
  @people       = []

  csv_from_url
  people_from_csv
end

Instance Attribute Details

#csv_filenameObject

Returns the value of attribute csv_filename.



7
8
9
# File 'lib/my_representatives/sa/csv_lower.rb', line 7

def csv_filename
  @csv_filename
end

#csv_urlObject

Returns the value of attribute csv_url.



7
8
9
# File 'lib/my_representatives/sa/csv_lower.rb', line 7

def csv_url
  @csv_url
end

#peopleObject

Returns the value of attribute people.



7
8
9
# File 'lib/my_representatives/sa/csv_lower.rb', line 7

def people
  @people
end

Instance Method Details

#csv_from_urlObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/my_representatives/sa/csv_lower.rb', line 18

def csv_from_url
  create_tmp

  f = open(@csv_url)
  doc = Nokogiri::HTML(f)

  CSV.open(@csv_filename, "wb") do |csv|
    csv << ["title", "first", "last", "electorate", "address", "email"]
    loops = 0

    doc.css("table").css("tr").each do |row|
      if loops > 0
        arr = []
        row.css("td").each do |cell|
          arr << cell.text
        end
        csv << arr
      end
      loops += 1
    end
  end
end

#people_from_csvObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/my_representatives/sa/csv_lower.rb', line 41

def people_from_csv
  CSV.foreach(@csv_filename, headers: true) do |row|

    # Setup the electorate
    electorate_name = find_electorate(row)
    electorate      = Electorate.new(electorate_name)
    electorate.state_sa_lower!

    # Setup the Representative (Person)
    person = Person.new(electorate)

    person.title                      = find_title(row)
    person.first_name                 = find_first_name(row)
    person.last_name                  = find_last_name(row)
    person.email                      = find_email(row)
    person.phone                      = find_phone(row)
    person.party_name                 = find_party(row)
    person.gender                     = guess_gender(person.title)
    person.formal_name                = format_formal_name(person)
    person.physical_address           = format_physical_address(row)
    person.postal_address             = format_postal_address(row)
    person.honorifics                 = "MP"
    person.preferred_name             = nil
    person.salutation                 = nil
    person.image_url                  = nil
    person.homepage_url               = nil

    @people << person
  end
end