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,130 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module Commonwealth
|
3
|
+
class CSVLower
|
4
|
+
include MyRepresentatives::Abbreviatable
|
5
|
+
include MyRepresentatives::Fileable
|
6
|
+
include MyRepresentatives::Guessable
|
7
|
+
|
8
|
+
attr_accessor :csv_url, :csv_filename, :people, :logger
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@logger = Logger.new(STDOUT)
|
12
|
+
@csv_url = "http://www.aph.gov.au/~/media/03%20Senators%20and%20Members/Address%20Labels%20and%20CSV%20files/SurnameRepsCSV.csv?la=en"
|
13
|
+
@csv_filename = "tmp/csv_commonwealth_lower.csv"
|
14
|
+
@people = []
|
15
|
+
|
16
|
+
csv_from_url
|
17
|
+
people_from_csv
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def csv_from_url
|
23
|
+
create_tmp
|
24
|
+
begin
|
25
|
+
open(@csv_filename, "wb") do |file|
|
26
|
+
open(@csv_url) do |uri|
|
27
|
+
file.write(uri.read)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
rescue SocketError => err
|
31
|
+
@logger.debug("Unable to connect to #{@csv_url}")
|
32
|
+
raise err
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def people_from_csv
|
37
|
+
CSV.foreach(@csv_filename, headers: true) do |row|
|
38
|
+
|
39
|
+
# Setup the electorate
|
40
|
+
electorate_name = find_electorate(row)
|
41
|
+
electorate_state = state_abbreviation(find_electorate_state(row))
|
42
|
+
electorate = Electorate.new(electorate_name)
|
43
|
+
electorate.commonwealth_lower!(electorate_state)
|
44
|
+
|
45
|
+
# Setup the Representative (Person)
|
46
|
+
person = Person.new(electorate)
|
47
|
+
|
48
|
+
person.first_name = find_first_name(row)
|
49
|
+
person.last_name = find_last_name(row)
|
50
|
+
person.preferred_name = find_preferred_name(row)
|
51
|
+
person.title = find_title(row)
|
52
|
+
person.honorifics = find_honorifics(row)
|
53
|
+
person.phone = find_phone(row)
|
54
|
+
person.party_name = find_party(row)
|
55
|
+
person.salutation = find_salutation(row)
|
56
|
+
person.gender = find_gender(row)
|
57
|
+
person.formal_name = format_formal_name(person)
|
58
|
+
person.physical_address = format_physical_address(row)
|
59
|
+
person.postal_address = format_postal_address(row)
|
60
|
+
person.email = nil
|
61
|
+
person.image_url = nil
|
62
|
+
person.homepage_url = nil
|
63
|
+
|
64
|
+
@people << person
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def find_first_name(arr)
|
69
|
+
arr["First Name"]
|
70
|
+
end
|
71
|
+
|
72
|
+
def find_last_name(arr)
|
73
|
+
arr["Surname"]
|
74
|
+
end
|
75
|
+
|
76
|
+
def find_preferred_name(arr)
|
77
|
+
arr["Preferred Name"]
|
78
|
+
end
|
79
|
+
|
80
|
+
def find_title(arr)
|
81
|
+
arr["Title"]
|
82
|
+
end
|
83
|
+
|
84
|
+
def find_honorifics(arr)
|
85
|
+
arr["Honorific"]
|
86
|
+
end
|
87
|
+
|
88
|
+
def find_electorate(arr)
|
89
|
+
arr["Electorate"]
|
90
|
+
end
|
91
|
+
|
92
|
+
def find_electorate_state(arr)
|
93
|
+
arr["State"]
|
94
|
+
end
|
95
|
+
|
96
|
+
def find_gender(arr)
|
97
|
+
arr["Gender"]
|
98
|
+
end
|
99
|
+
|
100
|
+
def find_party(arr)
|
101
|
+
arr["Political Party"]
|
102
|
+
end
|
103
|
+
|
104
|
+
def find_phone(arr)
|
105
|
+
arr["ElectorateTelephone"] ? arr["ElectorateTelephone"] : arr["Telephone"]
|
106
|
+
end
|
107
|
+
|
108
|
+
def find_salutation(arr)
|
109
|
+
arr["Salutation"]
|
110
|
+
end
|
111
|
+
|
112
|
+
def format_formal_name(person)
|
113
|
+
if person.preferred_name
|
114
|
+
"#{person.title} #{person.preferred_name} #{person.last_name} #{person.honorifics}"
|
115
|
+
else
|
116
|
+
"#{person.title} #{person.first_name} #{person.last_name} #{person.honorifics}"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def format_physical_address(arr)
|
121
|
+
"#{arr["ElectorateAddressLine1"]}\n#{arr["ElectorateAddressLine2"]}\n#{arr["ElectorateSuburb"]} #{arr["ElectorateState"]} #{arr["ElectoratePostCode"]}"
|
122
|
+
end
|
123
|
+
|
124
|
+
def format_postal_address(arr)
|
125
|
+
"#{arr["LabelAddress"]}\n#{arr["LabelSuburb"]} #{arr["LabelState"]} #{arr["LabelPostCode"]}"
|
126
|
+
end
|
127
|
+
|
128
|
+
end #CSVLower
|
129
|
+
end # Commonwealth
|
130
|
+
end # MyRepresentatives
|
@@ -0,0 +1,129 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module Commonwealth
|
3
|
+
class CSVUpper
|
4
|
+
include MyRepresentatives::Abbreviatable
|
5
|
+
include MyRepresentatives::Fileable
|
6
|
+
include MyRepresentatives::Guessable
|
7
|
+
|
8
|
+
attr_accessor :csv_url, :csv_filename, :people, :logger
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@logger = Logger.new(STDOUT)
|
12
|
+
@csv_url = "http://www.aph.gov.au/~/media/03%20Senators%20and%20Members/Address%20Labels%20and%20CSV%20files/allsenel.csv?la=en"
|
13
|
+
@csv_filename = "tmp/csv_commonwealth_upper.csv"
|
14
|
+
@people = []
|
15
|
+
|
16
|
+
csv_from_url
|
17
|
+
people_from_csv
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def csv_from_url
|
23
|
+
create_tmp
|
24
|
+
begin
|
25
|
+
open(@csv_filename, "wb") do |file|
|
26
|
+
open(@csv_url) do |uri|
|
27
|
+
file.write(uri.read)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
rescue SocketError => err
|
31
|
+
@logger.debug("Unable to connect to #{@csv_url}")
|
32
|
+
raise err
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def people_from_csv
|
37
|
+
CSV.foreach(@csv_filename, headers: true) do |row|
|
38
|
+
|
39
|
+
# Setup the electorate
|
40
|
+
electorate_name = state_abbreviation(find_electorate_state(row))
|
41
|
+
electorate = Electorate.new(electorate_name)
|
42
|
+
electorate.commonwealth_upper!(electorate_name)
|
43
|
+
|
44
|
+
# Setup the Representative (Person)
|
45
|
+
person = Person.new(electorate)
|
46
|
+
|
47
|
+
person.first_name = find_first_name(row)
|
48
|
+
person.last_name = find_last_name(row)
|
49
|
+
person.preferred_name = find_preferred_name(row)
|
50
|
+
person.title = find_title(row)
|
51
|
+
person.honorifics = find_honorifics(row)
|
52
|
+
person.phone = find_phone(row)
|
53
|
+
person.party_name = find_party(row)
|
54
|
+
person.salutation = find_salutation(row)
|
55
|
+
person.gender = find_gender(row)
|
56
|
+
person.formal_name = format_formal_name(person)
|
57
|
+
person.physical_address = format_physical_address(row)
|
58
|
+
person.postal_address = format_postal_address(row)
|
59
|
+
person.email = nil
|
60
|
+
person.image_url = nil
|
61
|
+
person.homepage_url = nil
|
62
|
+
|
63
|
+
@people << person
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def find_first_name(arr)
|
68
|
+
arr["First Name"]
|
69
|
+
end
|
70
|
+
|
71
|
+
def find_last_name(arr)
|
72
|
+
arr["Surname"]
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_preferred_name(arr)
|
76
|
+
arr["Preferred Name"]
|
77
|
+
end
|
78
|
+
|
79
|
+
def find_title(arr)
|
80
|
+
arr["Title"]
|
81
|
+
end
|
82
|
+
|
83
|
+
def find_honorifics(arr)
|
84
|
+
arr["Honorifics"]
|
85
|
+
end
|
86
|
+
|
87
|
+
def find_electorate(arr)
|
88
|
+
arr["Electorate"]
|
89
|
+
end
|
90
|
+
|
91
|
+
def find_electorate_state(arr)
|
92
|
+
arr["State"]
|
93
|
+
end
|
94
|
+
|
95
|
+
def find_gender(arr)
|
96
|
+
arr["Gender"]
|
97
|
+
end
|
98
|
+
|
99
|
+
def find_party(arr)
|
100
|
+
arr["Political Party"]
|
101
|
+
end
|
102
|
+
|
103
|
+
def find_phone(arr)
|
104
|
+
arr["ElectorateTelephone"] ? arr["ElectorateTelephone"] : arr["Telephone"]
|
105
|
+
end
|
106
|
+
|
107
|
+
def find_salutation(arr)
|
108
|
+
arr["Salutation"]
|
109
|
+
end
|
110
|
+
|
111
|
+
def format_formal_name(person)
|
112
|
+
if person.preferred_name
|
113
|
+
"#{person.title} #{person.preferred_name} #{person.last_name} #{person.honorifics}"
|
114
|
+
else
|
115
|
+
"#{person.title} #{person.first_name} #{person.last_name} #{person.honorifics}"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def format_physical_address(arr)
|
120
|
+
"#{arr["ElectorateAddressLine1"]}\n#{arr["ElectorateAddressLine2"]}\n#{arr["ElectorateSuburb"]} #{arr["ElectorateState"]} #{arr["ElectoratePostCode"]}"
|
121
|
+
end
|
122
|
+
|
123
|
+
def format_postal_address(arr)
|
124
|
+
"#{arr["LabelAddress"]}\n#{arr["LabelSuburb"]} #{arr["LabelState"]} #{arr["LabelPostCode"]}"
|
125
|
+
end
|
126
|
+
|
127
|
+
end #CSVUpper
|
128
|
+
end # Commonwealth
|
129
|
+
end # MyRepresentatives
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module Commonwealth
|
3
|
+
class MergeLower
|
4
|
+
attr_accessor :csv_people, :web_people, :email_people, :people
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@csv_people = MyRepresentatives::Commonwealth::CSVLower.new.people
|
8
|
+
@web_people = MyRepresentatives::Commonwealth::WebLower.new.people
|
9
|
+
@email_people = MyRepresentatives::Commonwealth::CSVEmail.new.people
|
10
|
+
@people = []
|
11
|
+
check_and_update_person
|
12
|
+
add_emails_to_person
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def check_and_update_person
|
18
|
+
@csv_people.each do |person|
|
19
|
+
if @web_people.any? { |wp| wp.phone == person.phone} || @web_people.any? { |wp| wp.email == person.email}
|
20
|
+
index = @web_people.find_index {|wp| wp.phone == person.phone}
|
21
|
+
|
22
|
+
if !index
|
23
|
+
index = @web_people.find_index { |wp| wp.email == person.email }
|
24
|
+
end
|
25
|
+
|
26
|
+
if index
|
27
|
+
person.email = @web_people[index].email
|
28
|
+
person.homepage_url = @web_people[index].homepage_url
|
29
|
+
person.image_url = @web_people[index].image_url
|
30
|
+
person.successful_merge = true
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
@people << person
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_emails_to_person
|
39
|
+
@people.each do |person|
|
40
|
+
if @email_people.any? { |ep| ep.phone == person.phone} && person.email.nil?
|
41
|
+
index = @email_people.find_index {|ep| ep.phone == person.phone}
|
42
|
+
|
43
|
+
if index
|
44
|
+
person.email = @email_people[index].email
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
end # MergeLower
|
53
|
+
end # Commonwealth
|
54
|
+
end # MyRepresentatives
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module Commonwealth
|
3
|
+
class MergeUpper
|
4
|
+
attr_accessor :csv_people, :web_people, :email_people, :people
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@csv_people = MyRepresentatives::Commonwealth::CSVUpper.new.people
|
8
|
+
@web_people = MyRepresentatives::Commonwealth::WebUpper.new.people
|
9
|
+
@email_people = MyRepresentatives::Commonwealth::CSVEmail.new.people
|
10
|
+
@people = []
|
11
|
+
check_and_update_person
|
12
|
+
add_emails_to_person
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def check_and_update_person
|
18
|
+
@csv_people.each do |person|
|
19
|
+
if @web_people.any? { |wp| wp.phone == person.phone} || @web_people.any? { |wp| wp.email == person.email}
|
20
|
+
index = @web_people.find_index {|wp| wp.phone == person.phone}
|
21
|
+
|
22
|
+
if !index
|
23
|
+
index = @web_people.find_index { |wp| wp.email == person.email }
|
24
|
+
end
|
25
|
+
|
26
|
+
if index
|
27
|
+
person.email = @web_people[index].email
|
28
|
+
person.homepage_url = @web_people[index].homepage_url
|
29
|
+
person.image_url = @web_people[index].image_url
|
30
|
+
person.successful_merge = true
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
@people << person
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_emails_to_person
|
39
|
+
@people.each do |person|
|
40
|
+
if @email_people.any? { |ep| ep.phone == person.phone} && person.email.nil?
|
41
|
+
index = @email_people.find_index {|ep| ep.phone == person.phone}
|
42
|
+
|
43
|
+
if index
|
44
|
+
person.email = @email_people[index].email
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
end # MergeLower
|
53
|
+
end # Commonwealth
|
54
|
+
end # MyRepresentatives
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module Commonwealth
|
3
|
+
class WebIndex
|
4
|
+
attr_accessor :index_url, :document, :senator_urls, :member_urls
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@logger = Logger.new(STDOUT)
|
8
|
+
@index_url = nil
|
9
|
+
@document = nil
|
10
|
+
@member_urls = []
|
11
|
+
@senator_urls = []
|
12
|
+
|
13
|
+
(1..13).each do |page|
|
14
|
+
@index_url = "http://www.aph.gov.au/Senators_and_Members/Parliamentarian_Search_Results?page=#{page}&expand=1&q=&mem=1&par=-1&gen=0&ps=12&st=1"
|
15
|
+
@document = find_representatives
|
16
|
+
representative_urls_from_document( { lower_house: true } )
|
17
|
+
end
|
18
|
+
|
19
|
+
(1..7).each do |page|
|
20
|
+
@index_url = "http://www.aph.gov.au/Senators_and_Members/Parliamentarian_Search_Results?page=#{page}&expand=1&q=&sen=1&par=-1&gen=0&ps=12&st=1"
|
21
|
+
@document = find_representatives
|
22
|
+
representative_urls_from_document( { lower_house: false } )
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def find_representatives
|
30
|
+
begin
|
31
|
+
request = open(@index_url)
|
32
|
+
Nokogiri::HTML(request)
|
33
|
+
rescue SocketError => err
|
34
|
+
@logger.debug("Unable to connect to #{@index_url}")
|
35
|
+
raise err
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def representative_urls_from_document(options = { lower_house: true } )
|
40
|
+
begin
|
41
|
+
results = @document.css("div.search-filter-results")
|
42
|
+
representatives = results.css("div.row") if results
|
43
|
+
rescue
|
44
|
+
end
|
45
|
+
|
46
|
+
representatives.each do |row|
|
47
|
+
begin
|
48
|
+
@member_urls << "http://www.aph.gov.au" + row.css("h4.title").css("a").first.attr("href") if options[:lower_house]
|
49
|
+
@senator_urls << "http://www.aph.gov.au" + row.css("h4.title").css("a").first.attr("href") if !options[:lower_house]
|
50
|
+
rescue
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end # WebIndex
|
56
|
+
end # Commonwealth
|
57
|
+
end # MyRepresentatives
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module Commonwealth
|
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 = WebIndex.new.member_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
|
+
state_name = state_abbreviation(document.state_name)
|
28
|
+
electorate = MyRepresentatives::Electorate.new(electorate_name)
|
29
|
+
electorate.commonwealth_lower!(state_name)
|
30
|
+
|
31
|
+
# Setup the Representative (Person)
|
32
|
+
person = MyRepresentatives::Person.new(electorate)
|
33
|
+
person.first_name = guess_first(document.formal_name)
|
34
|
+
person.last_name = guess_last(document.formal_name)
|
35
|
+
person.title = guess_title(document.formal_name)
|
36
|
+
person.email = document.email
|
37
|
+
person.phone = document.phone
|
38
|
+
person.party_name = document.party_name
|
39
|
+
person.image_url = document.image_url
|
40
|
+
person.homepage_url = document.homepage_url
|
41
|
+
person.formal_name = document.formal_name
|
42
|
+
person.physical_address = nil
|
43
|
+
person.postal_address = nil
|
44
|
+
person.honorifics = nil
|
45
|
+
person.gender = nil
|
46
|
+
person.preferred_name = nil
|
47
|
+
person.salutation = nil
|
48
|
+
|
49
|
+
@people << person
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end # WebLower
|
54
|
+
end # Commonwealth
|
55
|
+
end # MyRepresentatives
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module Commonwealth
|
3
|
+
class WebShow
|
4
|
+
|
5
|
+
attr_accessor :url, :document, :logger
|
6
|
+
|
7
|
+
def initialize(url)
|
8
|
+
@logger = Logger.new(STDOUT)
|
9
|
+
raise MyRepresentatives::InvalidURLError unless url && url.is_a?(String)
|
10
|
+
@url = url
|
11
|
+
fetch_document
|
12
|
+
raise MyRepresentatives::NokogiriDocumentPropertiesError unless test_document_for_validity
|
13
|
+
end
|
14
|
+
|
15
|
+
def homepage_url
|
16
|
+
@url
|
17
|
+
end
|
18
|
+
|
19
|
+
def formal_name
|
20
|
+
begin
|
21
|
+
@document.css("h1").text.strip
|
22
|
+
rescue NoMethodError
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def party_name
|
28
|
+
begin
|
29
|
+
@document.css(".medium-7").at('dt:contains("Party")').next_element.text.strip
|
30
|
+
rescue NoMethodError
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def phone
|
36
|
+
begin
|
37
|
+
@document.css("#panel21").at('dt:contains("Telephone")').next_element.text.strip
|
38
|
+
rescue NoMethodError
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def email
|
44
|
+
begin
|
45
|
+
@document.css("#panel31").at('dt:contains("Email")').next_element.text.strip
|
46
|
+
rescue NoMethodError
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def electorate_name
|
52
|
+
begin
|
53
|
+
@document.css(".medium-7").css("h3").text.split(",")[0].strip.gsub("Member for ","").gsub("Senator for ","").gsub(/\s{2,}/,"")
|
54
|
+
rescue NoMethodError
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def state_name
|
60
|
+
begin
|
61
|
+
@document.css(".medium-7").css("h3").text.split(",")[1].strip.gsub("Member for ","").gsub("Senator for ","").gsub(/\s{2,}/,"")
|
62
|
+
rescue NoMethodError
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def image_url
|
68
|
+
begin
|
69
|
+
@document.css(".medium-3").css("img").attr('src').value.strip
|
70
|
+
rescue NoMethodError
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def fetch_document
|
78
|
+
begin
|
79
|
+
request = open(@url)
|
80
|
+
@document = Nokogiri::HTML(request)
|
81
|
+
rescue => err
|
82
|
+
@logger.debug("Failed to connect to the url: #{@url}")
|
83
|
+
raise err
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_document_for_validity
|
88
|
+
true
|
89
|
+
# if @document && @document.css("div.section") && @document.css("div.section").css("img")
|
90
|
+
# true
|
91
|
+
# else
|
92
|
+
# @logger.debug("Failed to find a valid document to scrape")
|
93
|
+
# false
|
94
|
+
# end
|
95
|
+
end
|
96
|
+
|
97
|
+
end # WebShow
|
98
|
+
end # Commonwealth
|
99
|
+
end # MyRepresentatives
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MyRepresentatives
|
2
|
+
module Commonwealth
|
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 = WebIndex.new.senator_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
|
+
# Setup the electorate
|
25
|
+
electorate_name = document.electorate_name
|
26
|
+
state_name = state_abbreviation(document.electorate_name)
|
27
|
+
electorate = MyRepresentatives::Electorate.new(electorate_name)
|
28
|
+
electorate.commonwealth_upper!(state_name)
|
29
|
+
|
30
|
+
# Setup the Representative (Person)
|
31
|
+
person = MyRepresentatives::Person.new(electorate)
|
32
|
+
person.first_name = guess_first(document.formal_name)
|
33
|
+
person.last_name = guess_last(document.formal_name)
|
34
|
+
person.title = guess_title(document.formal_name)
|
35
|
+
person.email = document.email
|
36
|
+
person.phone = document.phone
|
37
|
+
person.party_name = document.party_name
|
38
|
+
person.image_url = document.image_url
|
39
|
+
person.homepage_url = document.homepage_url
|
40
|
+
person.formal_name = document.formal_name
|
41
|
+
person.physical_address = nil
|
42
|
+
person.postal_address = nil
|
43
|
+
person.honorifics = nil
|
44
|
+
person.gender = nil
|
45
|
+
person.preferred_name = nil
|
46
|
+
person.salutation = nil
|
47
|
+
|
48
|
+
@people << person
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end # WebUpper
|
53
|
+
end # Commonwealth
|
54
|
+
end # MyRepresentatives
|