govkit-ca 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/LICENSE +1 -1
  2. data/README.md +2 -2
  3. data/Rakefile +1 -1
  4. data/govkit-ca.gemspec +8 -8
  5. data/lib/gov_kit-ca/postal_code/strategy/base.rb +33 -1
  6. data/lib/gov_kit-ca/postal_code/strategy/{cbc-ca.rb → cbc_ca.rb} +6 -13
  7. data/lib/gov_kit-ca/postal_code/strategy/{conservative-ca.rb → conservative_ca.rb} +2 -4
  8. data/lib/gov_kit-ca/postal_code/strategy/digital-copyright_ca.rb +27 -0
  9. data/lib/gov_kit-ca/postal_code/strategy/{elections-ca.rb → elections_ca.rb} +2 -4
  10. data/lib/gov_kit-ca/postal_code/strategy/{greenparty-ca.rb → greenparty_ca.rb} +3 -5
  11. data/lib/gov_kit-ca/postal_code/strategy/{liberal-ca.rb → liberal_ca.rb} +3 -5
  12. data/lib/gov_kit-ca/postal_code/strategy/{ndp-ca.rb → ndp_ca.rb} +3 -5
  13. data/lib/gov_kit-ca/postal_code/strategy/{parl-gc-ca.rb → parl_gc_ca.rb} +3 -5
  14. data/lib/gov_kit-ca/postal_code/{strategy.rb → strategy_set.rb} +1 -7
  15. data/lib/gov_kit-ca/postal_code.rb +9 -3
  16. data/lib/gov_kit-ca/version.rb +1 -1
  17. data/lib/gov_kit-ca.rb +2 -1
  18. data/spec/cbc_ca_spec.rb +13 -6
  19. data/spec/digital-copyright_ca_spec.rb +32 -0
  20. data/spec/elections_ca_spec.rb +7 -0
  21. data/spec/fixtures/cbc_ca/A1A1A1.response +11 -0
  22. data/spec/fixtures/cbc_ca/G0C2Y0.response +11 -0
  23. data/spec/fixtures/cbc_ca/H0H0H0.response +309 -0
  24. data/spec/fixtures/cbc_ca/K0A1K0.response +11 -0
  25. data/spec/fixtures/cbc_ca/T5S2B9.response +11 -0
  26. data/spec/fixtures/cbc_ca/X1B1B1.response +309 -0
  27. data/spec/fixtures/conservative_ca/A1A1A1.response +467 -0
  28. data/spec/fixtures/conservative_ca/G0C2Y0.response +467 -0
  29. data/spec/fixtures/conservative_ca/H0H0H0.response +467 -0
  30. data/spec/fixtures/conservative_ca/K0A1K0.response +467 -0
  31. data/spec/fixtures/conservative_ca/T5S2B9.response +467 -0
  32. data/spec/fixtures/conservative_ca/X1B1B1.response +467 -0
  33. data/spec/fixtures/digital-copyright_ca/A1A1A1.response +262 -0
  34. data/spec/fixtures/digital-copyright_ca/G0C2Y0.response +263 -0
  35. data/spec/fixtures/digital-copyright_ca/H0H0H0.response +278 -0
  36. data/spec/fixtures/digital-copyright_ca/K0A1K0.response +264 -0
  37. data/spec/fixtures/digital-copyright_ca/T5S2B9.response +267 -0
  38. data/spec/fixtures/digital-copyright_ca/X1B1B1.response +278 -0
  39. data/spec/fixtures/elections_ca/A1A1A1.response +12 -0
  40. data/spec/fixtures/elections_ca/G0C2Y0.response +12 -0
  41. data/spec/fixtures/elections_ca/H0H0H0.response +11 -0
  42. data/spec/fixtures/elections_ca/K0A1K0.response +11 -0
  43. data/spec/fixtures/elections_ca/T5S2B9.response +12 -0
  44. data/spec/fixtures/elections_ca/X1B1B1.response +11 -0
  45. data/spec/fixtures/greenparty_ca/A1A1A1.response +13 -0
  46. data/spec/fixtures/greenparty_ca/G0C2Y0.response +13 -0
  47. data/spec/fixtures/greenparty_ca/H0H0H0.response +13 -0
  48. data/spec/fixtures/greenparty_ca/K0A1K0.response +13 -0
  49. data/spec/fixtures/greenparty_ca/T5S2B9.response +13 -0
  50. data/spec/fixtures/greenparty_ca/X1B1B1.response +13 -0
  51. data/spec/fixtures/liberal_ca/A1A1A1.response +582 -0
  52. data/spec/fixtures/liberal_ca/G0C2Y0.response +582 -0
  53. data/spec/fixtures/liberal_ca/H0H0H0.response +663 -0
  54. data/spec/fixtures/liberal_ca/K0A1K0.response +663 -0
  55. data/spec/fixtures/liberal_ca/T5S2B9.response +663 -0
  56. data/spec/fixtures/liberal_ca/X1B1B1.response +663 -0
  57. data/spec/fixtures/ndp_ca/A1A1A1.response +12 -0
  58. data/spec/fixtures/ndp_ca/G0C2Y0.response +12 -0
  59. data/spec/fixtures/ndp_ca/H0H0H0.response +12 -0
  60. data/spec/fixtures/ndp_ca/K0A1K0.response +12 -0
  61. data/spec/fixtures/ndp_ca/T5S2B9.response +12 -0
  62. data/spec/fixtures/ndp_ca/X1B1B1.response +12 -0
  63. data/spec/fixtures/parl_gc_ca/A1A1A1.response +337 -0
  64. data/spec/fixtures/parl_gc_ca/G0C2Y0.response +337 -0
  65. data/spec/fixtures/parl_gc_ca/H0H0H0.response +226 -0
  66. data/spec/fixtures/parl_gc_ca/K0A1K0.response +784 -0
  67. data/spec/fixtures/parl_gc_ca/T5S2B9.response +511 -0
  68. data/spec/fixtures/parl_gc_ca/X1B1B1.response +226 -0
  69. data/spec/greenparty_ca_spec.rb +7 -0
  70. data/spec/liberal_ca_spec.rb +10 -2
  71. data/spec/ndp_ca_spec.rb +7 -0
  72. data/spec/postal_code_spec.rb +13 -0
  73. data/spec/spec_helper.rb +13 -0
  74. data/spec/strategy_set_spec.rb +0 -0
  75. data/tasks/postal-code-for-districts.csv +212 -0
  76. data/tasks/tasks.rb +73 -0
  77. metadata +180 -98
  78. data/lib/gov_kit-ca/postal_code/strategy/digital_copyright-ca.rb +0 -28
  79. data/tasks/rid_to_edid.rb +0 -136
