my_representatives 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/LICENSE.md +8 -0
- data/README.md +60 -0
- data/lib/my_representatives/abbreviatable.rb +29 -0
- data/lib/my_representatives/act/csv_lower.rb +118 -0
- data/lib/my_representatives/act/merge_lower.rb +38 -0
- data/lib/my_representatives/act/web_index.rb +37 -0
- data/lib/my_representatives/act/web_index_row.rb +86 -0
- data/lib/my_representatives/act/web_lower.rb +65 -0
- data/lib/my_representatives/act/web_show.rb +46 -0
- data/lib/my_representatives/commonwealth/commonwealth.rb +5 -0
- data/lib/my_representatives/commonwealth/csv_email.rb +93 -0
- data/lib/my_representatives/commonwealth/csv_lower.rb +130 -0
- data/lib/my_representatives/commonwealth/csv_upper.rb +129 -0
- data/lib/my_representatives/commonwealth/merge_lower.rb +54 -0
- data/lib/my_representatives/commonwealth/merge_upper.rb +54 -0
- data/lib/my_representatives/commonwealth/web_index.rb +57 -0
- data/lib/my_representatives/commonwealth/web_lower.rb +55 -0
- data/lib/my_representatives/commonwealth/web_show.rb +99 -0
- data/lib/my_representatives/commonwealth/web_upper.rb +54 -0
- data/lib/my_representatives/electorate.rb +146 -0
- data/lib/my_representatives/errors.rb +7 -0
- data/lib/my_representatives/fileable.rb +11 -0
- data/lib/my_representatives/guessable.rb +120 -0
- data/lib/my_representatives/hashable.rb +11 -0
- data/lib/my_representatives/nsw/csv_lower.rb +115 -0
- data/lib/my_representatives/nsw/csv_upper.rb +115 -0
- data/lib/my_representatives/nsw/merge_lower.rb +41 -0
- data/lib/my_representatives/nsw/merge_upper.rb +41 -0
- data/lib/my_representatives/nsw/web_index.rb +56 -0
- data/lib/my_representatives/nsw/web_lower.rb +54 -0
- data/lib/my_representatives/nsw/web_show.rb +140 -0
- data/lib/my_representatives/nsw/web_upper.rb +54 -0
- data/lib/my_representatives/nt/web_index.rb +46 -0
- data/lib/my_representatives/nt/web_lower.rb +54 -0
- data/lib/my_representatives/nt/web_show.rb +134 -0
- data/lib/my_representatives/person.rb +197 -0
- data/lib/my_representatives/qld/csv_lower.rb +117 -0
- data/lib/my_representatives/qld/merge_lower.rb +37 -0
- data/lib/my_representatives/qld/web_index.rb +47 -0
- data/lib/my_representatives/qld/web_lower.rb +54 -0
- data/lib/my_representatives/qld/web_show.rb +146 -0
- data/lib/my_representatives/sa/csv_lower.rb +120 -0
- data/lib/my_representatives/sa/csv_upper.rb +115 -0
- data/lib/my_representatives/sa/merge_lower.rb +39 -0
- data/lib/my_representatives/sa/merge_upper.rb +39 -0
- data/lib/my_representatives/sa/web_index_lower.rb +42 -0
- data/lib/my_representatives/sa/web_index_upper.rb +43 -0
- data/lib/my_representatives/sa/web_lower.rb +54 -0
- data/lib/my_representatives/sa/web_show.rb +158 -0
- data/lib/my_representatives/sa/web_upper.rb +54 -0
- data/lib/my_representatives/static.rb +5 -0
- data/lib/my_representatives/tas/csv_lower.rb +125 -0
- data/lib/my_representatives/tas/csv_upper.rb +125 -0
- data/lib/my_representatives/version.rb +5 -0
- data/lib/my_representatives/vic/csv_lower.rb +99 -0
- data/lib/my_representatives/vic/csv_upper.rb +97 -0
- data/lib/my_representatives/vic/merge_lower.rb +37 -0
- data/lib/my_representatives/vic/merge_upper.rb +37 -0
- data/lib/my_representatives/vic/web_index.rb +58 -0
- data/lib/my_representatives/vic/web_lower.rb +54 -0
- data/lib/my_representatives/vic/web_show.rb +118 -0
- data/lib/my_representatives/vic/web_upper.rb +54 -0
- data/lib/my_representatives/wa/csv_lower.rb +144 -0
- data/lib/my_representatives/wa/csv_upper.rb +131 -0
- data/lib/my_representatives/wa/merge_lower.rb +41 -0
- data/lib/my_representatives/wa/merge_upper.rb +37 -0
- data/lib/my_representatives/wa/web_index.rb +45 -0
- data/lib/my_representatives/wa/web_lower.rb +54 -0
- data/lib/my_representatives/wa/web_show.rb +195 -0
- data/lib/my_representatives/wa/web_upper.rb +54 -0
- data/lib/my_representatives.rb +150 -0
- data/my_representatives.gemspec +25 -0
- metadata +204 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module SA
|
3
|
+
class MergeUpper
|
4
|
+
attr_accessor :csv_people, :web_people, :people
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@csv_people = MyRepresentatives::SA::CSVUpper.new.people
|
8
|
+
@web_people = MyRepresentatives::SA::WebUpper.new.people
|
9
|
+
@people = []
|
10
|
+
check_and_update_person
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def check_and_update_person
|
16
|
+
@csv_people.each do |person|
|
17
|
+
if @web_people.any? { |wp| wp.last_name == person.last_name }
|
18
|
+
index = @web_people.find_index { |wp| wp.last_name == person.last_name}
|
19
|
+
|
20
|
+
if index
|
21
|
+
person.formal_name = @web_people[index].formal_name
|
22
|
+
person.first_name = @web_people[index].first_name
|
23
|
+
person.preferred_name = @web_people[index].preferred_name
|
24
|
+
person.party_name = @web_people[index].party_name
|
25
|
+
person.email = @web_people[index].email unless person.email
|
26
|
+
person.phone = @web_people[index].phone unless person.phone
|
27
|
+
person.homepage_url = @web_people[index].homepage_url
|
28
|
+
person.image_url = @web_people[index].image_url
|
29
|
+
person.successful_merge = true
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
@people << person
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end # MergeUpper
|
38
|
+
end # SA
|
39
|
+
end # MyRepresentatives
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module SA
|
3
|
+
class WebIndexLower
|
4
|
+
attr_accessor :index_url, :document, :lower_urls
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@logger = Logger.new(STDOUT)
|
8
|
+
@lower_urls = []
|
9
|
+
|
10
|
+
# Lower House
|
11
|
+
@index_url = "http://www2.parliament.sa.gov.au/Internet/DesktopModules/memberlist.aspx?sort=name&house=HA"
|
12
|
+
@document = find_representatives
|
13
|
+
representative_urls_from_document
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def find_representatives
|
20
|
+
begin
|
21
|
+
request = open(@index_url)
|
22
|
+
Nokogiri::HTML(request)
|
23
|
+
rescue SocketError => err
|
24
|
+
@logger.debug("Unable to connect to #{@index_url}")
|
25
|
+
raise err
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def representative_urls_from_document
|
31
|
+
begin
|
32
|
+
@document.css("table#list").css("a").each do |link|
|
33
|
+
uri = link.attr("href")
|
34
|
+
@lower_urls << "http://www2.parliament.sa.gov.au/Internet/DesktopModules/#{uri}" if uri.include?("MemberDrill.aspx?pid=")
|
35
|
+
end
|
36
|
+
rescue
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end # WebIndexLower
|
41
|
+
end # SA
|
42
|
+
end # MyRepresentatives
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module SA
|
3
|
+
class WebIndexUpper
|
4
|
+
attr_accessor :index_url, :document, :upper_urls
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@logger = Logger.new(STDOUT)
|
8
|
+
@upper_urls = []
|
9
|
+
|
10
|
+
# Upper House
|
11
|
+
@index_url = "http://www2.parliament.sa.gov.au/Internet/DesktopModules/memberlist.aspx?sort=name&house=LC"
|
12
|
+
@document = find_representatives
|
13
|
+
representative_urls_from_document
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def find_representatives
|
21
|
+
begin
|
22
|
+
request = open(@index_url)
|
23
|
+
Nokogiri::HTML(request)
|
24
|
+
rescue SocketError => err
|
25
|
+
@logger.debug("Unable to connect to #{@index_url}")
|
26
|
+
raise err
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def representative_urls_from_document
|
32
|
+
begin
|
33
|
+
@document.css("table#list").css("a").each do |link|
|
34
|
+
uri = link.attr("href")
|
35
|
+
@upper_urls << "http://www2.parliament.sa.gov.au/Internet/DesktopModules/#{uri}" if uri.include?("MemberDrill.aspx?pid=")
|
36
|
+
end
|
37
|
+
rescue
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end # WebIndex
|
42
|
+
end # QLD
|
43
|
+
end # MyRepresentatives
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module SA
|
3
|
+
class WebLower
|
4
|
+
include MyRepresentatives::Abbreviatable
|
5
|
+
include MyRepresentatives::Guessable
|
6
|
+
|
7
|
+
attr_accessor :urls, :people
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@logger = Logger.new(STDOUT)
|
11
|
+
@urls = WebIndexLower.new.lower_urls
|
12
|
+
@people = []
|
13
|
+
|
14
|
+
@urls.each do |url|
|
15
|
+
document = WebShow.new(url)
|
16
|
+
create_person_from_document(document)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def create_person_from_document(document)
|
23
|
+
@logger.info("Attempting to create #{document.formal_name}")
|
24
|
+
|
25
|
+
# Setup the electorate
|
26
|
+
electorate_name = document.electorate_name
|
27
|
+
electorate = Electorate.new(electorate_name)
|
28
|
+
electorate.state_sa_lower!
|
29
|
+
|
30
|
+
# Setup the Representative (Person)
|
31
|
+
person = MyRepresentatives::Person.new(electorate)
|
32
|
+
person.formal_name = document.formal_name
|
33
|
+
person.title = document.title
|
34
|
+
person.first_name = document.first_name
|
35
|
+
person.preferred_name = document.preferred_name
|
36
|
+
person.last_name = document.last_name
|
37
|
+
person.email = document.email
|
38
|
+
person.phone = document.phone
|
39
|
+
person.party_name = document.party_name
|
40
|
+
person.image_url = document.image_url
|
41
|
+
person.homepage_url = document.homepage_url
|
42
|
+
person.gender = guess_gender(document.title)
|
43
|
+
person.physical_address = nil
|
44
|
+
person.postal_address = nil
|
45
|
+
person.honorifics = nil
|
46
|
+
person.salutation = nil
|
47
|
+
|
48
|
+
@people << person
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end # WebLower
|
53
|
+
end # SA
|
54
|
+
end # MyRepresentatives
|
@@ -0,0 +1,158 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module SA
|
3
|
+
class WebShow
|
4
|
+
include MyRepresentatives::Guessable
|
5
|
+
|
6
|
+
attr_accessor :url, :document, :logger
|
7
|
+
|
8
|
+
def initialize(url)
|
9
|
+
@logger = Logger.new(STDOUT)
|
10
|
+
raise MyRepresentatives::InvalidURLError unless url && url.is_a?(String)
|
11
|
+
@url = url
|
12
|
+
fetch_document
|
13
|
+
raise MyRepresentatives::NokogiriDocumentPropertiesError unless test_document_for_validity
|
14
|
+
end
|
15
|
+
|
16
|
+
def homepage_url
|
17
|
+
@url
|
18
|
+
end
|
19
|
+
|
20
|
+
def formal_name
|
21
|
+
begin
|
22
|
+
@document.title.strip.gsub("Profile: ","")
|
23
|
+
rescue NoMethodError
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def title
|
29
|
+
begin
|
30
|
+
guess_title(formal_name)
|
31
|
+
rescue NoMethodError
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def first_name
|
37
|
+
begin
|
38
|
+
guess_first(formal_name)
|
39
|
+
rescue NoMethodError
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def last_name
|
45
|
+
begin
|
46
|
+
guess_last(formal_name)
|
47
|
+
rescue NoMethodError
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def preferred_name
|
53
|
+
begin
|
54
|
+
guess_preferred(formal_name)
|
55
|
+
rescue NoMethodError
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def electorate_name
|
61
|
+
begin
|
62
|
+
@document.at('td.boxedtext:contains("Electorate")').next_element.text
|
63
|
+
rescue NoMethodError
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def party_name
|
69
|
+
begin
|
70
|
+
@document.at('td.boxedtext:contains("Political Party")').next_element.text
|
71
|
+
rescue NoMethodError
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def email
|
77
|
+
begin
|
78
|
+
regex = /Email(.+?@.+?.sa.gov.au)/
|
79
|
+
text = @document.text.strip
|
80
|
+
match = text.match(regex)
|
81
|
+
if match
|
82
|
+
match[1]
|
83
|
+
else
|
84
|
+
nil
|
85
|
+
end
|
86
|
+
rescue NoMethodError
|
87
|
+
nil
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def image_url
|
92
|
+
id = homepage_url.split("=")
|
93
|
+
"http://www2.parliament.sa.gov.au/FormerMembers/Common/Handler/PersonImage.ashx?PersonId=#{id.last}"
|
94
|
+
end
|
95
|
+
|
96
|
+
def phone
|
97
|
+
begin
|
98
|
+
phone = @document.at('b:contains("Electorate Telephone:")')
|
99
|
+
alt_phone = @document.at('b:contains("Telephone:")')
|
100
|
+
if phone
|
101
|
+
phone = phone.parent.text
|
102
|
+
regex = /Electorate Telephone:(08 [0-9]{4} [0-9]{4})/
|
103
|
+
alt_regex = /Electorate Telephone:([0-9]{4} [0-9]{4})/
|
104
|
+
match = phone.match(regex)
|
105
|
+
match_alt = phone.match(alt_regex)
|
106
|
+
|
107
|
+
if match
|
108
|
+
match[1]
|
109
|
+
elsif match_alt
|
110
|
+
"08"+match_alt[1]
|
111
|
+
else
|
112
|
+
nil
|
113
|
+
end
|
114
|
+
elsif alt_phone
|
115
|
+
phone = alt_phone.parent.text
|
116
|
+
regex = /Telephone:(08 [0-9]{4} [0-9]{4})/
|
117
|
+
alt_regex = /Telephone:([0-9]{4} [0-9]{4})/
|
118
|
+
last_attempt = /Telephone:(1800 [0-9]{3} [0-9]{3})/
|
119
|
+
match = phone.match(regex)
|
120
|
+
match_alt = phone.match(alt_regex)
|
121
|
+
match_last = phone.match(last_attempt)
|
122
|
+
|
123
|
+
if match
|
124
|
+
match[1]
|
125
|
+
elsif match_alt
|
126
|
+
"08"+match_alt[1]
|
127
|
+
elsif match_last
|
128
|
+
match_last[1]
|
129
|
+
else
|
130
|
+
nil
|
131
|
+
end
|
132
|
+
else
|
133
|
+
nil
|
134
|
+
end
|
135
|
+
rescue
|
136
|
+
nil
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
private
|
141
|
+
|
142
|
+
def fetch_document
|
143
|
+
begin
|
144
|
+
request = open(@url)
|
145
|
+
@document = Nokogiri::HTML(request)
|
146
|
+
rescue => err
|
147
|
+
@logger.debug("Failed to connect to the url: #{@url}")
|
148
|
+
raise err
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_document_for_validity
|
153
|
+
true
|
154
|
+
end
|
155
|
+
|
156
|
+
end # WebShow
|
157
|
+
end # SA
|
158
|
+
end # MyRepresentatives
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module SA
|
3
|
+
class WebUpper
|
4
|
+
include MyRepresentatives::Abbreviatable
|
5
|
+
include MyRepresentatives::Guessable
|
6
|
+
|
7
|
+
attr_accessor :urls, :people
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@logger = Logger.new(STDOUT)
|
11
|
+
@urls = WebIndexUpper.new.upper_urls
|
12
|
+
@people = []
|
13
|
+
|
14
|
+
@urls.each do |url|
|
15
|
+
document = WebShow.new(url)
|
16
|
+
create_person_from_document(document)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def create_person_from_document(document)
|
23
|
+
@logger.info("Attempting to create #{document.formal_name}")
|
24
|
+
|
25
|
+
# Setup the electorate
|
26
|
+
electorate_name = "All SA"
|
27
|
+
electorate = Electorate.new(electorate_name)
|
28
|
+
electorate.state_sa_upper!
|
29
|
+
|
30
|
+
# Setup the Representative (Person)
|
31
|
+
person = MyRepresentatives::Person.new(electorate)
|
32
|
+
person.formal_name = document.formal_name
|
33
|
+
person.title = document.title
|
34
|
+
person.first_name = document.first_name
|
35
|
+
person.preferred_name = document.preferred_name
|
36
|
+
person.last_name = document.last_name
|
37
|
+
person.email = document.email
|
38
|
+
person.phone = document.phone
|
39
|
+
person.party_name = document.party_name
|
40
|
+
person.image_url = document.image_url
|
41
|
+
person.homepage_url = document.homepage_url
|
42
|
+
person.gender = guess_gender(document.title)
|
43
|
+
person.physical_address = nil
|
44
|
+
person.postal_address = nil
|
45
|
+
person.honorifics = nil
|
46
|
+
person.salutation = nil
|
47
|
+
|
48
|
+
@people << person
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end # WebLower
|
53
|
+
end # SA
|
54
|
+
end # MyRepresentatives
|
@@ -0,0 +1,125 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module TAS
|
3
|
+
class CSVLower
|
4
|
+
include MyRepresentatives::Fileable
|
5
|
+
include MyRepresentatives::Guessable
|
6
|
+
|
7
|
+
attr_accessor :csv_url, :csv_filename, :people
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
# Tasmania's Parliament website has a word document for their House of Assembly and Legislative Council
|
11
|
+
# At this time, this gem uses a csv manually created by https://github.com/tennantje/.
|
12
|
+
# https://gist.github.com/tennantje/c94bd62340bd078b9b1ae12168f48388
|
13
|
+
# This gem has opted for this approach given that there is also no scrapable data on the Tasmanian Parliament website,
|
14
|
+
# whereas Northern Territory (for example) has no CSV file, but does at least have a scrapable website.
|
15
|
+
@csv_url = "https://gist.githubusercontent.com/tennantje/c94bd62340bd078b9b1ae12168f48388/raw/"
|
16
|
+
@csv_filename = "tmp/csv_tas_lower.csv"
|
17
|
+
@people = []
|
18
|
+
|
19
|
+
csv_from_url
|
20
|
+
people_from_csv
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def csv_from_url
|
26
|
+
create_tmp
|
27
|
+
begin
|
28
|
+
open(@csv_filename, "wb") do |file|
|
29
|
+
open(@csv_url) do |uri|
|
30
|
+
file.write(uri.read)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
rescue SocketError => err
|
34
|
+
@logger.debug("Unable to connect to #{@csv_url}")
|
35
|
+
raise err
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def people_from_csv
|
40
|
+
CSV.foreach(@csv_filename, headers: true) do |row|
|
41
|
+
|
42
|
+
# Setup the electorate
|
43
|
+
electorate_name = find_electorate(row)
|
44
|
+
electorate = Electorate.new(electorate_name)
|
45
|
+
electorate.state_tas_lower!
|
46
|
+
|
47
|
+
# Setup the Representative (Person)
|
48
|
+
person = Person.new(electorate)
|
49
|
+
|
50
|
+
person.title = find_title(row)
|
51
|
+
person.first_name = find_first_name(row)
|
52
|
+
person.preferred_name = find_preferred_name(row)
|
53
|
+
person.last_name = find_last_name(row)
|
54
|
+
person.email = find_email(row)
|
55
|
+
person.phone = find_phone(row)
|
56
|
+
person.party_name = find_party(row)
|
57
|
+
person.gender = find_gender(row)
|
58
|
+
person.honorifics = "MP"
|
59
|
+
person.formal_name = format_formal_name(person)
|
60
|
+
person.physical_address = format_physical_address(row)
|
61
|
+
person.postal_address = format_postal_address(row)
|
62
|
+
person.salutation = nil
|
63
|
+
person.image_url = nil
|
64
|
+
person.homepage_url = nil
|
65
|
+
|
66
|
+
@people << person
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def find_first_name(arr)
|
72
|
+
arr["FirstName"]
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_last_name(arr)
|
76
|
+
arr["LastName"]
|
77
|
+
end
|
78
|
+
|
79
|
+
def find_preferred_name(arr)
|
80
|
+
arr["PreferredName"]
|
81
|
+
end
|
82
|
+
|
83
|
+
def find_title(arr)
|
84
|
+
arr["Title"]
|
85
|
+
end
|
86
|
+
|
87
|
+
def find_electorate(arr)
|
88
|
+
arr["Electorate"]
|
89
|
+
end
|
90
|
+
|
91
|
+
def find_email(arr)
|
92
|
+
arr["Email"]
|
93
|
+
end
|
94
|
+
|
95
|
+
def find_party(arr)
|
96
|
+
arr["Party"]
|
97
|
+
end
|
98
|
+
|
99
|
+
def find_phone(arr)
|
100
|
+
arr["Phone"]
|
101
|
+
end
|
102
|
+
|
103
|
+
def find_gender(arr)
|
104
|
+
arr["Gender"]
|
105
|
+
end
|
106
|
+
|
107
|
+
def format_formal_name(person)
|
108
|
+
if person.preferred_name
|
109
|
+
"#{person.title} #{person.preferred_name} #{person.last_name} #{person.honorifics}"
|
110
|
+
else
|
111
|
+
"#{person.title} #{person.first_name} #{person.last_name} #{person.honorifics}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def format_physical_address(arr)
|
116
|
+
"#{arr["Address1"]}\n#{arr["Address2"]}\n#{arr["City"]}\n#{arr["State"]} #{arr["Postcode"]}"
|
117
|
+
end
|
118
|
+
|
119
|
+
def format_postal_address(arr)
|
120
|
+
"#{arr["Address1"]}\n#{arr["Address2"]}\n#{arr["City"]}\n#{arr["State"]} #{arr["Postcode"]}"
|
121
|
+
end
|
122
|
+
|
123
|
+
end # CSVLower
|
124
|
+
end # TAS
|
125
|
+
end # MyRepresentatives
|
@@ -0,0 +1,125 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module TAS
|
3
|
+
class CSVUpper
|
4
|
+
include MyRepresentatives::Fileable
|
5
|
+
include MyRepresentatives::Guessable
|
6
|
+
|
7
|
+
attr_accessor :csv_url, :csv_filename, :people
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
# Tasmania's Parliament website has a word document for their House of Assembly and Legislative Council.
|
11
|
+
# At this time, this gem uses a csv manually created by https://github.com/tennantje/.
|
12
|
+
# https://gist.github.com/tennantje/c662bd7a2ac44d2e5839ad73d6d0a80e
|
13
|
+
# This gem has opted for this approach given that there is also no scrapable data on the Tasmanian Parliament website,
|
14
|
+
# whereas Northern Territory (for example) has no CSV file, but does at least have a scrapable website.
|
15
|
+
@csv_url = "https://gist.githubusercontent.com/tennantje/c662bd7a2ac44d2e5839ad73d6d0a80e/raw"
|
16
|
+
@csv_filename = "tmp/csv_tas_upper.csv"
|
17
|
+
@people = []
|
18
|
+
|
19
|
+
csv_from_url
|
20
|
+
people_from_csv
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def csv_from_url
|
26
|
+
create_tmp
|
27
|
+
begin
|
28
|
+
open(@csv_filename, "wb") do |file|
|
29
|
+
open(@csv_url) do |uri|
|
30
|
+
file.write(uri.read)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
rescue SocketError => err
|
34
|
+
@logger.debug("Unable to connect to #{@csv_url}")
|
35
|
+
raise err
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def people_from_csv
|
40
|
+
CSV.foreach(@csv_filename, headers: true) do |row|
|
41
|
+
|
42
|
+
# Setup the electorate
|
43
|
+
electorate_name = find_electorate(row)
|
44
|
+
electorate = Electorate.new(electorate_name)
|
45
|
+
electorate.state_tas_upper!
|
46
|
+
|
47
|
+
# Setup the Representative (Person)
|
48
|
+
person = Person.new(electorate)
|
49
|
+
|
50
|
+
person.title = find_title(row)
|
51
|
+
person.first_name = find_first_name(row)
|
52
|
+
person.preferred_name = find_preferred_name(row)
|
53
|
+
person.last_name = find_last_name(row)
|
54
|
+
person.email = find_email(row)
|
55
|
+
person.phone = find_phone(row)
|
56
|
+
person.party_name = find_party(row)
|
57
|
+
person.gender = find_gender(row)
|
58
|
+
person.honorifics = "MLC"
|
59
|
+
person.formal_name = format_formal_name(person)
|
60
|
+
person.physical_address = format_physical_address(row)
|
61
|
+
person.postal_address = format_postal_address(row)
|
62
|
+
person.salutation = nil
|
63
|
+
person.image_url = nil
|
64
|
+
person.homepage_url = nil
|
65
|
+
|
66
|
+
@people << person
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def find_first_name(arr)
|
72
|
+
arr["FirstName"]
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_last_name(arr)
|
76
|
+
arr["LastName"]
|
77
|
+
end
|
78
|
+
|
79
|
+
def find_preferred_name(arr)
|
80
|
+
arr["PreferredName"]
|
81
|
+
end
|
82
|
+
|
83
|
+
def find_title(arr)
|
84
|
+
arr["Title"]
|
85
|
+
end
|
86
|
+
|
87
|
+
def find_electorate(arr)
|
88
|
+
arr["Electorate"]
|
89
|
+
end
|
90
|
+
|
91
|
+
def find_email(arr)
|
92
|
+
arr["Email"]
|
93
|
+
end
|
94
|
+
|
95
|
+
def find_party(arr)
|
96
|
+
arr["Party"]
|
97
|
+
end
|
98
|
+
|
99
|
+
def find_phone(arr)
|
100
|
+
arr["Phone"]
|
101
|
+
end
|
102
|
+
|
103
|
+
def find_gender(arr)
|
104
|
+
arr["Gender"]
|
105
|
+
end
|
106
|
+
|
107
|
+
def format_formal_name(person)
|
108
|
+
if person.preferred_name
|
109
|
+
"#{person.title} #{person.preferred_name} #{person.last_name} #{person.honorifics}"
|
110
|
+
else
|
111
|
+
"#{person.title} #{person.first_name} #{person.last_name} #{person.honorifics}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def format_physical_address(arr)
|
116
|
+
"#{arr["Address1"]}\n#{arr["City"]}\nTAS #{arr["Postcode"]}"
|
117
|
+
end
|
118
|
+
|
119
|
+
def format_postal_address(arr)
|
120
|
+
"#{arr["Address1"]}\n#{arr["City"]}\nTAS #{arr["Postcode"]}"
|
121
|
+
end
|
122
|
+
|
123
|
+
end # CSVUpper
|
124
|
+
end # TAS
|
125
|
+
end # MyRepresentatives
|