Class: MyRepresentatives::QLD::CSVLower

Inherits:
Object
  • Object
show all
Includes:
Fileable, Guessable
Defined in:
lib/my_representatives/qld/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.



11
12
13
14
15
16
17
18
19
# File 'lib/my_representatives/qld/csv_lower.rb', line 11

def initialize
  self.xls_url = "https://www.parliament.qld.gov.au/documents/Members/mailingLists/MEMMERGEEXCEL.xls"
  self.xls_filename = "tmp/csv_qld_lower.xls"
  self.csv_filename = "tmp/csv_qld_lower.csv"
  self.people = []

  csv_from_url
  people_from_csv
end

Instance Attribute Details

#csv_filenameObject

Returns the value of attribute csv_filename.



9
10
11
# File 'lib/my_representatives/qld/csv_lower.rb', line 9

def csv_filename
  @csv_filename
end

#peopleObject

Returns the value of attribute people.



9
10
11
# File 'lib/my_representatives/qld/csv_lower.rb', line 9

def people
  @people
end

#xls_filenameObject

Returns the value of attribute xls_filename.



9
10
11
# File 'lib/my_representatives/qld/csv_lower.rb', line 9

def xls_filename
  @xls_filename
end

#xls_urlObject

Returns the value of attribute xls_url.



9
10
11
# File 'lib/my_representatives/qld/csv_lower.rb', line 9

def xls_url
  @xls_url
end

Instance Method Details

#csv_from_urlObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/my_representatives/qld/csv_lower.rb', line 21

def csv_from_url
  create_tmp
  open(@xls_filename, "wb") do |file|
    open(@xls_url) do |uri|
       file.write(uri.read)
    end
  end

  excel = Excel2CSV.read(@xls_filename)
  CSV.open(@csv_filename, "wb") do |csv|
    excel.each do |row|
      csv << row
    end
  end
  FileUtils.rm_r(@xls_filename)
end

#people_from_csvObject



38
39
40
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
# File 'lib/my_representatives/qld/csv_lower.rb', line 38

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_qld_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.formal_name                = format_formal_name(person)
    person.physical_address           = format_physical_address(row)
    person.postal_address             = format_postal_address(row)
    person.gender                     = guess_gender(person.title)
    person.honorifics                 = "MP"
    person.preferred_name             = nil
    person.salutation                 = nil
    person.image_url                  = nil
    person.homepage_url               = nil

    @people << person
  end
end