metadata CHANGED
@@ -1,90 +1,79 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: govkit-ca
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 2
9
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
12
- - James McKinney
7
+ authors:
8
+ - Open North
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-04-16 00:00:00 -04:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-09-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: httparty
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70168055271360 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- - 6
31
- - 1
32
- version: 0.6.1
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.7.8
33
22
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: nokogiri
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70168055271360
25
+ - !ruby/object:Gem::Dependency
26
+ name: nokogiri
27
+ requirement: &70168055270320 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- segments:
44
- - 1
45
- - 4
46
- - 4
47
- version: 1.4.4
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.5.0
48
33
  type: :runtime
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
51
- name: yajl-ruby
52
34
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70168055270320
36
+ - !ruby/object:Gem::Dependency
37
+ name: yajl-ruby
38
+ requirement: &70168055269100 !ruby/object:Gem::Requirement
54
39
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- segments:
59
- - 0
60
- - 7
61
- - 8
62
- version: 0.7.8
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.8.2
63
44
  type: :runtime
64
- version_requirements: *id003
65
- - !ruby/object:Gem::Dependency
45
+ prerelease: false
46
+ version_requirements: *70168055269100
47
+ - !ruby/object:Gem::Dependency
66
48
  name: rspec
49
+ requirement: &70168055267600 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.6.0
55
+ type: :development
67
56
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
57
+ version_requirements: *70168055267600
58
+ - !ruby/object:Gem::Dependency
59
+ name: fakeweb
60
+ requirement: &70168055247900 !ruby/object:Gem::Requirement
69
61
  none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- segments:
74
- - 2
75
- version: "2"
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.0
76
66
  type: :development
77
- version_requirements: *id004
78
- description: GovKit-CA lets you quickly get encapsulated Ruby objects for Canadian civic data.
79
- email:
80
- - james@slashpoundbang.com
67
+ prerelease: false
68
+ version_requirements: *70168055247900
69
+ description: GovKit-CA lets you quickly get encapsulated Ruby objects for Canadian
70
+ civic data.
71
+ email:
72
+ - info@opennorth.ca
81
73
  executables: []
82
-
83
74
  extensions: []
84
-
85
75
  extra_rdoc_files: []
86
-
87
- files:
76
+ files:
88
77
  - .gitignore
89
78
  - Gemfile
90
79
  - LICENSE
@@ -95,65 +84,158 @@ files:
95
84
  - lib/data/rid_to_edid.yml
96
85
  - lib/gov_kit-ca.rb
97
86
  - lib/gov_kit-ca/postal_code.rb
98
- - lib/gov_kit-ca/postal_code/strategy.rb
99
87
  - lib/gov_kit-ca/postal_code/strategy/base.rb
100
- - lib/gov_kit-ca/postal_code/strategy/cbc-ca.rb
101
- - lib/gov_kit-ca/postal_code/strategy/conservative-ca.rb
102
- - lib/gov_kit-ca/postal_code/strategy/digital_copyright-ca.rb
103
- - lib/gov_kit-ca/postal_code/strategy/elections-ca.rb
104
- - lib/gov_kit-ca/postal_code/strategy/greenparty-ca.rb
105
- - lib/gov_kit-ca/postal_code/strategy/liberal-ca.rb
106
- - lib/gov_kit-ca/postal_code/strategy/ndp-ca.rb
107
- - lib/gov_kit-ca/postal_code/strategy/parl-gc-ca.rb
88
+ - lib/gov_kit-ca/postal_code/strategy/cbc_ca.rb
89
+ - lib/gov_kit-ca/postal_code/strategy/conservative_ca.rb
90
+ - lib/gov_kit-ca/postal_code/strategy/digital-copyright_ca.rb
91
+ - lib/gov_kit-ca/postal_code/strategy/elections_ca.rb
92
+ - lib/gov_kit-ca/postal_code/strategy/greenparty_ca.rb
93
+ - lib/gov_kit-ca/postal_code/strategy/liberal_ca.rb
94
+ - lib/gov_kit-ca/postal_code/strategy/ndp_ca.rb
95
+ - lib/gov_kit-ca/postal_code/strategy/parl_gc_ca.rb
96
+ - lib/gov_kit-ca/postal_code/strategy_set.rb
108
97
  - lib/gov_kit-ca/version.rb
109
98
  - lib/govkit-ca.rb
110
99
  - spec/cbc_ca_spec.rb
100
+ - spec/digital-copyright_ca_spec.rb
111
101
  - spec/elections_ca_spec.rb
102
+ - spec/fixtures/cbc_ca/A1A1A1.response
103
+ - spec/fixtures/cbc_ca/G0C2Y0.response
104
+ - spec/fixtures/cbc_ca/H0H0H0.response
105
+ - spec/fixtures/cbc_ca/K0A1K0.response
106
+ - spec/fixtures/cbc_ca/T5S2B9.response
107
+ - spec/fixtures/cbc_ca/X1B1B1.response
108
+ - spec/fixtures/conservative_ca/A1A1A1.response
109
+ - spec/fixtures/conservative_ca/G0C2Y0.response
110
+ - spec/fixtures/conservative_ca/H0H0H0.response
111
+ - spec/fixtures/conservative_ca/K0A1K0.response
112
+ - spec/fixtures/conservative_ca/T5S2B9.response
113
+ - spec/fixtures/conservative_ca/X1B1B1.response
114
+ - spec/fixtures/digital-copyright_ca/A1A1A1.response
115
+ - spec/fixtures/digital-copyright_ca/G0C2Y0.response
116
+ - spec/fixtures/digital-copyright_ca/H0H0H0.response
117
+ - spec/fixtures/digital-copyright_ca/K0A1K0.response
118
+ - spec/fixtures/digital-copyright_ca/T5S2B9.response
119
+ - spec/fixtures/digital-copyright_ca/X1B1B1.response
120
+ - spec/fixtures/elections_ca/A1A1A1.response
121
+ - spec/fixtures/elections_ca/G0C2Y0.response
122
+ - spec/fixtures/elections_ca/H0H0H0.response
123
+ - spec/fixtures/elections_ca/K0A1K0.response
124
+ - spec/fixtures/elections_ca/T5S2B9.response
125
+ - spec/fixtures/elections_ca/X1B1B1.response
126
+ - spec/fixtures/greenparty_ca/A1A1A1.response
127
+ - spec/fixtures/greenparty_ca/G0C2Y0.response
128
+ - spec/fixtures/greenparty_ca/H0H0H0.response
129
+ - spec/fixtures/greenparty_ca/K0A1K0.response
130
+ - spec/fixtures/greenparty_ca/T5S2B9.response
131
+ - spec/fixtures/greenparty_ca/X1B1B1.response
132
+ - spec/fixtures/liberal_ca/A1A1A1.response
133
+ - spec/fixtures/liberal_ca/G0C2Y0.response
134
+ - spec/fixtures/liberal_ca/H0H0H0.response
135
+ - spec/fixtures/liberal_ca/K0A1K0.response
136
+ - spec/fixtures/liberal_ca/T5S2B9.response
137
+ - spec/fixtures/liberal_ca/X1B1B1.response
138
+ - spec/fixtures/ndp_ca/A1A1A1.response
139
+ - spec/fixtures/ndp_ca/G0C2Y0.response
140
+ - spec/fixtures/ndp_ca/H0H0H0.response
141
+ - spec/fixtures/ndp_ca/K0A1K0.response
142
+ - spec/fixtures/ndp_ca/T5S2B9.response
143
+ - spec/fixtures/ndp_ca/X1B1B1.response
144
+ - spec/fixtures/parl_gc_ca/A1A1A1.response
145
+ - spec/fixtures/parl_gc_ca/G0C2Y0.response
146
+ - spec/fixtures/parl_gc_ca/H0H0H0.response
147
+ - spec/fixtures/parl_gc_ca/K0A1K0.response
148
+ - spec/fixtures/parl_gc_ca/T5S2B9.response
149
+ - spec/fixtures/parl_gc_ca/X1B1B1.response
112
150
  - spec/greenparty_ca_spec.rb
113
151
  - spec/liberal_ca_spec.rb
114
152
  - spec/ndp_ca_spec.rb
115
153
  - spec/postal_code_spec.rb
116
154
  - spec/spec.opts
117
155
  - spec/spec_helper.rb
118
- - tasks/rid_to_edid.rb
119
- has_rdoc: true
120
- homepage: http://github.com/jpmckinney/govkit-ca
156
+ - spec/strategy_set_spec.rb
157
+ - tasks/postal-code-for-districts.csv
158
+ - tasks/tasks.rb
159
+ homepage: http://github.com/opennorth/govkit-ca
121
160
  licenses: []
122
-
123
161
  post_install_message:
124
162
  rdoc_options: []
125
-
126
- require_paths:
163
+ require_paths:
127
164
  - lib
128
- required_ruby_version: !ruby/object:Gem::Requirement
165
+ required_ruby_version: !ruby/object:Gem::Requirement
129
166
  none: false
130
- requirements:
131
- - - ">="
132
- - !ruby/object:Gem::Version
133
- segments:
134
- - 0
135
- version: "0"
136
- required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
172
  none: false
138
- requirements:
139
- - - ">="
140
- - !ruby/object:Gem::Version
141
- segments:
142
- - 0
143
- version: "0"
173
+ requirements:
174
+ - - ! '>='
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
144
177
  requirements: []
145
-
146
178
  rubyforge_project: govkit-ca
147
- rubygems_version: 1.3.7
179
+ rubygems_version: 1.8.6
148
180
  signing_key:
149
181
  specification_version: 3
150
182
  summary: Easy access to Canadian civic data around the web
151
- test_files:
183
+ test_files:
152
184
  - spec/cbc_ca_spec.rb
185
+ - spec/digital-copyright_ca_spec.rb
153
186
  - spec/elections_ca_spec.rb
187
+ - spec/fixtures/cbc_ca/A1A1A1.response
188
+ - spec/fixtures/cbc_ca/G0C2Y0.response
189
+ - spec/fixtures/cbc_ca/H0H0H0.response
190
+ - spec/fixtures/cbc_ca/K0A1K0.response
191
+ - spec/fixtures/cbc_ca/T5S2B9.response
192
+ - spec/fixtures/cbc_ca/X1B1B1.response
193
+ - spec/fixtures/conservative_ca/A1A1A1.response
194
+ - spec/fixtures/conservative_ca/G0C2Y0.response
195
+ - spec/fixtures/conservative_ca/H0H0H0.response
196
+ - spec/fixtures/conservative_ca/K0A1K0.response
197
+ - spec/fixtures/conservative_ca/T5S2B9.response
198
+ - spec/fixtures/conservative_ca/X1B1B1.response
199
+ - spec/fixtures/digital-copyright_ca/A1A1A1.response
200
+ - spec/fixtures/digital-copyright_ca/G0C2Y0.response
201
+ - spec/fixtures/digital-copyright_ca/H0H0H0.response
202
+ - spec/fixtures/digital-copyright_ca/K0A1K0.response
203
+ - spec/fixtures/digital-copyright_ca/T5S2B9.response
204
+ - spec/fixtures/digital-copyright_ca/X1B1B1.response
205
+ - spec/fixtures/elections_ca/A1A1A1.response
206
+ - spec/fixtures/elections_ca/G0C2Y0.response
207
+ - spec/fixtures/elections_ca/H0H0H0.response
208
+ - spec/fixtures/elections_ca/K0A1K0.response
209
+ - spec/fixtures/elections_ca/T5S2B9.response
210
+ - spec/fixtures/elections_ca/X1B1B1.response
211
+ - spec/fixtures/greenparty_ca/A1A1A1.response
212
+ - spec/fixtures/greenparty_ca/G0C2Y0.response
213
+ - spec/fixtures/greenparty_ca/H0H0H0.response
214
+ - spec/fixtures/greenparty_ca/K0A1K0.response
215
+ - spec/fixtures/greenparty_ca/T5S2B9.response
216
+ - spec/fixtures/greenparty_ca/X1B1B1.response
217
+ - spec/fixtures/liberal_ca/A1A1A1.response
218
+ - spec/fixtures/liberal_ca/G0C2Y0.response
219
+ - spec/fixtures/liberal_ca/H0H0H0.response
220
+ - spec/fixtures/liberal_ca/K0A1K0.response
221
+ - spec/fixtures/liberal_ca/T5S2B9.response
222
+ - spec/fixtures/liberal_ca/X1B1B1.response
223
+ - spec/fixtures/ndp_ca/A1A1A1.response
224
+ - spec/fixtures/ndp_ca/G0C2Y0.response
225
+ - spec/fixtures/ndp_ca/H0H0H0.response
226
+ - spec/fixtures/ndp_ca/K0A1K0.response
227
+ - spec/fixtures/ndp_ca/T5S2B9.response
228
+ - spec/fixtures/ndp_ca/X1B1B1.response
229
+ - spec/fixtures/parl_gc_ca/A1A1A1.response
230
+ - spec/fixtures/parl_gc_ca/G0C2Y0.response
231
+ - spec/fixtures/parl_gc_ca/H0H0H0.response
232
+ - spec/fixtures/parl_gc_ca/K0A1K0.response
233
+ - spec/fixtures/parl_gc_ca/T5S2B9.response
234
+ - spec/fixtures/parl_gc_ca/X1B1B1.response
154
235
  - spec/greenparty_ca_spec.rb
155
236
  - spec/liberal_ca_spec.rb
156
237
  - spec/ndp_ca_spec.rb
157
238
  - spec/postal_code_spec.rb
158
239
  - spec/spec.opts
159
240
  - spec/spec_helper.rb
241
+ - spec/strategy_set_spec.rb
@@ -1,28 +0,0 @@
1
- module GovKit
2
- module CA
3
- module PostalCode
4
- module Strategy
5
- # Occasionally suffers from timeout errors.
6
- class DigitalCopyrightCa < Base
7
- base_uri 'www.digital-copyright.ca'
8
-
9
- private
10
-
11
- def electoral_districts!
12
- Nokogiri::HTML(response.parsed_response).css('.node .content a').map{|a| a[:href][/\d+\Z/]}
13
- end
14
-
15
- def valid?
16
- !response.parsed_response.match /\binvalid postal code\b/
17
- end
18
-
19
- def response
20
- @response ||= self.class.get "/edid/postal?postalcode=#{@postal_code}"
21
- end
22
- end
23
-
24
- StrategySet.register DigitalCopyrightCa
25
- end
26
- end
27
- end
28
- end
data/tasks/rid_to_edid.rb DELETED
@@ -1,136 +0,0 @@
1
- require 'csv'
2
- require 'open-uri'
3
- require File.expand_path('../../lib/gov_kit-ca', __FILE__)
4
-
5
- # http://www.digital-copyright.ca/pcfrf/pcfrf.tgz contains
6
- # `postal-code-for-districts.csv`, "which is 308 postal codes that should
7
- # map to each of the 308 different electoral districts." However, six of the
8
- # postal codes do not exist (G0A2C0, J8M1R8, J0W1B0, J0B1H0, L0J1B0, N2A1A3),
9
- # 14 are duplicate, and the remaining 294 map to 246 electoral districts.
10
- desc "Picks the set cover for postal codes to cbc.ca riding IDs"
11
- task :trim_postal_codes do |t,args|
12
- abort "Usage: rake #{t.name} file=postal-codes-for-districts.csv" unless args[:file]
13
-
14
- # Get the riding IDs that each postal code covers
15
- postal_to_rid = {}
16
- File.read(args[:file]).split("\n").uniq.each do |postal_code| # Remove duplicate postal codes
17
- begin
18
- postal_to_rid[postal_code] = GovKit::CA::PostalCode::Strategy::CbcCa.new(postal_code).json_response.map{|hash| hash['rid'].to_i}
19
- rescue JSON::ParserError => e # Ignore invalid postal codes
20
- puts "ERROR: #{postal_code} is invalid"
21
- end
22
- end
23
-
24
- # Get the minimum number of postal codes to cover all riding IDs. This is an
25
- # instance of the set cover problem, which is NP-complete. We use the greedy
26
- # algorithm, which is the best-possible polynomial time approximation
27
- # algorithm for set cover. http://en.wikipedia.org/wiki/Set_cover_problem
28
- postal_codes = []
29
- until postal_to_rid.empty?
30
- postal_code, rids = postal_to_rid.find{|k,v| v.size == postal_to_rid.map{|k,v| v.size}.max}
31
- postal_to_rid.each{|k,v| postal_to_rid[k] -= rids}
32
- postal_to_rid.reject!{|k,v| v.empty?}
33
- postal_codes << postal_code
34
- end
35
-
36
- puts postal_codes.sort
37
- end
38
-
39
- # Use https://github.com/danielharan/pc_scraper/raw/master/data/index.csv and
40
- # https://github.com/danielharan/canadian-postal-code-to-electoral-districts/raw/master/pc_edid.yml
41
- # The YAML file may contain invalid postal codes, e.g. M5V1L6, and does not
42
- # contain every electoral district, e.g. 35061. 35061 corresponds to riding ID
43
- # 168, which contains the postal code L1H1X8.
44
- desc "Generates postal codes within the given list of cbc.ca riding IDs"
45
- task :riding_id_to_postal_code do |t,args|
46
- abort "Usage: rake #{t.name} file=riding-ids.csv csv=index.csv yml=pc_edid.yml" unless args[:file] && args[:csv] && args[:yml]
47
-
48
- # The YML file maps postal codes to electoral districts
49
- yml = YAML.load_file(args[:yml])
50
-
51
- # Get an old map of riding IDs to electoral districts
52
- rid_to_edid = {}
53
- CSV.foreach(args[:csv]) do |row|
54
- rid_to_edid[row[1]] = row[0]
55
- end
56
-
57
- # Map riding IDs to postal codes
58
- rid_to_postal = {}
59
- File.read(args[:file]).split("\n").each do |riding_id|
60
- match = yml.find{|k,v| v == rid_to_edid[riding_id]}
61
- if match
62
- rid_to_postal[riding_id] = match[0]
63
- else
64
- puts "ERROR: No postal code for riding ID #{riding_id}, electoral district #{rid_to_edid[riding_id]}"
65
- end
66
- end
67
-
68
- puts rid_to_postal.values.sort
69
- end
70
-
71
- # Pass postal-code-for-districts.csv from http://www.digital-copyright.ca/pcfrf/pcfrf.tgz
72
- # through `rake trim_postal_codes`. This rake task will print a list of riding
73
- # IDs which none of the given postal codes belong to. Pass this list through
74
- # `rake riding_id_to_postal_code` to get a list of postal codes belonging to
75
- # those riding IDs. Manually add postal codes for any remaining riding IDs.
76
- desc "Generates YAML mapping cbc.ca riding IDs to electoral districts"
77
- task :riding_id_to_electoral_district do |t,args|
78
- abort "Usage: rake #{t.name} file=postal-codes-for-districts.csv" unless args[:file]
79
-
80
- def transliterate_elections_ca(string)
81
- {
82
- /\342\200\223/ => '-',
83
- /\303\242/ => 'a',
84
- /\303[\251\250]/ => 'e',
85
- /\303\264/ => 'o',
86
- /\303\211/ => 'E',
87
- /\303\216/ => 'I',
88
- }.reduce(string) do |string,map|
89
- string.gsub(map[0], map[1])
90
- end
91
- end
92
-
93
- # Map electoral district names to electoral district IDs
94
- name_to_edid = {}
95
- doc = Nokogiri::HTML(open("http://www.elections.ca/content.aspx?section=res&dir=cir/list&document=index&lang=e").read)
96
- doc.css('sup').remove
97
- edids = doc.css('table[summary^="List"] th.normal').map{|x| x.text.to_i}
98
- doc.css('table[summary^="List"] th.normal + td').map{|x| x.text.gsub(/\s+/, ' ').strip}.each_with_index do |name,index|
99
- name_to_edid[transliterate_elections_ca(name)] = edids[index]
100
- end
101
-
102
- def transliterate_cbc_ca(string)
103
- {
104
- / - / => '-',
105
- /\342/ => 'a',
106
- /[\350\351]/ => 'e',
107
- /\364/ => 'o',
108
- /\311/ => 'E',
109
- /\316/ => 'I',
110
- }.reduce(string) do |string,map|
111
- string.gsub(map[0], map[1])
112
- end
113
- end
114
-
115
- # Map electoral district names to riding IDs
116
- name_to_rid = {}
117
- File.read(args[:file]).split("\n").each do |postal_code|
118
- GovKit::CA::PostalCode::Strategy::CbcCa.new(postal_code).json_response.each do |hash|
119
- name_to_rid[transliterate_cbc_ca(hash['name'])] = hash['rid'].to_i
120
- end
121
- end
122
-
123
- # Can we generate a complete mapping?
124
- missing = name_to_edid.keys - name_to_rid.keys
125
- unless missing.empty?
126
- abort "The postal codes in #{args[:file]} do not cover #{missing.size} electoral districts:\n#{missing.sort.join("\n")}\n\nThe missing cbc.ca riding IDs are:\n#{([*1..308] - name_to_rid.map{|k,v| v}.flatten).sort.join("\n")}"
127
- end
128
-
129
- # Map riding IDs to electoral districts
130
- rid_to_edid = {}
131
- name_to_edid.keys.each do |name|
132
- rid_to_edid[name_to_rid[name]] = name_to_edid[name]
133
- end
134
-
135
- puts rid_to_edid.to_yaml
136
- end